
com.tinkerpop.gremlin.structure.util.HasContainer Maven / Gradle / Ivy
package com.tinkerpop.gremlin.structure.util;
import com.tinkerpop.gremlin.structure.Contains;
import com.tinkerpop.gremlin.structure.Element;
import com.tinkerpop.gremlin.structure.Property;
import java.io.Serializable;
import java.util.List;
import java.util.function.BiPredicate;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
public class HasContainer implements Serializable {
public String key;
public BiPredicate predicate;
public Object value;
public HasContainer(final String key, final BiPredicate predicate, final Object value) {
this.key = key;
this.predicate = predicate;
this.value = value;
if (null == this.value && !(this.predicate instanceof Contains)) {
throw new IllegalArgumentException("For determining the existence of a property, use the Contains predicate");
}
}
public HasContainer(final String key, final Contains contains) {
this(key, contains, null);
}
public boolean test(final Element element) {
if (null != this.value) {
if (this.key.equals(Element.ID))
return this.predicate.test(element.id(), this.value);
else if (this.key.equals(Element.LABEL))
return this.predicate.test(element.label(), this.value);
else {
final Property property = element.property(this.key);
return property.isPresent() && this.predicate.test(property.value(), this.value);
}
} else {
return Contains.IN.equals(this.predicate) ?
element.property(this.key).isPresent() :
!element.property(this.key).isPresent();
}
}
public static boolean testAll(final Element element, final List hasContainers) {
if (hasContainers.size() == 0)
return true;
else {
for (final HasContainer hasContainer : hasContainers) {
if (!hasContainer.test(element))
return false;
}
return true;
}
}
public String toString() {
return this.value == null ?
(this.predicate == Contains.IN ?
"[" + this.key + "]" :
"[!" + this.key + "]") :
"[" + this.key + "," + this.predicate + "," + this.value + "]";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy