@NotNull static ItemPathImpl createFromArray(@NotNull Object[] components) { ItemPathImpl path = new ItemPathImpl(); path.addAll(components); return path; }
@NotNull static ItemPathImpl createFromListReverse(@NotNull List<?> components) { ItemPathImpl path = new ItemPathImpl(); for (int i = components.size() - 1; i >= 0; i--) { path.add(components.get(i)); } return path; }
/** * Creates the path from given components. * @see ItemPath#create(Object...) */ @NotNull static ItemPath create(@NotNull List<?> components) { return ItemPathImpl.createFromList(components); }
@Override public ItemPath firstAsPath() { return segments.isEmpty() ? this : new ItemPathImpl(Collections.singletonList(first())); }
@NotNull static ItemPathImpl createFromIterator(@NotNull Iterator<?> iterator) { ItemPathImpl path = new ItemPathImpl(); iterator.forEachRemaining(path::add); return path; }
private void addAll(List<?> components) { for (Object component : components) { add(component); } }
@NotNull @Override public ItemPath rest() { return rest(1); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); shortDump(sb); return sb.toString(); } }
/** * Creates the path from given components. The components can contain objects of various kinds: * - QName -> interpreted as either named segment or a special segment (if the name exactly matches special segment name) * - Integer/Long -> interpreted as Id path segment * - null -> interpreted as null Id path segment * - ItemPathSegment -> interpreted as such * - ItemPath, Object[], Collection -> interpreted recursively as a sequence of components * * Creates the default implementation of ItemPathImpl. Components are normalized on creation as necessary; although * the number of object creation is minimized. */ @NotNull static ItemPath create(Object... components) { return ItemPathImpl.createFromArray(components); }
/** * Creates the path from given components in reverse direction. E.g. [a, b, c] -> c/b/a */ @NotNull static ItemPath createReverse(@NotNull List<?> components) { return ItemPathImpl.createFromListReverse(components); }
private void add(Object component) { if (component instanceof ItemPath && !(component instanceof ItemName)) { addAll(((ItemPath) component).getSegments()); } else if (component instanceof Object[]) { addAll((Object[]) component); } else if (component instanceof String) { segments.add(new ItemName((String) component)); } else if (component != null) { segments.add(component); } else { segments.add(IdItemPathSegment.NULL); } }
public static ItemPath remainder(ItemPath main, ItemPath prefix) { ItemPathNormalizingIterator mainIterator = normalizingIterator(main); ItemPathNormalizingIterator prefixIterator = normalizingIterator(prefix); while (prefixIterator.hasNext()) { if (!mainIterator.hasNext()) { throw new IllegalArgumentException("Cannot subtract '"+prefix+"' from path '"+main+ "' because it is not a prefix (subpath): it is a superpath instead."); } Object mainSegment = mainIterator.next(); Object prefixSegment = prefixIterator.next(); if (!segmentsEquivalent(mainSegment, prefixSegment)) { throw new IllegalArgumentException("Cannot subtract segment '"+prefixSegment+"' from path '"+main+ "' because it does not contain corresponding segment; it has '"+mainSegment+"' instead."); } } return ItemPathImpl.createFromIterator(mainIterator); }
@NotNull @Override public ItemPath removeIds() { List<Object> filtered = new ArrayList<>(segments); filtered.removeIf(ItemPath::isId); return new ItemPathImpl(filtered); }
private void addAll(Object[] components) { for (Object component : components) { add(component); } }
@NotNull static ItemPathImpl createFromList(@NotNull List<?> components) { ItemPathImpl path = new ItemPathImpl(); path.addAll(components); return path; }
@Override public ItemPath subPath(int from, int to) { int fromClipped = Math.max(0, from); int toClipped = Math.min(to, segments.size()); if (fromClipped >= toClipped) { return EMPTY_PATH; } else { // TODO ... the problem is that subList is not serializable; but this creates one object more return new ItemPathImpl(new ArrayList<>(segments.subList(fromClipped, toClipped))); } }
@NotNull @Override public ItemPath namedSegmentsOnly() { List<Object> newComponents = new ArrayList<>(); for (Object segment : segments) { if (ItemPath.isName(segment)) { newComponents.add(segment); } } return new ItemPathImpl(newComponents); }