
com.tinkerpop.gremlin.structure.strategy.StrategyWrappedElement Maven / Gradle / Ivy
package com.tinkerpop.gremlin.structure.strategy;
import com.tinkerpop.gremlin.process.graph.GraphTraversal;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.Property;
import com.tinkerpop.gremlin.structure.util.ElementHelper;
import org.javatuples.Pair;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;
/**
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
public abstract class StrategyWrappedElement implements Element, StrategyWrapped {
protected final StrategyWrappedGraph strategyWrappedGraph;
private final Element baseElement;
private final Strategy.Context elementStrategyContext;
protected StrategyWrappedElement(final Element baseElement, final StrategyWrappedGraph strategyWrappedGraph) {
if (baseElement instanceof StrategyWrapped) throw new IllegalArgumentException(
String.format("The element %s is already StrategyWrapped and must be a base Element", baseElement));
this.strategyWrappedGraph = strategyWrappedGraph;
this.baseElement = baseElement;
this.elementStrategyContext = new Strategy.Context<>(strategyWrappedGraph.getBaseGraph(), this);
}
public Element getBaseElement() {
return this.baseElement;
}
@Override
public V value(final String key) throws NoSuchElementException {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementValue(elementStrategyContext),
this.baseElement::value).apply(key);
}
@Override
public void properties(final Object... keyValues) {
this.strategyWrappedGraph.strategy().compose(
s -> s.getElementPropertiesSetter(elementStrategyContext),
this.baseElement::properties).accept(keyValues);
}
@Override
public Property property(final String key) {
return new StrategyWrappedProperty<>(this.strategyWrappedGraph.strategy().compose(
s -> s.getElementGetProperty(elementStrategyContext),
this.baseElement::property).apply(key), this.strategyWrappedGraph);
}
@Override
public Map properties() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementPropertiesGetter(elementStrategyContext),
() -> this.baseElement.properties()).get().entrySet().stream()
.map(e -> Pair.with(e.getKey(), new StrategyWrappedProperty(e.getValue(), strategyWrappedGraph)))
.collect(Collectors.toMap(p -> p.getValue0(), p -> p.getValue1()));
}
@Override
public Map hiddens() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementHiddens(elementStrategyContext),
this.baseElement::hiddens).get().entrySet().stream()
.map(e -> Pair.with(e.getKey(), new StrategyWrappedProperty(e.getValue(), strategyWrappedGraph)))
.collect(Collectors.toMap(p -> p.getValue0(), p -> p.getValue1()));
}
@Override
public Set keys() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementKeys(elementStrategyContext),
this.baseElement::keys).get();
}
@Override
public Set hiddenKeys() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementHiddenKeys(elementStrategyContext),
this.baseElement::hiddenKeys).get();
}
@Override
public Map values() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementValues(elementStrategyContext),
this.baseElement::values).get();
}
@Override
public Map hiddenValues() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementHiddenValues(elementStrategyContext),
this.baseElement::hiddenValues).get();
}
@Override
public String label() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementLabel(elementStrategyContext),
this.baseElement::label).get();
}
@Override
public Object id() {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementId(elementStrategyContext),
this.baseElement::id).get();
}
@Override
public Property property(final String key, final V value) {
return this.strategyWrappedGraph.strategy().compose(
s -> s.getElementProperty(elementStrategyContext),
this.baseElement::property).apply(key, value);
}
@Override
public void remove() {
this.strategyWrappedGraph.strategy().compose(
s -> s.getRemoveElementStrategy(elementStrategyContext),
() -> {
this.baseElement.remove();
return null;
}).get();
}
@Override
public String toString() {
return baseElement.toString();
}
@Override
public int hashCode() {
return this.id().hashCode();
}
@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override
public boolean equals(final Object object) {
return ElementHelper.areEqual(this, object);
}
protected GraphTraversal applyStrategy(final GraphTraversal traversal) {
traversal.strategies().register(new StrategyWrappedTraversalStrategy(this.strategyWrappedGraph));
this.strategyWrappedGraph.strategy().getGraphStrategy().ifPresent(s -> s.applyStrategyToTraversal(traversal));
return traversal;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy