@Override public void dropPath() { this.path = ImmutablePath.make(); }
@Override public Path extend(final Object object, final Set<String> labels) { return new ImmutablePath(this, object, labels); }
@Override public boolean popEquals(final Pop pop, final Object other) { if (!(other instanceof Path)) return false; final Path otherPath = (Path) other; ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) break; for (final String label : currentPath.currentLabels) { if (!otherPath.hasLabel(label) || !this.get(pop, label).equals(otherPath.get(pop, label))) return false; } currentPath = currentPath.previousPath; } return true; }
@Override public int hashCode() { // hashCode algorithm from AbstractList int[] hashCodes = new int[this.size()]; int index = hashCodes.length - 1; ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) break; hashCodes[index] = currentPath.currentObject.hashCode(); currentPath = currentPath.previousPath; index--; } int hashCode = 1; for (final int hash : hashCodes) { hashCode = hashCode * 31 + hash; } return hashCode; }
@Override public int size() { int counter = 0; ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) return counter; counter++; currentPath = currentPath.previousPath; } }
private Object convertGremlinValue(Object rawValue) { if (rawValue instanceof Vertex) { return GraphDbObjectFactory.createVertex(this, (Vertex) rawValue); } else if (rawValue instanceof Edge) { return GraphDbObjectFactory.createEdge(this, (Edge) rawValue); } else if (rawValue instanceof Map) { Map<String, Object> rowValue = (Map<String, Object>) rawValue; return Maps.transformValues(rowValue, GREMLIN_VALUE_CONVERSION_FUNCTION); } else if (rawValue instanceof ImmutablePath) { ImmutablePath path = (ImmutablePath) rawValue; return convertGremlinValue(path.objects()); } else if (rawValue instanceof List) { return Lists.transform((List) rawValue, GREMLIN_VALUE_CONVERSION_FUNCTION); } else if (rawValue instanceof Collection) { throw new UnsupportedOperationException("Unhandled collection type: " + rawValue.getClass()); } return rawValue; }
@Override public <A> A get(final int index) { int counter = this.size(); ImmutablePath currentPath = this; while (true) { if (index == --counter) return (A) currentPath.currentObject; currentPath = currentPath.previousPath; } }
@Override public boolean equals(final Object other) { if (!(other instanceof Path)) return false; final Path otherPath = (Path) other; int size = this.size(); if (otherPath.size() != size) return false; if (size > 0) { ImmutablePath currentPath = this; final List<Object> otherObjects = otherPath.objects(); final List<Set<String>> otherLabels = otherPath.labels(); for (int i = otherLabels.size() - 1; i >= 0; i--) { if (currentPath.isTail()) return true; else if (!currentPath.currentObject.equals(otherObjects.get(i)) || !currentPath.currentLabels.equals(otherLabels.get(i))) return false; else currentPath = currentPath.previousPath; } } return true; }
@Override public boolean isEmpty() { return this.isTail(); }
private Object convertGremlinValue(Object rawValue) { if (rawValue instanceof Vertex) { return GraphDbObjectFactory.createVertex(this, (Vertex) rawValue); } else if (rawValue instanceof Edge) { return GraphDbObjectFactory.createEdge(this, (Edge) rawValue); } else if (rawValue instanceof Map) { Map<String,Object> rowValue = (Map<String,Object>)rawValue; return Maps.transformValues(rowValue, GREMLIN_VALUE_CONVERSION_FUNCTION); } else if (rawValue instanceof ImmutablePath) { ImmutablePath path = (ImmutablePath) rawValue; return convertGremlinValue(path.objects()); } else if (rawValue instanceof List) { return Lists.transform((List)rawValue, GREMLIN_VALUE_CONVERSION_FUNCTION); } else if (rawValue instanceof Collection) { throw new UnsupportedOperationException("Unhandled collection type: " + rawValue.getClass()); } return rawValue; }
@Override public <A> A get(final int index) { int counter = this.size(); ImmutablePath currentPath = this; while (true) { if (index == --counter) return (A) currentPath.currentObject; currentPath = currentPath.previousPath; } }
@Override public void dropPath() { this.path = ImmutablePath.make(); }
@Override public <A> A get(final Pop pop, final String label) { if (Pop.mixed == pop) { return this.get(label); } else if (Pop.all == pop) { ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) break; else if (currentPath.currentLabels.contains(label)) ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) throw Path.Exceptions.stepWithProvidedLabelDoesNotExist(label); else if (currentPath.currentLabels.contains(label)) ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) break; else if (currentPath.currentLabels.contains(label))
@Override public int hashCode() { // hashCode algorithm from AbstractList int[] hashCodes = new int[this.size()]; int index = hashCodes.length - 1; ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) break; hashCodes[index] = currentPath.currentObject.hashCode(); currentPath = currentPath.previousPath; index--; } int hashCode = 1; for (final int hash : hashCodes) { hashCode = hashCode * 31 + hash; } return hashCode; }
@Override public boolean hasLabel(final String label) { ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) return false; else if (currentPath.currentLabels.contains(label)) return true; else currentPath = currentPath.previousPath; } }
@Override public Path extend(final Set<String> labels) { if (labels.isEmpty() || this.currentLabels.containsAll(labels)) return this; else { final Set<String> newLabels = new LinkedHashSet<>(); newLabels.addAll(this.currentLabels); newLabels.addAll(labels); return new ImmutablePath(this.previousPath, this.currentObject, newLabels); } }
public B_LP_O_S_SE_SL_Traverser(final T t, final Step<T, ?> step, final long initialBulk) { super(t, step, initialBulk); this.path = ImmutablePath.make(); final Set<String> labels = step.getLabels(); if (!labels.isEmpty()) this.path = this.path.extend(t, labels); }
@Override public boolean popEquals(final Pop pop, final Object other) { if (!(other instanceof Path)) return false; final Path otherPath = (Path) other; ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) break; for (final String label : currentPath.currentLabels) { if (!otherPath.hasLabel(label) || !this.get(pop, label).equals(otherPath.get(pop, label))) return false; } currentPath = currentPath.previousPath; } return true; }
@Override public boolean equals(final Object other) { if (!(other instanceof Path)) return false; final Path otherPath = (Path) other; int size = this.size(); if (otherPath.size() != size) return false; if (size > 0) { ImmutablePath currentPath = this; final List<Object> otherObjects = otherPath.objects(); final List<Set<String>> otherLabels = otherPath.labels(); for (int i = otherLabels.size() - 1; i >= 0; i--) { if (currentPath.isTail()) return true; else if (!currentPath.currentObject.equals(otherObjects.get(i)) || !currentPath.currentLabels.equals(otherLabels.get(i))) return false; else currentPath = currentPath.previousPath; } } return true; }
@Override public List<Set<String>> labels() { final List<Set<String>> labels = new ArrayList<>(); ImmutablePath currentPath = this; while (true) { if (currentPath.isTail()) break; labels.add(0, currentPath.currentLabels); currentPath = currentPath.previousPath; } return Collections.unmodifiableList(labels); }