public Builder edge(final Edge edge) { if (entities.isEmpty()) { entities.add(new AbstractMap.SimpleEntry<>(edge.getMatchedVertexValue(), Sets.newHashSet())); } else { verifyEdge(entities.getLast().getKey(), edge); } edges.add(Sets.newHashSet(edge)); entities.add(new AbstractMap.SimpleEntry<>(edge.getAdjacentMatchedVertexValue(), Sets.newHashSet())); return this; }
private Builder edgeSet(final Set<Edge> edges) { final Object matchedVertexValue = edges.stream() .findFirst() .orElseThrow(IllegalAccessError::new) .getMatchedVertexValue(); final Object adjacentMatchedVertexValue = edges.stream() .findFirst() .orElseThrow(IllegalAccessError::new) .getAdjacentMatchedVertexValue(); if (entities.isEmpty()) { entities.add(new AbstractMap.SimpleEntry<>(matchedVertexValue, Sets.newHashSet())); } else { final Object root = entities.getLast().getKey(); if (!Objects.equals(root, matchedVertexValue)) { throw new IllegalArgumentException("Edge must continue the current walk."); } } this.edges.add(edges); entities.add(new AbstractMap.SimpleEntry<>(adjacentMatchedVertexValue, Sets.newHashSet())); return this; }
public Builder entity(final Entity entity) { if (!edges.isEmpty()) { final Object root = edges.getLast().stream().findAny().orElseThrow(RuntimeException::new).getAdjacentMatchedVertexValue(); verifyEntity(root, entity); } if (!entities.isEmpty()) { final Object root = entities.getLast().getKey(); verifyEntity(root, entity); final Entry<Object, Set<Entity>> entry = entities.getLast(); final Set<Entity> currentEntities = entry.getValue(); currentEntities.add(entity); entry.setValue(currentEntities); } else { entities.push(new AbstractMap.SimpleEntry<>(entity.getVertex(), Sets.newHashSet(entity))); } return this; }
public Builder entities(final Iterable<Entity> entities) { if (Iterables.isEmpty(entities)) { return this; } if (Iterables.size(entities) == 1) { return entity(entities.iterator().next()); } if (distinct(Streams.toStream(entities).map(EntityId::getVertex).collect(toList()))) { throw new IllegalArgumentException("Entities must all have the same vertex."); } final Entity entity = entities.iterator().next(); if (!edges.isEmpty()) { final Object root = edges.getLast().stream().findAny().orElseThrow(RuntimeException::new).getAdjacentMatchedVertexValue(); verifyEntity(root, entity); } if (!this.entities.isEmpty()) { final Entry<Object, Set<Entity>> entry = this.entities.getLast(); final Object root = entry.getKey(); verifyEntity(root, entity); final Set<Entity> currentEntities = entry.getValue(); currentEntities.addAll(Lists.newArrayList(entities)); entry.setValue(currentEntities); } else { this.entities.push(new AbstractMap.SimpleEntry<>(entity.getVertex(), Sets.newHashSet(entities))); } return this; }
@Override public Object getIdentifier(final IdentifierType identifierType) { switch (identifierType) { case GROUP: return getGroup(); case SOURCE: return getSource(); case DESTINATION: return getDestination(); case DIRECTED: return isDirected(); case MATCHED_VERTEX: return getMatchedVertexValue(); case ADJACENT_MATCHED_VERTEX: return getAdjacentMatchedVertexValue(); default: return null; } }
@Test public void shouldSetAndGetIdentifiersWithMatchedDestination() { // Given final Edge edge = new Edge.Builder() .group("group") .source("source vertex") .dest("destination vertex") .directed(true) .matchedVertex(EdgeId.MatchedVertex.DESTINATION) .build(); // When/Then assertEquals("destination vertex", edge.getMatchedVertexValue()); assertEquals("destination vertex", edge.getIdentifier(IdentifierType.MATCHED_VERTEX)); assertEquals("source vertex", edge.getIdentifier(IdentifierType.ADJACENT_MATCHED_VERTEX)); assertEquals("source vertex", edge.getAdjacentMatchedVertexValue()); assertTrue(edge.isDirected()); }
@Test public void shouldSetAndGetIdentifiersWithMatchedSource() { // Given final Edge edge = new Edge.Builder() .group("group") .source("source vertex") .dest("destination vertex") .directed(true) .matchedVertex(EdgeId.MatchedVertex.SOURCE) .build(); // When/Then assertEquals("source vertex", edge.getMatchedVertexValue()); assertEquals("source vertex", edge.getIdentifier(IdentifierType.MATCHED_VERTEX)); assertEquals("destination vertex", edge.getAdjacentMatchedVertexValue()); assertEquals("destination vertex", edge.getIdentifier(IdentifierType.ADJACENT_MATCHED_VERTEX)); assertTrue(edge.isDirected()); }
public Builder edge(final Edge edge) { if (entities.isEmpty()) { entities.add(new AbstractMap.SimpleEntry<>(edge.getMatchedVertexValue(), Sets.newHashSet())); } else { verifyEdge(entities.getLast().getKey(), edge); } edges.add(Sets.newHashSet(edge)); entities.add(new AbstractMap.SimpleEntry<>(edge.getAdjacentMatchedVertexValue(), Sets.newHashSet())); return this; }
private Builder edgeSet(final Set<Edge> edges) { final Object matchedVertexValue = edges.stream() .findFirst() .orElseThrow(IllegalAccessError::new) .getMatchedVertexValue(); final Object adjacentMatchedVertexValue = edges.stream() .findFirst() .orElseThrow(IllegalAccessError::new) .getAdjacentMatchedVertexValue(); if (entities.isEmpty()) { entities.add(new AbstractMap.SimpleEntry<>(matchedVertexValue, Sets.newHashSet())); } else { final Object root = entities.getLast().getKey(); if (!Objects.equals(root, matchedVertexValue)) { throw new IllegalArgumentException("Edge must continue the current walk."); } } this.edges.add(edges); entities.add(new AbstractMap.SimpleEntry<>(adjacentMatchedVertexValue, Sets.newHashSet())); return this; }
public Builder entity(final Entity entity) { if (!edges.isEmpty()) { final Object root = edges.getLast().stream().findAny().orElseThrow(RuntimeException::new).getAdjacentMatchedVertexValue(); verifyEntity(root, entity); } if (!entities.isEmpty()) { final Object root = entities.getLast().getKey(); verifyEntity(root, entity); final Entry<Object, Set<Entity>> entry = entities.getLast(); final Set<Entity> currentEntities = entry.getValue(); currentEntities.add(entity); entry.setValue(currentEntities); } else { entities.push(new AbstractMap.SimpleEntry<>(entity.getVertex(), Sets.newHashSet(entity))); } return this; }
public Builder entities(final Iterable<Entity> entities) { if (Iterables.isEmpty(entities)) { return this; } if (Iterables.size(entities) == 1) { return entity(entities.iterator().next()); } if (distinct(Streams.toStream(entities).map(EntityId::getVertex).collect(toList()))) { throw new IllegalArgumentException("Entities must all have the same vertex."); } final Entity entity = entities.iterator().next(); if (!edges.isEmpty()) { final Object root = edges.getLast().stream().findAny().orElseThrow(RuntimeException::new).getAdjacentMatchedVertexValue(); verifyEntity(root, entity); } if (!this.entities.isEmpty()) { final Entry<Object, Set<Entity>> entry = this.entities.getLast(); final Object root = entry.getKey(); verifyEntity(root, entity); final Set<Entity> currentEntities = entry.getValue(); currentEntities.addAll(Lists.newArrayList(entities)); entry.setValue(currentEntities); } else { this.entities.push(new AbstractMap.SimpleEntry<>(entity.getVertex(), Sets.newHashSet(entities))); } return this; }
@Override public Object getIdentifier(final IdentifierType identifierType) { switch (identifierType) { case GROUP: return getGroup(); case SOURCE: return getSource(); case DESTINATION: return getDestination(); case DIRECTED: return isDirected(); case MATCHED_VERTEX: return getMatchedVertexValue(); case ADJACENT_MATCHED_VERTEX: return getAdjacentMatchedVertexValue(); default: return null; } }