Skip to content

Loading…

Change Element.getProperty and Element.removeProperty to avoid casting in client code. #369

Merged
merged 2 commits into from

2 participants

@BrynCooke

Changing the signatures to:

public <T> T getProperty(String key);
public <T> T removeProperty(String key);

Means that clients can write code like this:

String test = v.getProperty("testProp");

Implementations of Element don't have to update their code, but probably should in the long run. e.g. The following will cause a compile error if TitanVertex is not updated:

TitanVertex tv = (TitanVertex) v;
String test2 = tv.getProperty("testProp"); //Type mismatch
@okram okram merged commit 19f5e32 into tinkerpop:master

1 check failed

Details default The Travis build failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 55 additions and 55 deletions.
  1. +2 −2 blueprints-core/src/main/java/com/tinkerpop/blueprints/Element.java
  2. +4 −4 blueprints-core/src/main/java/com/tinkerpop/blueprints/impls/tg/TinkerElement.java
  3. +3 −3 blueprints-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/event/EventElement.java
  4. +2 −2 blueprints-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/id/IdElement.java
  5. +2 −2 blueprints-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/partition/PartitionElement.java
  6. +1 −1 blueprints-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/readonly/ReadOnlyElement.java
  7. +2 −2 blueprints-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/wrapped/WrappedElement.java
  8. +5 −5 blueprints-dex-graph/src/main/java/com/tinkerpop/blueprints/impls/dex/DexElement.java
  9. +4 −4 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/Neo4jElement.java
  10. +2 −2 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/batch/Neo4jBatchEdge.java
  11. +2 −2 ...rints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/batch/Neo4jBatchElement.java
  12. +2 −2 ...prints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/batch/Neo4jBatchVertex.java
  13. +6 −6 blueprints-orient-graph/src/main/java/com/tinkerpop/blueprints/impls/orient/OrientElement.java
  14. +4 −4 blueprints-rexster-graph/src/main/java/com/tinkerpop/blueprints/impls/rexster/RexsterElement.java
  15. +4 −4 blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailEdge.java
  16. +10 −10 blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailVertex.java
