[77] | 1 | package com.framsticks.core; |
---|
| 2 | |
---|
[100] | 3 | import com.framsticks.params.Access; |
---|
[84] | 4 | import com.framsticks.params.CompositeParam; |
---|
[77] | 5 | import com.framsticks.params.Param; |
---|
[90] | 6 | import com.framsticks.util.FramsticksException; |
---|
[84] | 7 | import com.framsticks.util.dispatching.Dispatching; |
---|
[99] | 8 | import com.framsticks.util.dispatching.ExceptionResultHandler; |
---|
| 9 | import com.framsticks.util.dispatching.IgnoreExceptionHandler; |
---|
| 10 | import com.framsticks.util.lang.Pair; |
---|
[85] | 11 | |
---|
[77] | 12 | import java.util.Iterator; |
---|
| 13 | import java.util.LinkedList; |
---|
[84] | 14 | import java.util.List; |
---|
[99] | 15 | import java.util.regex.Pattern; |
---|
[77] | 16 | |
---|
[96] | 17 | import javax.annotation.Nonnull; |
---|
[85] | 18 | import javax.annotation.concurrent.Immutable; |
---|
| 19 | |
---|
[84] | 20 | import org.apache.commons.collections.ListUtils; |
---|
| 21 | |
---|
[77] | 22 | /** |
---|
| 23 | * @author Piotr Sniegowski |
---|
| 24 | */ |
---|
[85] | 25 | @Immutable |
---|
[87] | 26 | public final class Path { |
---|
[100] | 27 | // private final static Logger log = LogManager.getLogger(Path.class.getName()); |
---|
[77] | 28 | |
---|
[97] | 29 | final Tree tree; |
---|
[87] | 30 | final String textual; |
---|
| 31 | final LinkedList<Node> nodes; |
---|
[77] | 32 | |
---|
[100] | 33 | protected static Object getKnownChild(Tree tree, Access access, CompositeParam param, ExceptionResultHandler handler) { |
---|
[84] | 34 | Object child = access.get(param, Object.class); |
---|
| 35 | if (child == null) { |
---|
| 36 | return null; |
---|
| 37 | } |
---|
[90] | 38 | try { |
---|
[97] | 39 | tree.prepareAccess(param); |
---|
[100] | 40 | tree.putSideNote(child, CompositeParam.class, param); |
---|
[90] | 41 | return child; |
---|
| 42 | } catch (FramsticksException e) { |
---|
[99] | 43 | handler.handle(e); |
---|
[90] | 44 | } |
---|
| 45 | return null; |
---|
[84] | 46 | } |
---|
[77] | 47 | |
---|
[87] | 48 | /** |
---|
[97] | 49 | * @param tree |
---|
[87] | 50 | * @param textual |
---|
| 51 | * @param nodes |
---|
| 52 | */ |
---|
[97] | 53 | Path(Tree tree, String textual, LinkedList<Node> nodes) { |
---|
| 54 | this.tree = tree; |
---|
[87] | 55 | this.textual = textual; |
---|
| 56 | this.nodes = nodes; |
---|
| 57 | } |
---|
[77] | 58 | |
---|
[87] | 59 | public Path appendNode(Node node) { |
---|
| 60 | assert isResolved(); |
---|
[97] | 61 | return new PathBuilder().tree(tree).textual(textual + ((size() == 1) ? "" : "/") + node.getParam().getId()).add(nodes).add(node).finish(); |
---|
[87] | 62 | } |
---|
[77] | 63 | |
---|
[87] | 64 | public Path appendParam(CompositeParam param) { |
---|
| 65 | assert isResolved(); |
---|
[98] | 66 | return appendNode(new Node(tree, param, null)); |
---|
[87] | 67 | } |
---|
[77] | 68 | |
---|
[87] | 69 | public static class PathBuilder { |
---|
[77] | 70 | |
---|
[97] | 71 | Tree tree; |
---|
[87] | 72 | String textual; |
---|
| 73 | final LinkedList<Node> nodes = new LinkedList<Node>(); |
---|
| 74 | |
---|
| 75 | public Path finish() { |
---|
[97] | 76 | assert tree != null; |
---|
[87] | 77 | assert textual != null; |
---|
[97] | 78 | return new Path(tree, textual, nodes); |
---|
[84] | 79 | } |
---|
[77] | 80 | |
---|
[87] | 81 | public PathBuilder() |
---|
| 82 | { |
---|
| 83 | } |
---|
| 84 | |
---|
[97] | 85 | public PathBuilder tree(Tree tree) { |
---|
| 86 | this.tree = tree; |
---|
[87] | 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(); |
---|
[98] | 107 | nodes.add(new Node(n.getTree(), n.getParam(), object)); |
---|
[87] | 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 | } |
---|
[84] | 123 | } |
---|
[87] | 124 | this.textual = (nodes.size() == 1) ? "/" : b.toString(); |
---|
| 125 | return this; |
---|
[84] | 126 | } |
---|
[77] | 127 | |
---|
[96] | 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 | } |
---|
[77] | 137 | |
---|
[97] | 138 | public PathBuilder resolve(@Nonnull Tree tree, String textual) { |
---|
[99] | 139 | return resolve(tree, textual, IgnoreExceptionHandler.getInstance()); |
---|
| 140 | } |
---|
[96] | 141 | |
---|
[99] | 142 | public PathBuilder resolve(@Nonnull Tree tree, String textual, ExceptionResultHandler handler) { |
---|
| 143 | |
---|
[87] | 144 | assert nodes.isEmpty(); |
---|
[97] | 145 | assert tree.isActive(); |
---|
| 146 | this.tree = tree; |
---|
[77] | 147 | |
---|
[98] | 148 | Node current = tree.getAssignedRoot(); |
---|
| 149 | nodes.add(current); |
---|
[87] | 150 | |
---|
| 151 | StringBuilder b = new StringBuilder(); |
---|
[96] | 152 | Iterator<String> i = splitPath(textual); |
---|
[87] | 153 | while (i.hasNext() && current.getObject() != null) { |
---|
[100] | 154 | Access access = TreeOperations.bindAccess(current);// tree.prepareAccess(current.getParam()); |
---|
[87] | 155 | String e = i.next(); |
---|
| 156 | Param p = access.getParam(e); |
---|
[99] | 157 | if (p == null) { |
---|
[87] | 158 | break; |
---|
| 159 | } |
---|
[99] | 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; |
---|
[87] | 164 | b.append("/").append(e); |
---|
| 165 | access.select(current.getObject()); |
---|
[100] | 166 | tree.putSideNote(current.getObject(), CompositeParam.class, current.getParam()); |
---|
[99] | 167 | current = new Node(current.getTree(), c, getKnownChild(tree, access, c, handler)); |
---|
[87] | 168 | nodes.add(current); |
---|
| 169 | } |
---|
| 170 | this.textual = (nodes.size() == 1) ? "/" : b.toString(); |
---|
| 171 | |
---|
| 172 | return this; |
---|
| 173 | } |
---|
[84] | 174 | } |
---|
[77] | 175 | |
---|
[87] | 176 | public static PathBuilder build() { |
---|
| 177 | return new PathBuilder(); |
---|
[84] | 178 | } |
---|
[77] | 179 | |
---|
[84] | 180 | public Path appendResolution(Object object) { |
---|
| 181 | assert !isResolved(); |
---|
[97] | 182 | Path result = new PathBuilder().textual(textual).tree(tree).add(nodes).setLast(object).finish(); |
---|
[87] | 183 | assert size() == result.size(); |
---|
[84] | 184 | return result; |
---|
| 185 | } |
---|
[77] | 186 | |
---|
[84] | 187 | public final Object getTopObject() { |
---|
| 188 | return getTop().getObject(); |
---|
| 189 | } |
---|
[77] | 190 | |
---|
[84] | 191 | public final Node getTop() { |
---|
| 192 | return nodes.getLast(); |
---|
| 193 | } |
---|
[77] | 194 | |
---|
[84] | 195 | public final Node getUnder() { |
---|
| 196 | assert nodes.size() >= 2; |
---|
| 197 | return nodes.get(nodes.size() - 2); |
---|
| 198 | } |
---|
[77] | 199 | |
---|
[84] | 200 | public final String getTextual() { |
---|
| 201 | return textual; |
---|
| 202 | } |
---|
[77] | 203 | |
---|
[84] | 204 | public String toString() { |
---|
[97] | 205 | return tree + textual + (!isResolved() ? "!" : ""); |
---|
[84] | 206 | } |
---|
[77] | 207 | |
---|
[97] | 208 | public String getFullTextual() { |
---|
| 209 | return tree.getName() + textual; |
---|
| 210 | } |
---|
| 211 | |
---|
[84] | 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 | |
---|
[98] | 232 | public final boolean isTheSameTextually(Path path) { |
---|
| 233 | assert Dispatching.isThreadSafe(); |
---|
| 234 | return (tree == path.getTree()) && textual.equals(path.getTextual()); |
---|
| 235 | } |
---|
| 236 | |
---|
[97] | 237 | public final @Nonnull Tree getTree() { |
---|
[84] | 238 | assert Dispatching.isThreadSafe(); |
---|
[97] | 239 | return tree; |
---|
[84] | 240 | } |
---|
| 241 | |
---|
[96] | 242 | public Path tryResolveIfNeeded() { |
---|
| 243 | if (isResolved()) { |
---|
| 244 | return this; |
---|
| 245 | } |
---|
| 246 | return tryFindResolution(); |
---|
| 247 | } |
---|
[84] | 248 | |
---|
| 249 | /** Attach resolution at end, if available. |
---|
| 250 | * |
---|
| 251 | * @return Modified path, if resolution was available, this otherwise. |
---|
| 252 | */ |
---|
| 253 | public Path tryFindResolution() { |
---|
[97] | 254 | assert tree.isActive(); |
---|
[84] | 255 | assert !isResolved(); |
---|
| 256 | if (size() == 1) { |
---|
[97] | 257 | return Path.build().resolve(tree, "/").finish(); |
---|
[84] | 258 | } |
---|
[99] | 259 | Object child = getKnownChild(tree, TreeOperations.bindAccess(getUnder()), getTop().getParam(), IgnoreExceptionHandler.getInstance()); |
---|
[84] | 260 | if (child == null) { |
---|
| 261 | return this; |
---|
| 262 | } |
---|
| 263 | return appendResolution(child); |
---|
| 264 | } |
---|
[77] | 265 | |
---|
| 266 | |
---|
[84] | 267 | public String getLastElement() { |
---|
| 268 | return getTop().getParam().getId(); |
---|
| 269 | } |
---|
[77] | 270 | |
---|
[97] | 271 | public final boolean isOwner(Tree tree) { |
---|
| 272 | return this.tree == tree; |
---|
[84] | 273 | } |
---|
| 274 | |
---|
| 275 | @SuppressWarnings("unchecked") |
---|
| 276 | public |
---|
| 277 | List<Node> getNodes() { |
---|
| 278 | return ListUtils.unmodifiableList(nodes); |
---|
| 279 | } |
---|
[90] | 280 | |
---|
[98] | 281 | public Path assureResolved() { |
---|
[90] | 282 | if (!isResolved()) { |
---|
| 283 | throw new FramsticksException().msg("path is not resolved").arg("path", this); |
---|
| 284 | } |
---|
[98] | 285 | return this; |
---|
[90] | 286 | } |
---|
[96] | 287 | |
---|
[99] | 288 | public static Path tryTo(@Nonnull Tree tree, String textual) { |
---|
[97] | 289 | return Path.build().resolve(tree, textual).finish(); |
---|
[96] | 290 | } |
---|
[98] | 291 | |
---|
[99] | 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 | |
---|
[98] | 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 | |
---|
[99] | 321 | public static final Pattern pathPattern = Pattern.compile("(\\/)|((\\/[^/]+)+)"); |
---|
[98] | 322 | |
---|
[99] | 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 | |
---|
[98] | 355 | // public boolean isEmpty() { |
---|
| 356 | // return nodes.isEmpty(); |
---|
| 357 | // } |
---|
[77] | 358 | } |
---|
| 359 | |
---|