package com.dooapp.gaedo.blueprints.queries;
import java.util.Iterator;
import com.dooapp.gaedo.blueprints.GraphUtils;
import com.dooapp.gaedo.blueprints.Properties;
import com.dooapp.gaedo.properties.Property;
import com.tinkerpop.blueprints.pgm.Edge;
import com.tinkerpop.blueprints.pgm.Vertex;
/**
* Base class for all simple tests (like contains, greater than, ...)
* @author ndx
*
* @param <ValueType>
*/
public abstract class MonovaluedValuedVertexTest<ValueType extends Object> extends TargettedVertexTest implements VertexTest {
/**
* Expected value
*/
protected final ValueType expected;
public MonovaluedValuedVertexTest(Iterable<Property> p, ValueType value) {
super(p);
this.expected = value;
}
/**
* To match node
* @param examined
* @return
* @see com.dooapp.gaedo.blueprints.queries.VertexTest#matches(com.tinkerpop.blueprints.pgm.Vertex)
*/
@Override
public boolean matches(Vertex examined) {
// Navigates to the first target edge and perform etest when reached
Vertex currentVertex = examined;
Property finalProperty = null;
for(Property currentProperty : path) {
Iterator<Edge> edges = currentVertex.getOutEdges(GraphUtils.getEdgeNameFor(currentProperty)).iterator();
if(edges.hasNext()) {
currentVertex = edges.next().getInVertex();
} else {
return false;
}
finalProperty = currentProperty;
}
// TODO add automatic loading of value
Object value = currentVertex.getProperty(Properties.value.name());
return matches((ValueType) finalProperty.fromString(value.toString()));
}
protected abstract boolean matches(ValueType effective);
}