com.scalar.db.sql.OrPredicateList Maven / Gradle / Ivy
package com.scalar.db.sql;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Objects;
import javax.annotation.concurrent.Immutable;
/** An or-wise list of {@link Predicate} used in WHERE clauses. */
@Immutable
public class OrPredicateList implements PredicateList {
public final ImmutableList predicates;
private OrPredicateList(ImmutableList predicates) {
this.predicates = predicates;
}
/**
* Returns the list of {@code Predicate}.
*
* @return list of {@code Predicate}
*/
@Override
public List getPredicates() {
return predicates;
}
/**
* Indicates whether some other object is "equal to" this object. The other object is considered
* equal if:
*
*
* - it is also an {@code OrPredicateList}
*
- both instances have the same list of {@code Predicate}
*
*
* @param o an object to be tested for equality
* @return {@code true} if the other object is "equal to" this object otherwise {@code false}
*/
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof OrPredicateList)) {
return false;
}
OrPredicateList other = (OrPredicateList) o;
return predicates.equals(other.predicates);
}
@Override
public int hashCode() {
return Objects.hash(predicates);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("predicates", predicates).toString();
}
/**
* Adds a predicate for a {@code OrPredicateList}.
*
* @param predicate a predicate for a {@code OrPredicateList}.
* @return a builder object
*/
public static Builder predicate(Predicate predicate) {
ImmutableList.Builder builder = new ImmutableList.Builder<>();
return new Builder(builder.add(predicate));
}
/**
* Adds a list of predicates for a {@code OrPredicateList}.
*
* @param predicates a list of predicates for a {@code OrPredicateList}.
* @return a builder object
*/
public static Builder predicates(List predicates) {
ImmutableList.Builder builder = new ImmutableList.Builder<>();
return new Builder(builder.addAll(predicates));
}
public static class Builder {
private final ImmutableList.Builder predicateBuilder;
private Builder(ImmutableList.Builder predicateBuilder) {
this.predicateBuilder = predicateBuilder;
}
/**
* Adds a predicate for a {@code OrPredicateList}.
*
* @param predicate a predicate for a {@code OrPredicateList}.
* @return a builder object
*/
public Builder or(Predicate predicate) {
predicateBuilder.add(predicate);
return this;
}
public OrPredicateList build() {
return new OrPredicateList(predicateBuilder.build());
}
}
}