diff options
Diffstat (limited to 'predicate-search-core/src/main/java/com/yahoo/document/predicate/Disjunction.java')
-rw-r--r-- | predicate-search-core/src/main/java/com/yahoo/document/predicate/Disjunction.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/predicate-search-core/src/main/java/com/yahoo/document/predicate/Disjunction.java b/predicate-search-core/src/main/java/com/yahoo/document/predicate/Disjunction.java new file mode 100644 index 00000000000..c5fa6ba1f70 --- /dev/null +++ b/predicate-search-core/src/main/java/com/yahoo/document/predicate/Disjunction.java @@ -0,0 +1,93 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.document.predicate; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +/** + * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen Hult</a> + */ +public class Disjunction extends PredicateOperator { + + private List<Predicate> operands; + + public Disjunction(Predicate... operands) { + this(Arrays.asList(operands)); + } + + public Disjunction(List<? extends Predicate> operands) { + this.operands = new ArrayList<>(operands); + } + + public Disjunction addOperand(Predicate operand) { + operands.add(operand); + return this; + } + + public Disjunction addOperands(Collection<? extends Predicate> operands) { + this.operands.addAll(operands); + return this; + } + + public Disjunction setOperands(Collection<? extends Predicate> operands) { + this.operands.clear(); + this.operands.addAll(operands); + return this; + } + + @Override + public List<Predicate> getOperands() { + return operands; + } + + @Override + public Disjunction clone() throws CloneNotSupportedException { + Disjunction obj = (Disjunction)super.clone(); + obj.operands = new ArrayList<>(operands.size()); + for (Predicate operand : operands) { + obj.operands.add(operand.clone()); + } + return obj; + } + + @Override + public int hashCode() { + return operands.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof Disjunction)) { + return false; + } + Disjunction rhs = (Disjunction)obj; + if (!operands.equals(rhs.operands)) { + return false; + } + return true; + } + + @Override + protected void appendTo(StringBuilder out) { + for (Iterator<Predicate> it = operands.iterator(); it.hasNext(); ) { + Predicate operand = it.next(); + if (operand instanceof Conjunction) { + out.append('('); + operand.appendTo(out); + out.append(')'); + } else { + operand.appendTo(out); + } + if (it.hasNext()) { + out.append(" or "); + } + } + } + +} |