Class ElementHelper
java.lang.Object
org.apache.tinkerpop.gremlin.structure.util.ElementHelper
Utility class supporting common functions for
Element.- Author:
- Marko A. Rodriguez (http://markorodriguez.com), Stephen Mallette (http://stephen.genoprime.com)
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplyLabelsToVertex(Vertex vertex, Object labelValue) Applies label value(s) to a vertex using addLabel semantics.static booleanstatic booleanA standard method for determining if twoElementobjects are equal.static booleanA standard method for determining if twoPropertyobjects are equal.static booleanareEqual(VertexProperty a, Object b) A standard method for determining if twoVertexPropertyobjects are equal.static booleanstatic booleanConverts a set of key values to a Map.Convert a set of key values to a list of Pair objects.static voidattachProperties(Element element, Object... propertyKeyValues) Assign key/value pairs as properties to anElement.static voidattachProperties(Vertex vertex, Object... propertyKeyValues) Assign key/value pairs as properties to anVertex.static voidattachProperties(Vertex vertex, VertexProperty.Cardinality cardinality, Object... propertyKeyValues) Assign key/value pairs as properties to aVertex.getIdValue(Object... keyValues) Extracts the value of theT.idkey from the list of arguments.Gets the list of keys from the key values.getLabelsValue(Object... keyValues) getLabelValue(Object... keyValues) Extracts the value of theT.labelkey from the list of arguments.static Object[]getProperties(Element element, boolean includeId, boolean includeLabel, Set<String> propertiesToCopy) Retrieve the properties associated with a particular element.static intIf twoElementinstances are equal, then they must have the same hash codes.static intIf twoPropertyinstances are equal, then they must have the same hash codes.static booleanhaveEqualIds(Element a, Element b) Simply tests if the value returned fromElement.id()areequal().static booleanstatic booleanChecks if a key exists within a list of provided keys.static voidlegalPropertyKeyValueArray(Object... propertyKeyValues) propertyMap(Element element, String... propertyKeys) propertyValueMap(Element element, String... propertyKeys) Remove a key from the set of key/value pairs.Removes an accessor from the set of key/value pairs.static Object[]replaceKey(Object[] keyValues, Object oldKey, Object newKey) Replaces one key with a different key.static <V> Optional<VertexProperty<V>>stageVertexProperty(Vertex vertex, VertexProperty.Cardinality cardinality, String key, V value, Object... keyValues) This is a helper method for dealing with vertex property cardinality and typically used inVertex.property(VertexProperty.Cardinality, String, Object, Object...).static Object[]Append a key/value pair to a list of existing key/values.static voidvalidateLabel(String label) Determine whether theElementlabel can be legally set.static voidvalidateProperty(String key, Object value) Determines whether the property key/value for the specified thing can be legally set.static Map<String,List<VertexProperty>> vertexPropertyMap(Vertex vertex, String... propertyKeys) vertexPropertyValueMap(Vertex vertex, String... propertyKeys)
-
Method Details
-
validateLabel
Determine whether theElementlabel can be legally set. This is typically used as a pre-condition check.- Parameters:
label- the element label- Throws:
IllegalArgumentException- whether the label is legal and if not, a clear reason exception is provided
-
validateProperty
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.- Parameters:
key- the key of the propertyvalue- the value of the property\- Throws:
IllegalArgumentException- whether the key/value pair is legal and if not, a clear reason exception message is provided
-
legalPropertyKeyValueArray
public static void legalPropertyKeyValueArray(Object... propertyKeyValues) throws IllegalArgumentException Determines whether a list of key/values are legal, ensuring that there are an even number of values submitted and that the keys in the list of arguments areStringorTobjects.- Parameters:
propertyKeyValues- a list of key/value pairs- Throws:
IllegalArgumentException- if something in the pairs is illegal
-
getIdValue
Extracts the value of theT.idkey from the list of arguments.- Parameters:
keyValues- a list of key/value pairs- Returns:
- the value associated with
T.id
-
remove
Remove a key from the set of key/value pairs. Assumes that validations have already taken place to assure that key positions contain strings and that there are an even number of elements. If after removal there are no values left, the key value list is returned as empty.- Parameters:
keyToRemove- the key to removekeyValues- the list to remove the accessor from- Returns:
- the key/values without the specified accessor or an empty array if no values remain after removal
-
remove
Removes an accessor from the set of key/value pairs. Assumes that validations have already taken place to assure that key positions contain strings and that there are an even number of elements. If after removal there are no values left, the key value list is returned as empty.- Parameters:
accessor- to removekeyValues- the list to remove the accessor from- Returns:
- the key/values without the specified accessor or an empty array if no values remain after removal
-
upsert
Append a key/value pair to a list of existing key/values. If the key already exists in the keyValues then that value is overwritten with the provided value. -
replaceKey
Replaces one key with a different key.- Parameters:
keyValues- the list of key/values to alteroldKey- the key to replacenewKey- the new key
-
asMap
Converts a set of key values to a Map. Assumes that validations have already taken place to assure that key positions contain strings and that there are an even number of elements. -
asPairs
Convert a set of key values to a list of Pair objects. Assumes that validations have already taken place to assure that key positions contain strings and that there are an even number of elements. -
getKeys
Gets the list of keys from the key values.- Parameters:
keyValues- a list of key/values pairs
-
getLabelValue
Extracts the value of theT.labelkey from the list of arguments.- Parameters:
keyValues- a list of key/value pairs- Returns:
- the value associated with
T.label - Throws:
ClassCastException- if the value of the label is not aStringNullPointerException- if the value for theT.labelkey isnull
-
getLabelsValue
Extracts the value of theT.labelkey from the list of arguments as aSetof labels. Supports both singleStringvalues andCollectionvalues for multi-label vertices.- Parameters:
keyValues- a list of key/value pairs- Returns:
- the labels associated with
T.label, or empty if not present - Since:
- 4.0.0
-
applyLabelsToVertex
Applies label value(s) to a vertex using addLabel semantics. Handles both single String and Collection<String> label values as used in mergeV onMatch maps.- Parameters:
vertex- the vertex to add labels tolabelValue- the label value (String or Collection<String>)- Since:
- 4.0.0
-
attachProperties
Assign key/value pairs as properties to anElement. If the value ofT.idorT.labelis in the set of pairs, then they are ignored.- Parameters:
element- the graph element to assign thepropertyKeyValuespropertyKeyValues- the key/value pairs to assign to theelement- Throws:
ClassCastException- if the value of the key is not aStringIllegalArgumentException- if the value ofelementis null
-
attachProperties
Assign key/value pairs as properties to anVertex. If the value ofT.idorT.labelis in the set of pairs, then they are ignored. TheVertexProperty.Cardinalityof the key is determined from theGraph.Features.VertexFeatures.- Parameters:
vertex- the graph vertex to assign thepropertyKeyValuespropertyKeyValues- the key/value pairs to assign to theelement- Throws:
ClassCastException- if the value of the key is not aStringIllegalArgumentException- if the value ofelementis null
-
attachProperties
public static void attachProperties(Vertex vertex, VertexProperty.Cardinality cardinality, Object... propertyKeyValues) Assign key/value pairs as properties to aVertex. If the value ofT.idorT.labelis in the set of pairs, then they are ignored.- Parameters:
vertex- the vertex to attach the properties tocardinality- the cardinality of the key value pair settingspropertyKeyValues- the key/value pairs to assign to theelement- Throws:
ClassCastException- if the value of the key is not aStringIllegalArgumentException- if the value ofelementis null
-
stageVertexProperty
public static <V> Optional<VertexProperty<V>> stageVertexProperty(Vertex vertex, VertexProperty.Cardinality cardinality, String key, V value, Object... keyValues) This is a helper method for dealing with vertex property cardinality and typically used inVertex.property(VertexProperty.Cardinality, String, Object, Object...). If the cardinality is list, simply returnOptional.empty(). If the cardinality is single, delete all existing properties of the provided key and returnOptional.empty(). If the cardinality is set, find one that has the same key/value and attached the properties to it and return it. Else, if no equal value is found, returnOptional.empty().- Type Parameters:
V- the type of the vertex property value- Parameters:
vertex- the vertex to stage a vertex property forcardinality- the cardinality of the vertex propertykey- the key of the vertex propertyvalue- the value of the vertex propertykeyValues- the properties of vertex property- Returns:
- a vertex property if it has been found in set with equal value
-
getProperties
public static Object[] getProperties(Element element, boolean includeId, boolean includeLabel, Set<String> propertiesToCopy) Retrieve the properties associated with a particular element. The result is a Object[] where odd indices are String keys and even indices are the values.- Parameters:
element- the element to retrieve properties fromincludeId- include Element.ID in the key/value listincludeLabel- include Element.LABEL in the key/value listpropertiesToCopy- the properties to include with an empty list meaning copy all properties- Returns:
- a key/value array of properties where odd indices are String keys and even indices are the values.
-
areEqual
A standard method for determining if twoElementobjects are equal. This method should be used by anyObject.equals(Object)implementation to ensure consistent behavior. This method is used for Vertex, Edge, and VertexProperty. -
areEqual
-
areEqual
-
areEqual
-
areEqual
A standard method for determining if twoVertexPropertyobjects are equal. This method should be used by anyObject.equals(Object)implementation to ensure consistent behavior.- Parameters:
a- the firstVertexPropertyb- the secondVertexProperty- Returns:
- true if equal and false otherwise
-
haveEqualIds
Simply tests if the value returned fromElement.id()areequal(). -
hashCode
If twoElementinstances are equal, then they must have the same hash codes. This methods ensures consistent hashCode values.- Parameters:
element- the element to get the hashCode for- Returns:
- the hash code of the element
-
hashCode
If twoPropertyinstances are equal, then they must have the same hash codes. This methods ensures consistent hashCode values. ForVertexPropertyusehashCode(Element).- Parameters:
property- the property to get the hashCode for- Returns:
- the hash code of the property
-
areEqual
A standard method for determining if twoPropertyobjects are equal. This method should be used by anyObject.equals(Object)implementation to ensure consistent behavior. -
propertyValueMap
-
propertyMap
-
vertexPropertyValueMap
-
vertexPropertyMap
-
keyExists
Checks if a key exists within a list of provided keys. Returnsfalseif the key isnullor if theGraph.Hidden. Returnstrueif noprovidedKeysare supplied.- Parameters:
key- must not benull
-
idExists
-