
com.tinkerpop.gremlin.structure.util.ElementHelper Maven / Gradle / Ivy
package com.tinkerpop.gremlin.structure.util;
import com.tinkerpop.gremlin.structure.Edge;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.Graph;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.Vertex;
import org.javatuples.Pair;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
/**
* Utility class supporting common functions for {@link com.tinkerpop.gremlin.structure.Element}.
*
* @author Marko A. Rodriguez (http://markorodriguez.com)
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
public class ElementHelper {
public static Vertex getOrAddVertex(final Graph graph, final Object id, final String label) {
try {
return graph.v(id);
} catch (final NoSuchElementException e) {
return graph.addVertex(Element.ID, id, Element.LABEL, label);
}
}
/**
* Determines whether the property key/value for the specified thing can be legally set. This is typically used as
* a pre-condition check prior to setting a property.
*
* @param key the key of the property
* @param value the value of the property
* @throws IllegalArgumentException whether the key/value pair is legal and if not, a clear reason exception
* message is provided
*/
public static void validateProperty(final String key, final Object value) throws IllegalArgumentException {
if (null == value)
throw Property.Exceptions.propertyValueCanNotBeNull();
if (null == key)
throw Property.Exceptions.propertyKeyCanNotBeNull();
if (key.equals(Element.ID))
throw Property.Exceptions.propertyKeyIdIsReserved();
if (key.equals(Element.LABEL))
throw Property.Exceptions.propertyKeyLabelIsReserved();
if (key.isEmpty())
throw Property.Exceptions.propertyKeyCanNotBeEmpty();
}
/**
* Determines whether a list of key/values are legal, ensuring that there are an even number of values submitted
* and that the key values in the list of arguments are {@link String} or {@link com.tinkerpop.gremlin.structure.Element} objects.
*
* @param propertyKeyValues a list of key/value pairs
* @throws IllegalArgumentException if something in the pairs is illegal
*/
public static void legalPropertyKeyValueArray(final Object... propertyKeyValues) throws IllegalArgumentException {
if (propertyKeyValues.length % 2 != 0)
throw Element.Exceptions.providedKeyValuesMustBeAMultipleOfTwo();
for (int i = 0; i < propertyKeyValues.length; i = i + 2) {
if (!(propertyKeyValues[i] instanceof String))
throw Element.Exceptions.providedKeyValuesMustHaveALegalKeyOnEvenIndices();
}
}
/**
* Extracts the value of the {@link com.tinkerpop.gremlin.structure.Element#ID} key from the list of arguments.
*
* @param keyValues a list of key/value pairs
* @return the value associated with {@link com.tinkerpop.gremlin.structure.Element#ID}
* @throws NullPointerException if the value for the {@link com.tinkerpop.gremlin.structure.Element#ID} key is {@code null}
*/
public static Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy