private void validateHomogenousIds(final List<Object> ids) { final Iterator<Object> iterator = ids.iterator(); Object id = iterator.next(); if (id == null) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); final Class firstClass = id.getClass(); while (iterator.hasNext()) { id = iterator.next(); if (id == null || !id.getClass().equals(firstClass)) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } }
/** * Determine whether an array of ids are either all elements or ids of elements. This is typically used as a pre-condition check. * * @param clazz the class of the element for which the ids will bind * @param ids the ids that must be either elements or id objects, else * {@link org.apache.tinkerpop.gremlin.structure.Graph.Exceptions#idArgsMustBeEitherIdOrElement()} is thrown. */ public static void validateMixedElementIds(final Class<? extends Element> clazz, final Object... ids) throws IllegalArgumentException { if (ids.length > 1) { final boolean element = clazz.isAssignableFrom(ids[0].getClass()); for (int i = 1; i < ids.length; i++) { if (clazz.isAssignableFrom(ids[i].getClass()) != element) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } } }
@Override public Iterator<Vertex> vertices(final Object... vertexIds) { try { if (0 == vertexIds.length) { return new HadoopVertexIterator(this); } else { // base the conversion function on the first item in the id list as the expectation is that these // id values will be a uniform list if (vertexIds[0] instanceof Vertex) { // based on the first item assume all vertices in the argument list if (!Stream.of(vertexIds).allMatch(id -> id instanceof Vertex)) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); // no need to get the vertices again, so just flip it back - some implementation may want to treat this // as a refresh operation. that's not necessary for hadoopgraph. return Stream.of(vertexIds).map(id -> (Vertex) id).iterator(); } else { final Class<?> firstClass = vertexIds[0].getClass(); if (!Stream.of(vertexIds).map(Object::getClass).allMatch(firstClass::equals)) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); // todo: change exception to be ids of the same type return IteratorUtils.filter(new HadoopVertexIterator(this), vertex -> ElementHelper.idExists(vertex.id(), vertexIds)); } } } catch (final IOException e) { throw new IllegalStateException(e.getMessage(), e); } }
/** * Determine whether an array of ids are either all elements or ids of elements. This is typically used as a pre-condition check. * * @param clazz the class of the element for which the ids will bind * @param ids the ids that must be either elements or id objects, else * {@link org.apache.tinkerpop.gremlin.structure.Graph.Exceptions#idArgsMustBeEitherIdOrElement()} is thrown. */ public static void validateMixedElementIds(final Class<? extends Element> clazz, final Object... ids) throws IllegalArgumentException { if (ids.length > 1) { final boolean element = clazz.isAssignableFrom(ids[0].getClass()); for (int i = 1; i < ids.length; i++) { if (clazz.isAssignableFrom(ids[i].getClass()) != element) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ANY_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_STRING_IDS) public void shouldNotMixTypesForGettingSpecificVerticesWithVertexFirst() { final Vertex v1 = graph.addVertex(); try { graph.vertices(v1, graphProvider.convertId("1", Vertex.class)); fail("Should have thrown an exception because id arguments were mixed."); } catch (Exception ex) { final Exception expected = Graph.Exceptions.idArgsMustBeEitherIdOrElement(); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ANY_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_STRING_IDS) public void shouldNotMixTypesForGettingSpecificVerticesWithStringFirst() { final Vertex v1 = graph.addVertex(); try { graph.vertices(graphProvider.convertId("1", Vertex.class), v1); fail("Should have thrown an exception because id arguments were mixed."); } catch (Exception ex) { final Exception expected = Graph.Exceptions.idArgsMustBeEitherIdOrElement(); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Override public Iterator<Edge> edges(Object... edgeIds) { if (edgeIds == null || edgeIds.length == 0) return queryHandler.edges(); if(edgeIds.length > 1 && !edgeIds[0].getClass().equals(edgeIds[1].getClass())) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); if(edgeIds[0] instanceof Edge) { ArrayList<Edge> list = new ArrayList(); for(int i = 0; i < edgeIds.length; i++) list.add((Edge) edgeIds[i]); return list.iterator(); } return queryHandler.edges(edgeIds); }
private void validateHomogenousIds(final List<Object> ids) { final Iterator<Object> iterator = ids.iterator(); Object id = iterator.next(); if (id == null) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); final Class firstClass = id.getClass(); while (iterator.hasNext()) { id = iterator.next(); if (id == null || !id.getClass().equals(firstClass)) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } }
@Test @LoadGraphWith(MODERN) public void shouldThrowExceptionWhenIdsMixed() { final List<Vertex> vertices = g.V().toList(); try { g.V(vertices.get(0), vertices.get(1).id()).toList(); } catch (Exception ex) { validateException(Graph.Exceptions.idArgsMustBeEitherIdOrElement(), ex); } }
@Override public Iterator<Vertex> vertices(Object... vertexIds) { if (vertexIds == null || vertexIds.length == 0) return (Iterator<Vertex>) queryHandler.vertices(); if(vertexIds.length > 1 && !vertexIds[0].getClass().equals(vertexIds[1].getClass())) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); if(vertexIds[0] instanceof Vertex) { ArrayList<Vertex> list = new ArrayList(); for(int i = 0; i < vertexIds.length; i++) list.add((Vertex) vertexIds[i]); return list.iterator(); } return (Iterator<Vertex>) queryHandler.vertices(vertexIds); }
public static void verifyArgsMustBeEitherIdorElement(Object... ids) { assert ids.length>0; int numElements = 0; for (int i = 0; i < ids.length; i++) { if (ids[i] instanceof Element) numElements++; } if (numElements>0 && numElements<ids.length) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } }
private void validateHomogenousIds(final List<Object> ids) { final Iterator<Object> iterator = ids.iterator(); Object id = iterator.next(); if (id == null) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); final Class firstClass = id.getClass(); while (iterator.hasNext()) { id = iterator.next(); if (id == null || !id.getClass().equals(firstClass)) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } }
@Override public Iterator<Edge> edges(final Object... edgeIds) { try { if (0 == edgeIds.length) { return new HadoopEdgeIterator(this); } else { // base the conversion function on the first item in the id list as the expectation is that these // id values will be a uniform list if (edgeIds[0] instanceof Edge) { // based on the first item assume all Edges in the argument list if (!Stream.of(edgeIds).allMatch(id -> id instanceof Edge)) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); // no need to get the vertices again, so just flip it back - some implementation may want to treat this // as a refresh operation. that's not necessary for hadoopgraph. return Stream.of(edgeIds).map(id -> (Edge) id).iterator(); } else { final Class<?> firstClass = edgeIds[0].getClass(); if (!Stream.of(edgeIds).map(Object::getClass).allMatch(firstClass::equals)) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); // todo: change exception to be ids of the same type return IteratorUtils.filter(new HadoopEdgeIterator(this), vertex -> ElementHelper.idExists(vertex.id(), edgeIds)); } } } catch (final IOException e) { throw new IllegalStateException(e.getMessage(), e); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ANY_IDS) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_STRING_IDS) public void shouldNotMixTypesForGettingSpecificEdgesWithStringFirst() { final Vertex v = graph.addVertex(); final Edge e1 = v.addEdge("self", v); try { graph.edges(graphProvider.convertId("1", Edge.class), e1); fail("Should have thrown an exception because id arguments were mixed."); } catch (Exception ex) { final Exception expected = Graph.Exceptions.idArgsMustBeEitherIdOrElement(); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ANY_IDS) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_STRING_IDS) public void shouldNotMixTypesForGettingSpecificEdgesWithEdgeFirst() { final Vertex v = graph.addVertex(); final Edge e1 = v.addEdge("self", v); try { graph.edges(e1, graphProvider.convertId("1", Edge.class)); fail("Should have thrown an exception because id arguments were mixed."); } catch (Exception ex) { final Exception expected = Graph.Exceptions.idArgsMustBeEitherIdOrElement(); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ANY_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_STRING_IDS) public void shouldNotMixTypesForGettingSpecificVerticesWithVertexFirst() { final Vertex v1 = graph.addVertex(); try { graph.vertices(v1, graphProvider.convertId("1", Vertex.class)); fail("Should have thrown an exception because id arguments were mixed."); } catch (Exception ex) { final Exception expected = Graph.Exceptions.idArgsMustBeEitherIdOrElement(); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ANY_IDS) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_STRING_IDS) public void shouldNotMixTypesForGettingSpecificVerticesWithStringFirst() { final Vertex v1 = graph.addVertex(); try { graph.vertices(graphProvider.convertId("1", Vertex.class), v1); fail("Should have thrown an exception because id arguments were mixed."); } catch (Exception ex) { final Exception expected = Graph.Exceptions.idArgsMustBeEitherIdOrElement(); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Test @LoadGraphWith(MODERN) public void shouldThrowExceptionWhenIdsMixed() { final List<Vertex> vertices = g.V().toList(); try { g.V(vertices.get(0), vertices.get(1).id()).toList(); } catch (Exception ex) { validateException(Graph.Exceptions.idArgsMustBeEitherIdOrElement(), ex); } }
public static void verifyArgsMustBeEitherIdOrElement(Object... ids) { assert ids.length>0; int numElements = 0; for (Object id : ids) { if (id instanceof Element) numElements++; } if (numElements>0 && numElements<ids.length) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } }
public static void verifyArgsMustBeEitherIdorElement(Object... ids) { assert ids.length>0; int numElements = 0; for (int i = 0; i < ids.length; i++) { if (ids[i] instanceof Element) numElements++; } if (numElements>0 && numElements<ids.length) throw Graph.Exceptions.idArgsMustBeEitherIdOrElement(); } }