Skip to content

Commit

Permalink
Merge pull request #369 from BrynCooke/master
Browse files Browse the repository at this point in the history
Change Element.getProperty and Element.removeProperty to avoid casting in client code.
  • Loading branch information
okram committed Mar 10, 2013
2 parents 008cec3 + 46b0200 commit 19f5e32
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract interface Element {
* @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.
Expand All @@ -46,7 +46,7 @@ public abstract interface Element {
* @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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public Set<String> getPropertyKeys() {
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) {
Expand All @@ -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;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -129,7 +129,7 @@ public Object getProperty(final String key) {
throw new UnsupportedOperationException(DexTokens.TYPE_EXCEPTION_MESSAGE);
}
}
return result;
return (T) result;
}

/*
Expand Down Expand Up @@ -252,15 +252,15 @@ 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 {
Object ret = getProperty(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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public Set<String> getPropertyKeys() {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public Set<String> getPropertyKeys() {
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;
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down

0 comments on commit 19f5e32

Please sign in to comment.