1 | package com.framsticks.core; |
---|
2 | |
---|
3 | import com.framsticks.params.AccessInterface; |
---|
4 | import com.framsticks.params.CompositeParam; |
---|
5 | import com.framsticks.params.Param; |
---|
6 | import com.framsticks.util.dispatching.Dispatching; |
---|
7 | import java.util.Iterator; |
---|
8 | import java.util.LinkedList; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | import org.apache.commons.collections.ListUtils; |
---|
12 | |
---|
13 | /** |
---|
14 | * @author Piotr Sniegowski |
---|
15 | */ |
---|
16 | public class Path { |
---|
17 | // private final static Logger log = Logger.getLogger(Path.class.getName()); |
---|
18 | |
---|
19 | final LinkedList<Node> nodes = new LinkedList<Node>(); |
---|
20 | String textual; |
---|
21 | Instance instance; |
---|
22 | |
---|
23 | protected Object getKnownChild(AccessInterface access, CompositeParam param) { |
---|
24 | Object child = access.get(param, Object.class); |
---|
25 | if (child == null) { |
---|
26 | return null; |
---|
27 | } |
---|
28 | return (instance.registry.prepareAccess(param) != null) ? child : null; |
---|
29 | } |
---|
30 | |
---|
31 | // public Path constructPath(Instance instance, String textual) { |
---|
32 | // assert instance.isActive(); |
---|
33 | |
---|
34 | // } |
---|
35 | |
---|
36 | Path(Instance instance, String textual) { |
---|
37 | assert instance.isActive(); |
---|
38 | this.instance = instance; |
---|
39 | |
---|
40 | nodes.add(instance.root); |
---|
41 | Node current = instance.root; |
---|
42 | |
---|
43 | StringBuilder b = new StringBuilder(); |
---|
44 | Iterator<String> i = Instance.splitPath(textual); |
---|
45 | while (i.hasNext() && current.getObject() != null) { |
---|
46 | AccessInterface access = instance.registry.prepareAccess(current.getParam()); |
---|
47 | if (access == null) { |
---|
48 | break; |
---|
49 | } |
---|
50 | String e = i.next(); |
---|
51 | Param p = access.getParam(e); |
---|
52 | if (!(p instanceof CompositeParam)) { |
---|
53 | //entries.add(new Entry()); |
---|
54 | break; |
---|
55 | } |
---|
56 | CompositeParam c = (CompositeParam)p; |
---|
57 | b.append("/").append(e); |
---|
58 | access.select(current.getObject()); |
---|
59 | current = new Node(c, getKnownChild(access, c)); |
---|
60 | nodes.add(current); |
---|
61 | } |
---|
62 | this.textual = (size() == 1) ? "/" : b.toString(); |
---|
63 | } |
---|
64 | |
---|
65 | public Path(Instance instance, List<Node> nodes, Node node) { |
---|
66 | this.instance = instance; |
---|
67 | StringBuilder b = new StringBuilder(); |
---|
68 | boolean add = false; |
---|
69 | for (Node n : nodes) { |
---|
70 | this.nodes.add(n); |
---|
71 | if (add) { |
---|
72 | b.append("/").append(n.getParam().getId()); |
---|
73 | } |
---|
74 | add = true; |
---|
75 | if (n == node) { |
---|
76 | break; |
---|
77 | } |
---|
78 | } |
---|
79 | this.textual = (size() == 1) ? "/" : b.toString(); |
---|
80 | } |
---|
81 | |
---|
82 | protected Path() { |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | public Path appendNode(Node node) { |
---|
87 | assert isResolved(); |
---|
88 | Path result = new Path(); |
---|
89 | result.textual = textual + ((size() == 1) ? "" : "/") + node.getParam().getId(); |
---|
90 | result.instance = instance; |
---|
91 | result.nodes.addAll(nodes); |
---|
92 | result.nodes.add(node); |
---|
93 | return result; |
---|
94 | } |
---|
95 | |
---|
96 | public Path appendParam(CompositeParam param) { |
---|
97 | assert isResolved(); |
---|
98 | return appendNode(new Node(param, null)); |
---|
99 | } |
---|
100 | |
---|
101 | public Path appendResolution(Object object) { |
---|
102 | assert !isResolved(); |
---|
103 | Path result = new Path(); |
---|
104 | result.textual = textual; |
---|
105 | result.instance = instance; |
---|
106 | result.nodes.addAll(nodes); |
---|
107 | result.nodes.add(new Node(result.nodes.pollLast().getParam(), object)); |
---|
108 | return result; |
---|
109 | } |
---|
110 | |
---|
111 | public final Object getTopObject() { |
---|
112 | return getTop().getObject(); |
---|
113 | } |
---|
114 | |
---|
115 | public final Node getTop() { |
---|
116 | return nodes.getLast(); |
---|
117 | } |
---|
118 | |
---|
119 | public final Node getUnder() { |
---|
120 | assert nodes.size() >= 2; |
---|
121 | return nodes.get(nodes.size() - 2); |
---|
122 | } |
---|
123 | |
---|
124 | public final String getTextual() { |
---|
125 | return textual; |
---|
126 | } |
---|
127 | |
---|
128 | public String toString() { |
---|
129 | return instance + textual + (!isResolved() ? "!" : ""); |
---|
130 | } |
---|
131 | |
---|
132 | public final int size() { |
---|
133 | assert Dispatching.isThreadSafe(); |
---|
134 | return nodes.size(); |
---|
135 | } |
---|
136 | |
---|
137 | public final boolean isResolved() { |
---|
138 | assert Dispatching.isThreadSafe(); |
---|
139 | return getTop().getObject() != null; |
---|
140 | } |
---|
141 | |
---|
142 | public final boolean isResolved(String textual) { |
---|
143 | assert Dispatching.isThreadSafe(); |
---|
144 | return isTheSame(textual) && isResolved(); |
---|
145 | } |
---|
146 | |
---|
147 | public final boolean isTheSame(String textual) { |
---|
148 | assert Dispatching.isThreadSafe(); |
---|
149 | return this.textual.equals(textual); |
---|
150 | } |
---|
151 | |
---|
152 | public final Instance getInstance() { |
---|
153 | assert Dispatching.isThreadSafe(); |
---|
154 | return instance; |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /** Attach resolution at end, if available. |
---|
159 | * |
---|
160 | * @return Modified path, if resolution was available, this otherwise. |
---|
161 | */ |
---|
162 | public Path tryFindResolution() { |
---|
163 | assert instance.isActive(); |
---|
164 | assert !isResolved(); |
---|
165 | if (size() == 1) { |
---|
166 | return new Path(instance, "/");//appendResolution(instance.root.object); |
---|
167 | } |
---|
168 | Object child = getKnownChild(instance.bindAccess(getUnder()), getTop().getParam()); |
---|
169 | if (child == null) { |
---|
170 | return this; |
---|
171 | } |
---|
172 | return appendResolution(child); |
---|
173 | } |
---|
174 | |
---|
175 | public boolean matches(Path p) { |
---|
176 | assert Dispatching.isThreadSafe(); |
---|
177 | assert instance == p.instance; |
---|
178 | Iterator<Node> a = nodes.iterator(); |
---|
179 | Iterator<Node> b = p.nodes.iterator(); |
---|
180 | while (a.hasNext() && b.hasNext()) { |
---|
181 | Node an = a.next(); |
---|
182 | Node bn = b.next(); |
---|
183 | if (an.object != bn.object) { |
---|
184 | return false; |
---|
185 | } |
---|
186 | } |
---|
187 | return a.hasNext() == b.hasNext(); |
---|
188 | } |
---|
189 | |
---|
190 | public String getLastElement() { |
---|
191 | return getTop().getParam().getId(); |
---|
192 | } |
---|
193 | |
---|
194 | public final boolean isOwner(Instance instance) { |
---|
195 | return this.instance == instance; |
---|
196 | } |
---|
197 | |
---|
198 | @SuppressWarnings("unchecked") |
---|
199 | public |
---|
200 | List<Node> getNodes() { |
---|
201 | return ListUtils.unmodifiableList(nodes); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|