com.scalar.db.sql.AndPredicateList 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 and-wise list of {@link Predicate} used in WHERE clauses. */
@Immutable
public class AndPredicateList implements PredicateList {
public final ImmutableList predicates;
private AndPredicateList(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 AndPredicateList}
*
- 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 AndPredicateList)) {
return false;
}
AndPredicateList other = (AndPredicateList) 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 AndPredicateList}.
*
* @param predicate a predicate for a {@code AndPredicateList}.
* @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 AndPredicateList}.
*
* @param predicates a list of predicates for a {@code AndPredicateList}.
* @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 AndPredicateList}.
*
* @param predicate a predicate for a {@code AndPredicateList}.
* @return a builder object
*/
public Builder and(Predicate predicate) {
predicateBuilder.add(predicate);
return this;
}
public AndPredicateList build() {
return new AndPredicateList(predicateBuilder.build());
}
}
}