1 | package com.framsticks.core; |
---|
2 | |
---|
3 | import com.framsticks.params.Access; |
---|
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 | import com.framsticks.util.dispatching.ExceptionResultHandler; |
---|
9 | import com.framsticks.util.dispatching.IgnoreExceptionHandler; |
---|
10 | import com.framsticks.util.lang.Pair; |
---|
11 | |
---|
12 | import java.util.Iterator; |
---|
13 | import java.util.LinkedList; |
---|
14 | import java.util.List; |
---|
15 | import java.util.regex.Pattern; |
---|
16 | |
---|
17 | import javax.annotation.Nonnull; |
---|
18 | import javax.annotation.concurrent.Immutable; |
---|
19 | |
---|
20 | import org.apache.commons.collections.ListUtils; |
---|
21 | |
---|
22 | /** |
---|
23 | * @author Piotr Sniegowski |
---|
24 | */ |
---|
25 | @Immutable |
---|
26 | public final class Path { |
---|
27 | // private final static Logger log = LogManager.getLogger(Path.class.getName()); |
---|
28 | |
---|
29 | final Tree tree; |
---|
30 | final String textual; |
---|
31 | final LinkedList<Node> nodes; |
---|
32 | |
---|
33 | protected static Object getKnownChild(Tree tree, Access access, CompositeParam param, ExceptionResultHandler handler) { |
---|
34 | Object child = access.get(param, Object.class); |
---|
35 | if (child == null) { |
---|
36 | return null; |
---|
37 | } |
---|
38 | try { |
---|
39 | tree.prepareAccess(param); |
---|
40 | tree.putSideNote(child, CompositeParam.class, param); |
---|
41 | return child; |
---|
42 | } catch (FramsticksException e) { |
---|
43 | handler.handle(e); |
---|
44 | } |
---|
45 | return null; |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * @param tree |
---|
50 | * @param textual |
---|
51 | * @param nodes |
---|
52 | */ |
---|
53 | Path(Tree tree, String textual, LinkedList<Node> nodes) { |
---|
54 | this.tree = tree; |
---|
55 | this.textual = textual; |
---|
56 | this.nodes = nodes; |
---|
57 | } |
---|
58 | |
---|
59 | public Path appendNode(Node node) { |
---|
60 | assert isResolved(); |
---|
61 | return new PathBuilder().tree(tree).textual(textual + ((size() == 1) ? "" : "/") + node.getParam().getId()).add(nodes).add(node).finish(); |
---|
62 | } |
---|
63 | |
---|
64 | public Path appendParam(CompositeParam param) { |
---|
65 | assert isResolved(); |
---|
66 | return appendNode(new Node(tree, param, null)); |
---|
67 | } |
---|
68 | |
---|
69 | public static class PathBuilder { |
---|
70 | |
---|
71 | Tree tree; |
---|
72 | String textual; |
---|
73 | final LinkedList<Node> nodes = new LinkedList<Node>(); |
---|
74 | |
---|
75 | public Path finish() { |
---|
76 | assert tree != null; |
---|
77 | assert textual != null; |
---|
78 | return new Path(tree, textual, nodes); |
---|
79 | } |
---|
80 | |
---|
81 | public PathBuilder() |
---|
82 | { |
---|
83 | } |
---|
84 | |
---|
85 | public PathBuilder tree(Tree tree) { |
---|
86 | this.tree = tree; |
---|
87 | return this; |
---|
88 | } |
---|
89 | |
---|
90 | public PathBuilder textual(String textual) { |
---|
91 | this.textual = textual; |
---|
92 | return this; |
---|
93 | } |
---|
94 | |
---|
95 | public PathBuilder add(List<Node> nodes) { |
---|
96 | this.nodes.addAll(nodes); |
---|
97 | return this; |
---|
98 | } |
---|
99 | |
---|
100 | public PathBuilder add(Node node) { |
---|
101 | this.nodes.add(node); |
---|
102 | return this; |
---|
103 | } |
---|
104 | |
---|
105 | public PathBuilder setLast(Object object) { |
---|
106 | Node n = nodes.pollLast(); |
---|
107 | nodes.add(new Node(n.getTree(), n.getParam(), object)); |
---|
108 | return this; |
---|
109 | } |
---|
110 | |
---|
111 | public PathBuilder buildUpTo(List<Node> nodes, Node node) { |
---|
112 | StringBuilder b = new StringBuilder(); |
---|
113 | boolean add = false; |
---|
114 | for (Node n : nodes) { |
---|
115 | this.nodes.add(n); |
---|
116 | if (add) { |
---|
117 | b.append("/").append(n.getParam().getId()); |
---|
118 | } |
---|
119 | add = true; |
---|
120 | if (n == node) { |
---|
121 | break; |
---|
122 | } |
---|
123 | } |
---|
124 | this.textual = (nodes.size() == 1) ? "/" : b.toString(); |
---|
125 | return this; |
---|
126 | } |
---|
127 | |
---|
128 | public static Iterator<String> splitPath(String path) { |
---|
129 | List<String> list = new LinkedList<String>(); |
---|
130 | for (String s : path.split("/")) { |
---|
131 | if (!s.isEmpty()) { |
---|
132 | list.add(s); |
---|
133 | } |
---|
134 | } |
---|
135 | return list.iterator(); |
---|
136 | } |
---|
137 | |
---|
138 | public PathBuilder resolve(@Nonnull Tree tree, String textual) { |
---|
139 | return resolve(tree, textual, IgnoreExceptionHandler.getInstance()); |
---|
140 | } |
---|
141 | |
---|
142 | public PathBuilder resolve(@Nonnull Tree tree, String textual, ExceptionResultHandler handler) { |
---|
143 | |
---|
144 | assert nodes.isEmpty(); |
---|
145 | assert tree.isActive(); |
---|
146 | this.tree = tree; |
---|
147 | |
---|
148 | Node current = tree.getAssignedRoot(); |
---|
149 | nodes.add(current); |
---|
150 | |
---|
151 | StringBuilder b = new StringBuilder(); |
---|
152 | Iterator<String> i = splitPath(textual); |
---|
153 | while (i.hasNext() && current.getObject() != null) { |
---|
154 | Access access = TreeOperations.bindAccess(current);// tree.prepareAccess(current.getParam()); |
---|
155 | String e = i.next(); |
---|
156 | Param p = access.getParam(e); |
---|
157 | if (p == null) { |
---|
158 | break; |
---|
159 | } |
---|
160 | if (!(p instanceof CompositeParam)) { |
---|
161 | throw new FramsticksException().msg("param is not a composite").arg("param", p).arg("tree", tree).arg("textual", textual).arg("access", access); |
---|
162 | } |
---|
163 | CompositeParam c = (CompositeParam) p; |
---|
164 | b.append("/").append(e); |
---|
165 | access.select(current.getObject()); |
---|
166 | tree.putSideNote(current.getObject(), CompositeParam.class, current.getParam()); |
---|
167 | current = new Node(current.getTree(), c, getKnownChild(tree, access, c, handler)); |
---|
168 | nodes.add(current); |
---|
169 | } |
---|
170 | this.textual = (nodes.size() == 1) ? "/" : b.toString(); |
---|
171 | |
---|
172 | return this; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | public static PathBuilder build() { |
---|
177 | return new PathBuilder(); |
---|
178 | } |
---|
179 | |
---|
180 | public Path appendResolution(Object object) { |
---|
181 | assert !isResolved(); |
---|
182 | Path result = new PathBuilder().textual(textual).tree(tree).add(nodes).setLast(object).finish(); |
---|
183 | assert size() == result.size(); |
---|
184 | return result; |
---|
185 | } |
---|
186 | |
---|
187 | public final Object getTopObject() { |
---|
188 | return getTop().getObject(); |
---|
189 | } |
---|
190 | |
---|
191 | public final Node getTop() { |
---|
192 | return nodes.getLast(); |
---|
193 | } |
---|
194 | |
---|
195 | public final Node getUnder() { |
---|
196 | assert nodes.size() >= 2; |
---|
197 | return nodes.get(nodes.size() - 2); |
---|
198 | } |
---|
199 | |
---|
200 | public final String getTextual() { |
---|
201 | return textual; |
---|
202 | } |
---|
203 | |
---|
204 | public String toString() { |
---|
205 | return tree + textual + (!isResolved() ? "!" : ""); |
---|
206 | } |
---|
207 | |
---|
208 | public String getFullTextual() { |
---|
209 | return tree.getName() + textual; |
---|
210 | } |
---|
211 | |
---|
212 | public final int size() { |
---|
213 | assert Dispatching.isThreadSafe(); |
---|
214 | return nodes.size(); |
---|
215 | } |
---|
216 | |
---|
217 | public final boolean isResolved() { |
---|
218 | assert Dispatching.isThreadSafe(); |
---|
219 | return getTop().getObject() != null; |
---|
220 | } |
---|
221 | |
---|
222 | public final boolean isResolved(String textual) { |
---|
223 | assert Dispatching.isThreadSafe(); |
---|
224 | return isTheSame(textual) && isResolved(); |
---|
225 | } |
---|
226 | |
---|
227 | public final boolean isTheSame(String textual) { |
---|
228 | assert Dispatching.isThreadSafe(); |
---|
229 | return this.textual.equals(textual); |
---|
230 | } |
---|
231 | |
---|
232 | public final boolean isTheSameTextually(Path path) { |
---|
233 | assert Dispatching.isThreadSafe(); |
---|
234 | return (tree == path.getTree()) && textual.equals(path.getTextual()); |
---|
235 | } |
---|
236 | |
---|
237 | public final @Nonnull Tree getTree() { |
---|
238 | assert Dispatching.isThreadSafe(); |
---|
239 | return tree; |
---|
240 | } |
---|
241 | |
---|
242 | public Path tryResolveIfNeeded() { |
---|
243 | if (isResolved()) { |
---|
244 | return this; |
---|
245 | } |
---|
246 | return tryFindResolution(); |
---|
247 | } |
---|
248 | |
---|
249 | /** Attach resolution at end, if available. |
---|
250 | * |
---|
251 | * @return Modified path, if resolution was available, this otherwise. |
---|
252 | */ |
---|
253 | public Path tryFindResolution() { |
---|
254 | assert tree.isActive(); |
---|
255 | assert !isResolved(); |
---|
256 | if (size() == 1) { |
---|
257 | return Path.build().resolve(tree, "/").finish(); |
---|
258 | } |
---|
259 | Object child = getKnownChild(tree, TreeOperations.bindAccess(getUnder()), getTop().getParam(), IgnoreExceptionHandler.getInstance()); |
---|
260 | if (child == null) { |
---|
261 | return this; |
---|
262 | } |
---|
263 | return appendResolution(child); |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | public String getLastElement() { |
---|
268 | return getTop().getParam().getId(); |
---|
269 | } |
---|
270 | |
---|
271 | public final boolean isOwner(Tree tree) { |
---|
272 | return this.tree == tree; |
---|
273 | } |
---|
274 | |
---|
275 | @SuppressWarnings("unchecked") |
---|
276 | public |
---|
277 | List<Node> getNodes() { |
---|
278 | return ListUtils.unmodifiableList(nodes); |
---|
279 | } |
---|
280 | |
---|
281 | public Path assureResolved() { |
---|
282 | if (!isResolved()) { |
---|
283 | throw new FramsticksException().msg("path is not resolved").arg("path", this); |
---|
284 | } |
---|
285 | return this; |
---|
286 | } |
---|
287 | |
---|
288 | public static Path tryTo(@Nonnull Tree tree, String textual) { |
---|
289 | return Path.build().resolve(tree, textual).finish(); |
---|
290 | } |
---|
291 | |
---|
292 | public static Path to(@Nonnull Tree tree, String textual) { |
---|
293 | Path path = tryTo(tree, textual); |
---|
294 | if (path.getTextual().equals(textual)) { |
---|
295 | return path; |
---|
296 | } |
---|
297 | throw new FramsticksException().msg("failed to create path").arg("textual", textual).arg("result", path).arg("tree", tree); |
---|
298 | } |
---|
299 | |
---|
300 | public boolean isTheSameObjects(Path path) { |
---|
301 | if (tree != path.getTree()) { |
---|
302 | return false; |
---|
303 | } |
---|
304 | if (size() != path.size()) { |
---|
305 | return false; |
---|
306 | } |
---|
307 | if (!getTextual().equals(path.getTextual())) { |
---|
308 | return false; |
---|
309 | } |
---|
310 | Iterator<Node> a = nodes.iterator(); |
---|
311 | Iterator<Node> b = path.getNodes().iterator(); |
---|
312 | while (a.hasNext()) { |
---|
313 | assert b.hasNext(); |
---|
314 | if (a.next() != b.next()) { |
---|
315 | return false; |
---|
316 | } |
---|
317 | } |
---|
318 | return true; |
---|
319 | } |
---|
320 | |
---|
321 | public static final Pattern pathPattern = Pattern.compile("(\\/)|((\\/[^/]+)+)"); |
---|
322 | |
---|
323 | public static boolean isValidString(String path) { |
---|
324 | return pathPattern.matcher(path).matches(); |
---|
325 | } |
---|
326 | |
---|
327 | public static String appendString(String path, String element) { |
---|
328 | if (path.equals("/")) { |
---|
329 | return path + element; |
---|
330 | } |
---|
331 | return path + "/" + element; |
---|
332 | } |
---|
333 | |
---|
334 | public static Pair<String, String> removeLastElement(String path) { |
---|
335 | assert isValidString(path); |
---|
336 | if (path.equals("/")) { |
---|
337 | throw new FramsticksException().msg("cannot remove last element from root path"); |
---|
338 | } |
---|
339 | int index = path.lastIndexOf('/'); |
---|
340 | assert index != -1; |
---|
341 | if (index == 0) { |
---|
342 | return new Pair<String, String>("/", path.substring(1)); |
---|
343 | } |
---|
344 | return new Pair<String, String>(path.substring(0, index), path.substring(index + 1)); |
---|
345 | } |
---|
346 | |
---|
347 | public static String validateString(String path) { |
---|
348 | if (!isValidString(path)) { |
---|
349 | throw new FramsticksException().msg("path string validation failured").arg("path", path); |
---|
350 | } |
---|
351 | return path; |
---|
352 | } |
---|
353 | |
---|
354 | |
---|
355 | // public boolean isEmpty() { |
---|
356 | // return nodes.isEmpty(); |
---|
357 | // } |
---|
358 | } |
---|
359 | |
---|