source: java/main/src/main/java/com/framsticks/core/Path.java @ 77

Last change on this file since 77 was 77, checked in by psniegowski, 11 years ago

Add new java codebase.

File size: 4.7 KB
Line 
1package com.framsticks.core;
2
3import com.framsticks.params.AccessInterface;
4import com.framsticks.params.Param;
5import com.framsticks.params.types.CompositeParam;
6import com.framsticks.util.Dispatching;
7import org.apache.log4j.Logger;
8
9import java.util.Iterator;
10import java.util.LinkedList;
11
12/**
13 * @author Piotr Sniegowski
14 */
15public class Path {
16    private final static Logger LOGGER = Logger.getLogger(Path.class.getName());
17
18    final LinkedList<Node> nodes = new LinkedList<Node>();
19    String textual;
20    Instance instance;
21
22    protected Object getKnownChild(AccessInterface access, CompositeParam param) {
23        Object child = access.get(param, Object.class);
24        if (child == null) {
25            return null;
26        }
27        return (instance.prepareAccess(param) != null) ? child : null;
28    }
29
30    public Path(Instance instance, String textual) {
31        assert instance.isActive();
32        this.instance = instance;
33        StringBuilder b = new StringBuilder();
34        nodes.add(instance.root);
35        Node current = instance.root;
36        Iterator<String> i = Instance.splitPath(textual);
37        while (i.hasNext() && current.getObject() != null) {
38            AccessInterface access = instance.prepareAccess(current.getParam());
39            if (access == null) {
40                break;
41            }
42            String e = i.next();
43            Param p = access.getParam(e);
44            if (!(p instanceof CompositeParam)) {
45                //entries.add(new Entry());
46                break;
47            }
48            CompositeParam c = (CompositeParam)p;
49            b.append("/").append(e);
50            access.select(current.getObject());
51            current = new Node(c, getKnownChild(access, c));
52            nodes.add(current);
53        }
54        this.textual = (size() == 1) ? "/" : b.toString();
55    }
56
57    protected Path() {
58
59    }
60
61    public Path appendNode(Node node) {
62        assert isResolved();
63        Path result = new Path();
64        result.textual = textual + ((size() == 1) ? "" : "/") + node.getParam().getId();
65        result.instance = instance;
66        result.nodes.addAll(nodes);
67        result.nodes.add(node);
68        return result;
69    }
70
71    public Path appendParam(CompositeParam param) {
72        assert isResolved();
73        return appendNode(new Node(param, null));
74    }
75
76    public Path appendResolution(Object object) {
77        assert !isResolved();
78        Path result = new Path();
79        result.textual = textual;
80        result.instance = instance;
81        result.nodes.addAll(nodes);
82        result.nodes.add(new Node(result.nodes.pollLast().getParam(), object));
83        return result;
84    }
85
86    public final Object getTopObject() {
87        return getTop().getObject();
88    }
89
90    public final Node getTop() {
91        return nodes.getLast();
92    }
93
94    public final Node getUnder() {
95        assert nodes.size() >= 2;
96        return nodes.get(nodes.size() - 2);
97    }
98
99    public final String getTextual() {
100        return textual;
101    }
102
103    public String toString() {
104        return instance + textual + (!isResolved() ? "!" : "");
105    }
106
107    public final int size() {
108        assert Dispatching.isThreadSafe();
109        return nodes.size();
110    }
111
112    public final boolean isResolved() {
113        assert Dispatching.isThreadSafe();
114        return getTop().getObject() != null;
115    }
116
117    public final boolean isResolved(String textual) {
118        assert Dispatching.isThreadSafe();
119        return isTheSame(textual) && isResolved();
120    }
121
122    public final boolean isTheSame(String textual) {
123        assert Dispatching.isThreadSafe();
124        return this.textual.equals(textual);
125    }
126
127    public final Instance getInstance() {
128        assert Dispatching.isThreadSafe();
129        return instance;
130    }
131
132    public Path findResolution() {
133        assert instance.isActive();
134        assert !isResolved();
135        if (size() == 1) {
136                        return new Path(instance, "/");//appendResolution(instance.root.object);
137        }
138        Object child = getKnownChild(instance.bindAccess(getUnder()), getTop().getParam());
139        if (child == null) {
140            return this;
141        }
142        return appendResolution(child);
143    }
144
145    public boolean matches(Path p) {
146        assert Dispatching.isThreadSafe();
147        assert instance == p.instance;
148        Iterator<Node> a = nodes.iterator();
149        Iterator<Node> b = p.nodes.iterator();
150        while (a.hasNext() && b.hasNext()) {
151            Node an = a.next();
152            Node bn = b.next();
153            if (an.object != bn.object) {
154                return false;
155            }
156        }
157        return a.hasNext() == b.hasNext();
158    }
159
160    public String getLastElement() {
161        return getTop().getParam().getId();
162    }
163
164    public final boolean isOwner(Instance instance) {
165        return this.instance == instance;
166    }
167}
168
Note: See TracBrowser for help on using the repository browser.