View
4 blueprints-core/src/main/java/com/tinkerpop/blueprints/Element.java
@@ -21,7 +21,7 @@
* @param key the key of the key/value property
* @return the object value related to the string key
*/
- public Object getProperty(String key);
+ public <T> T getProperty(String key);
/**
* Return all the keys associated with the element.
@@ -46,7 +46,7 @@
* @param key the key of the property to remove from the element
* @return the object value associated with that key prior to removal
*/
- public Object removeProperty(String key);
+ public <T> T removeProperty(String key);
/**
* Remove the element from the graph.
View
8 blueprints-core/src/main/java/com/tinkerpop/blueprints/impls/tg/TinkerElement.java
@@ -32,8 +32,8 @@ protected TinkerElement(final String id, final TinkerGraph graph) {
return new HashSet<String>(this.properties.keySet());
}
- public Object getProperty(final String key) {
- return this.properties.get(key);
+ public <T> T getProperty(final String key) {
+ return (T) this.properties.get(key);
}
public void setProperty(final String key, final Object value) {
@@ -51,13 +51,13 @@ public void setProperty(final String key, final Object value) {
this.graph.edgeKeyIndex.autoUpdate(key, value, oldValue, (TinkerEdge) this);
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
Object oldValue = this.properties.remove(key);
if (this instanceof TinkerVertex)
this.graph.vertexKeyIndex.autoRemove(key, oldValue, (TinkerVertex) this);
else
this.graph.edgeKeyIndex.autoRemove(key, oldValue, (TinkerEdge) this);
- return oldValue;
+ return (T) oldValue;
}
View
6 blueprints-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/event/EventElement.java
@@ -56,7 +56,7 @@ public Object getId() {
/**
* Raises a vertexPropertyRemoved or edgePropertyRemoved event.
*/
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
final Object propertyRemoved = baseElement.removeProperty(key);
if (this instanceof Vertex) {
@@ -65,10 +65,10 @@ public Object removeProperty(final String key) {
this.onEdgePropertyRemoved((Edge) this, key, propertyRemoved);
}
- return propertyRemoved;
+ return (T) propertyRemoved;
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
return this.baseElement.getProperty(key);
}
View
4 blueprints-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/id/IdElement.java
@@ -21,7 +21,7 @@ protected IdElement(final Element baseElement, final IdGraph idGraph) {
this.idGraph = idGraph;
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
if (key.equals(IdGraph.ID)) {
return null;
}
@@ -45,7 +45,7 @@ public void setProperty(final String key, final Object value) {
baseElement.setProperty(key, value);
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
if (key.equals(IdGraph.ID)) {
throw new IllegalArgumentException("Unable to remove value for reserved property " + IdGraph.ID);
}
View
4 ...core/src/main/java/com/tinkerpop/blueprints/util/wrappers/partition/PartitionElement.java
@@ -26,13 +26,13 @@ public void setProperty(final String key, final Object value) {
this.baseElement.setProperty(key, value);
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
if (key.equals(this.graph.getPartitionKey()))
return null;
return this.baseElement.getProperty(key);
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
if (key.equals(this.graph.getPartitionKey()))
return null;
return this.baseElement.removeProperty(key);
View
2 ...s-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/readonly/ReadOnlyElement.java
@@ -33,7 +33,7 @@ public Object removeProperty(final String key) throws UnsupportedOperationExcept
throw new UnsupportedOperationException(ReadOnlyTokens.MUTATE_ERROR_MESSAGE);
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
return this.baseElement.getProperty(key);
}
View
4 ...nts-core/src/main/java/com/tinkerpop/blueprints/util/wrappers/wrapped/WrappedElement.java
@@ -20,11 +20,11 @@ public void setProperty(final String key, final Object value) {
this.baseElement.setProperty(key, value);
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
return this.baseElement.getProperty(key);
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
return this.baseElement.removeProperty(key);
}
View
10 blueprints-dex-graph/src/main/java/com/tinkerpop/blueprints/impls/dex/DexElement.java
@@ -90,13 +90,13 @@ protected DexElement(final DexGraph g, final long oid) {
* @see com.tinkerpop.blueprints.Element#getProperty(java.lang.String)
*/
@Override
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
graph.autoStartTransaction();
int type = getObjectType();
if (key.compareTo(StringFactory.LABEL) == 0) {
com.sparsity.dex.gdb.Type tdata = graph.getRawGraph().getType(type);
- return tdata.getName();
+ return (T) tdata.getName();
}
int attr = graph.getRawGraph().findAttribute(getObjectType(), key);
if (attr == com.sparsity.dex.gdb.Attribute.InvalidAttribute) {
@@ -129,7 +129,7 @@ public Object getProperty(final String key) {
throw new UnsupportedOperationException(DexTokens.TYPE_EXCEPTION_MESSAGE);
}
}
- return result;
+ return (T) result;
}
/*
@@ -252,7 +252,7 @@ public void setProperty(final String key, final Object value) {
* com.tinkerpop.blueprints.Element#removeProperty(java.lang.String)
*/
@Override
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
graph.autoStartTransaction();
try {
@@ -260,7 +260,7 @@ public Object removeProperty(final String key) {
com.sparsity.dex.gdb.Value v = new com.sparsity.dex.gdb.Value();
v.setNull();
setProperty(key, v);
- return ret;
+ return (T) ret;
} catch (RuntimeException e) {
return null;
}
View
8 blueprints-neo4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/Neo4jElement.java
@@ -29,9 +29,9 @@ public Neo4jElement(final Neo4jGraph graph) {
this.graph = graph;
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
if (this.rawElement.hasProperty(key))
- return this.rawElement.getProperty(key);
+ return (T) this.rawElement.getProperty(key);
else
return null;
}
@@ -49,12 +49,12 @@ public void setProperty(final String key, final Object value) {
this.rawElement.setProperty(key, tryConvertCollectionToArray(value));
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
if (!this.rawElement.hasProperty(key))
return null;
else {
this.graph.autoStartTransaction();
- return this.rawElement.removeProperty(key);
+ return (T) this.rawElement.removeProperty(key);
}
}
View
4 ...-neo4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/batch/Neo4jBatchEdge.java
@@ -20,11 +20,11 @@ public Neo4jBatchEdge(final Neo4jBatchGraph graph, final Long id, final String l
this.label = label;
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
final Map<String, Object> properties = this.getPropertyMapClone();
final Object value = properties.remove(key);
this.graph.getRawGraph().setRelationshipProperties(this.id, properties);
- return value;
+ return (T) value;
}
View
4 ...o4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/batch/Neo4jBatchElement.java
@@ -30,8 +30,8 @@ public Object getId() {
return this.getPropertyMap().keySet();
}
- public Object getProperty(final String key) {
- return this.getPropertyMap().get(key);
+ public <T> T getProperty(final String key) {
+ return (T) this.getPropertyMap().get(key);
}
protected Map<String, Object> getPropertyMapClone() {
View
4 ...eo4j-graph/src/main/java/com/tinkerpop/blueprints/impls/neo4j/batch/Neo4jBatchVertex.java
@@ -18,11 +18,11 @@ public Neo4jBatchVertex(final Neo4jBatchGraph graph, final Long id) {
super(graph, id);
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
final Map<String, Object> properties = this.getPropertyMapClone();
final Object value = properties.remove(key);
this.graph.getRawGraph().setNodeProperties(this.id, properties);
- return value;
+ return (T) value;
}
View
12 ...rints-orient-graph/src/main/java/com/tinkerpop/blueprints/impls/orient/OrientElement.java
@@ -44,23 +44,23 @@ public void setProperty(final String key, final Object value) {
this.graph.getRawGraph().save(rawElement);
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
this.graph.autoStartTransaction();
final Object oldValue = this.rawElement.removeField(key);
this.save();
- return oldValue;
+ return (T) oldValue;
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
if (key == null)
return null;
if (key.equals("_class"))
- return rawElement.getSchemaClass().getName();
+ return (T) rawElement.getSchemaClass().getName();
else if (key.equals("_version"))
- return rawElement.getVersion();
+ return (T) new Integer(rawElement.getVersion());
else if (key.equals("_rid"))
- return rawElement.getIdentity().toString();
+ return (T) rawElement.getIdentity().toString();
return this.rawElement.field(key);
}
View
8 ...ts-rexster-graph/src/main/java/com/tinkerpop/blueprints/impls/rexster/RexsterElement.java
@@ -63,7 +63,7 @@ public void remove() {
this.graph.removeEdge((Edge) this);
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
JSONObject rawElement;
if (this instanceof Vertex)
rawElement = RestHelper.getResultObject(this.graph.getGraphURI() + RexsterTokens.SLASH_VERTICES_SLASH + RestHelper.encode(this.getId()) + RexsterTokens.QUESTION + RexsterTokens.REXSTER_SHOW_TYPES_EQUALS_TRUE);
@@ -72,7 +72,7 @@ public Object getProperty(final String key) {
JSONObject typedProperty = rawElement.optJSONObject(key);
if (null != typedProperty)
- return RestHelper.typeCast(typedProperty.optString(RexsterTokens.TYPE), typedProperty.opt(RexsterTokens.VALUE));
+ return (T) RestHelper.typeCast(typedProperty.optString(RexsterTokens.TYPE), typedProperty.opt(RexsterTokens.VALUE));
else
return null;
}
@@ -103,7 +103,7 @@ public int hashCode() {
return this.getId().hashCode();
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
Object object = this.getProperty(key);
@@ -112,7 +112,7 @@ public Object removeProperty(final String key) {
else
RestHelper.delete(this.graph.getGraphURI() + RexsterTokens.SLASH_EDGES_SLASH + RestHelper.encode(this.getId()) + RexsterTokens.QUESTION + RestHelper.encode(key));
- return object;
+ return (T) object;
}
public boolean equals(final Object object) {
View
8 blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailEdge.java
@@ -58,13 +58,13 @@ public boolean hasNamedGraph() {
return keys;
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
if (key.equals(SailTokens.NAMED_GRAPH)) {
Resource resource = this.rawEdge.getContext();
if (null == resource)
return null;
else
- return resource.stringValue();
+ return (T) resource.stringValue();
} else
return null;
}
@@ -84,14 +84,14 @@ public void setProperty(final String key, final Object value) {
}
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
if (key.equals(SailTokens.NAMED_GRAPH)) {
try {
Resource ng = this.rawEdge.getContext();
SailHelper.removeStatement(this.rawEdge, this.graph.getSailConnection().get());
this.rawEdge = new StatementImpl(this.rawEdge.getSubject(), this.rawEdge.getPredicate(), this.rawEdge.getObject());
SailHelper.addStatement(this.rawEdge, this.graph.getSailConnection().get());
- return ng;
+ return (T) ng;
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
View
20 blueprints-sail-graph/src/main/java/com/tinkerpop/blueprints/impls/sail/SailVertex.java
@@ -93,7 +93,7 @@ public void setProperty(final String key, final Object value) {
}
}
- public Object removeProperty(final String key) {
+ public <T> T removeProperty(final String key) {
if (this.rawVertex instanceof Resource) {
throw new RuntimeException(URI_BLANK_NODE_PROPERTIES);
} else {
@@ -103,35 +103,35 @@ public Object removeProperty(final String key) {
this.updateLiteral(oldLiteral, (Literal) this.rawVertex);
}
if (key.equals(SailTokens.DATATYPE)) {
- return oldLiteral.getDatatype().toString();
+ return (T) oldLiteral.getDatatype().toString();
} else if (key.equals(SailTokens.LANGUAGE)) {
- return oldLiteral.getLanguage();
+ return (T) oldLiteral.getLanguage();
}
}
return null;
}
- public Object getProperty(final String key) {
+ public <T> T getProperty(final String key) {
if (key.equals(SailTokens.KIND)) {
if (this.rawVertex instanceof Literal)
- return SailTokens.LITERAL;
+ return (T) SailTokens.LITERAL;
else if (this.rawVertex instanceof URI)
- return SailTokens.URI;
+ return (T) SailTokens.URI;
else
- return SailTokens.BNODE;
+ return (T) SailTokens.BNODE;
}
if (this.rawVertex instanceof Literal) {
final Literal literal = (Literal) rawVertex;
if (key.equals(SailTokens.DATATYPE)) {
if (null != literal.getDatatype())
- return literal.getDatatype().stringValue();
+ return (T) literal.getDatatype().stringValue();
else
return null;
} else if (key.equals(SailTokens.LANGUAGE)) {
- return literal.getLanguage();
+ return (T) literal.getLanguage();
} else if (key.equals(SailTokens.VALUE)) {
- return castLiteral(literal);
+ return (T) castLiteral(literal);
}
}
return null;
Something went wrong with that request. Please try again.