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.FramsticksException; |
---|
7 | import com.framsticks.util.dispatching.Dispatching; |
---|
8 | |
---|
9 | import java.util.Iterator; |
---|
10 | import java.util.LinkedList; |
---|
11 | import java.util.List; |
---|
12 | |
---|
13 | import javax.annotation.concurrent.Immutable; |
---|
14 | |
---|
15 | import org.apache.commons.collections.ListUtils; |
---|
16 | |
---|
17 | /** |
---|
18 | * @author Piotr Sniegowski |
---|
19 | */ |
---|
20 | @Immutable |
---|
21 | public final class Path { |
---|
22 | // private final static Logger log = Logger.getLogger(Path.class.getName()); |
---|
23 | |
---|
24 | final Instance instance; |
---|
25 | final String textual; |
---|
26 | final LinkedList<Node> nodes; |
---|
27 | |
---|
28 | protected static Object getKnownChild(Instance instance, AccessInterface access, CompositeParam param) { |
---|
29 | Object child = access.get(param, Object.class); |
---|
30 | if (child == null) { |
---|
31 | return null; |
---|
32 | } |
---|
33 | try { |
---|
34 | instance.registry.prepareAccess(param); |
---|
35 | return child; |
---|
36 | } catch (FramsticksException e) { |
---|
37 | } |
---|
38 | return null; |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * @param instance |
---|
43 | * @param textual |
---|
44 | * @param nodes |
---|
45 | */ |
---|
46 | Path(Instance instance, String textual, LinkedList<Node> nodes) { |
---|
47 | this.instance = instance; |
---|
48 | this.textual = textual; |
---|
49 | this.nodes = nodes; |
---|
50 | } |
---|
51 | |
---|
52 | public Path appendNode(Node node) { |
---|
53 | assert isResolved(); |
---|
54 | return new PathBuilder().instance(instance).textual(textual + ((size() == 1) ? "" : "/") + node.getParam().getId()).add(nodes).add(node).finish(); |
---|
55 | } |
---|
56 | |
---|
57 | public Path appendParam(CompositeParam param) { |
---|
58 | assert isResolved(); |
---|
59 | return appendNode(new Node(param, null)); |
---|
60 | } |
---|
61 | |
---|
62 | public static class PathBuilder { |
---|
63 | |
---|
64 | Instance instance; |
---|
65 | String textual; |
---|
66 | final LinkedList<Node> nodes = new LinkedList<Node>(); |
---|
67 | |
---|
68 | public Path finish() { |
---|
69 | assert instance != null; |
---|
70 | assert textual != null; |
---|
71 | return new Path(instance, textual, nodes); |
---|
72 | } |
---|
73 | |
---|
74 | public PathBuilder() |
---|
75 | { |
---|
76 | } |
---|
77 | |
---|
78 | public PathBuilder instance(Instance instance) { |
---|
79 | this.instance = instance; |
---|
80 | return this; |
---|
81 | } |
---|
82 | |
---|
83 | public PathBuilder textual(String textual) { |
---|
84 | this.textual = textual; |
---|
85 | return this; |
---|
86 | } |
---|
87 | |
---|
88 | public PathBuilder add(List<Node> nodes) { |
---|
89 | this.nodes.addAll(nodes); |
---|
90 | return this; |
---|
91 | } |
---|
92 | |
---|
93 | public PathBuilder add(Node node) { |
---|
94 | this.nodes.add(node); |
---|
95 | return this; |
---|
96 | } |
---|
97 | |
---|
98 | public PathBuilder setLast(Object object) { |
---|
99 | Node n = nodes.pollLast(); |
---|
100 | nodes.add(new Node(n.getParam(), object)); |
---|
101 | return this; |
---|
102 | } |
---|
103 | |
---|
104 | public PathBuilder buildUpTo(List<Node> nodes, Node node) { |
---|
105 | StringBuilder b = new StringBuilder(); |
---|
106 | boolean add = false; |
---|
107 | for (Node n : nodes) { |
---|
108 | this.nodes.add(n); |
---|
109 | if (add) { |
---|
110 | b.append("/").append(n.getParam().getId()); |
---|
111 | } |
---|
112 | add = true; |
---|
113 | if (n == node) { |
---|
114 | break; |
---|
115 | } |
---|
116 | } |
---|
117 | this.textual = (nodes.size() == 1) ? "/" : b.toString(); |
---|
118 | return this; |
---|
119 | } |
---|
120 | |
---|
121 | public PathBuilder resolve(Instance instance, String textual) { |
---|
122 | |
---|
123 | assert nodes.isEmpty(); |
---|
124 | assert instance.isActive(); |
---|
125 | this.instance = instance; |
---|
126 | |
---|
127 | nodes.add(instance.getRoot()); |
---|
128 | Node current = instance.getRoot(); |
---|
129 | |
---|
130 | StringBuilder b = new StringBuilder(); |
---|
131 | Iterator<String> i = Instance.splitPath(textual); |
---|
132 | while (i.hasNext() && current.getObject() != null) { |
---|
133 | AccessInterface access = instance.registry.prepareAccess(current.getParam()); |
---|
134 | if (access == null) { |
---|
135 | break; |
---|
136 | } |
---|
137 | String e = i.next(); |
---|
138 | Param p = access.getParam(e); |
---|
139 | if (!(p instanceof CompositeParam)) { |
---|
140 | //entries.add(new Entry()); |
---|
141 | break; |
---|
142 | } |
---|
143 | CompositeParam c = (CompositeParam)p; |
---|
144 | b.append("/").append(e); |
---|
145 | access.select(current.getObject()); |
---|
146 | current = new Node(c, getKnownChild(instance, access, c)); |
---|
147 | nodes.add(current); |
---|
148 | } |
---|
149 | this.textual = (nodes.size() == 1) ? "/" : b.toString(); |
---|
150 | |
---|
151 | return this; |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | public static PathBuilder build() { |
---|
156 | return new PathBuilder(); |
---|
157 | } |
---|
158 | |
---|
159 | public Path appendResolution(Object object) { |
---|
160 | assert !isResolved(); |
---|
161 | Path result = new PathBuilder().textual(textual).instance(instance).add(nodes).setLast(object).finish(); |
---|
162 | assert size() == result.size(); |
---|
163 | return result; |
---|
164 | } |
---|
165 | |
---|
166 | public final Object getTopObject() { |
---|
167 | return getTop().getObject(); |
---|
168 | } |
---|
169 | |
---|
170 | public final Node getTop() { |
---|
171 | return nodes.getLast(); |
---|
172 | } |
---|
173 | |
---|
174 | public final Node getUnder() { |
---|
175 | assert nodes.size() >= 2; |
---|
176 | return nodes.get(nodes.size() - 2); |
---|
177 | } |
---|
178 | |
---|
179 | public final String getTextual() { |
---|
180 | return textual; |
---|
181 | } |
---|
182 | |
---|
183 | public String toString() { |
---|
184 | return instance + textual + (!isResolved() ? "!" : ""); |
---|
185 | } |
---|
186 | |
---|
187 | public final int size() { |
---|
188 | assert Dispatching.isThreadSafe(); |
---|
189 | return nodes.size(); |
---|
190 | } |
---|
191 | |
---|
192 | public final boolean isResolved() { |
---|
193 | assert Dispatching.isThreadSafe(); |
---|
194 | return getTop().getObject() != null; |
---|
195 | } |
---|
196 | |
---|
197 | public final boolean isResolved(String textual) { |
---|
198 | assert Dispatching.isThreadSafe(); |
---|
199 | return isTheSame(textual) && isResolved(); |
---|
200 | } |
---|
201 | |
---|
202 | public final boolean isTheSame(String textual) { |
---|
203 | assert Dispatching.isThreadSafe(); |
---|
204 | return this.textual.equals(textual); |
---|
205 | } |
---|
206 | |
---|
207 | public final Instance getInstance() { |
---|
208 | assert Dispatching.isThreadSafe(); |
---|
209 | return instance; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | /** Attach resolution at end, if available. |
---|
214 | * |
---|
215 | * @return Modified path, if resolution was available, this otherwise. |
---|
216 | */ |
---|
217 | public Path tryFindResolution() { |
---|
218 | assert instance.isActive(); |
---|
219 | assert !isResolved(); |
---|
220 | if (size() == 1) { |
---|
221 | return Path.build().resolve(instance, "/").finish();//appendResolution(instance.root.object); |
---|
222 | } |
---|
223 | Object child = getKnownChild(instance, instance.bindAccess(getUnder()), getTop().getParam()); |
---|
224 | if (child == null) { |
---|
225 | return this; |
---|
226 | } |
---|
227 | return appendResolution(child); |
---|
228 | } |
---|
229 | |
---|
230 | public boolean matches(Path p) { |
---|
231 | assert Dispatching.isThreadSafe(); |
---|
232 | assert instance == p.instance; |
---|
233 | Iterator<Node> a = nodes.iterator(); |
---|
234 | Iterator<Node> b = p.nodes.iterator(); |
---|
235 | while (a.hasNext() && b.hasNext()) { |
---|
236 | Node an = a.next(); |
---|
237 | Node bn = b.next(); |
---|
238 | if (an.object != bn.object) { |
---|
239 | return false; |
---|
240 | } |
---|
241 | } |
---|
242 | return a.hasNext() == b.hasNext(); |
---|
243 | } |
---|
244 | |
---|
245 | public String getLastElement() { |
---|
246 | return getTop().getParam().getId(); |
---|
247 | } |
---|
248 | |
---|
249 | public final boolean isOwner(Instance instance) { |
---|
250 | return this.instance == instance; |
---|
251 | } |
---|
252 | |
---|
253 | // public void setInstance(Instance instance) { |
---|
254 | // this.instance = instance; |
---|
255 | // } |
---|
256 | |
---|
257 | @SuppressWarnings("unchecked") |
---|
258 | public |
---|
259 | List<Node> getNodes() { |
---|
260 | return ListUtils.unmodifiableList(nodes); |
---|
261 | } |
---|
262 | |
---|
263 | public void assureResolved() { |
---|
264 | if (!isResolved()) { |
---|
265 | throw new FramsticksException().msg("path is not resolved").arg("path", this); |
---|
266 | } |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|