AndCondition.java
package fr.sii.ogham.core.condition;
import java.util.List;
import fr.sii.ogham.core.util.StringUtils;
/**
* Condition that provides a logical AND operation on manipulated conditions.
*
* @author Aurélien Baudet
*
* @param <T>
* the type of the object to test
*/
public class AndCondition<T> extends CompositeCondition<T> {
/**
* Initializes the {@code and} operator with none, one or several
* sub-conditions.
*
* <pre>
* Condition<String> condition1 = ...
* Condition<String> condition2 = ...
* AndCondition<String> myCondition = new AndCondition<>(condition1, condition2);
* </pre>
*
* Has the same effect as:
*
* <pre>
* Condition<String> condition1 = ...
* Condition<String> condition2 = ...
* AndCondition<String> myCondition = new AndCondition<>();
* myCondition.and(condition1);
* myCondition.and(condition2);
* </pre>
*
* @param conditions
* the conditions to register (order is important)
*/
@SafeVarargs
public AndCondition(Condition<T>... conditions) {
super(conditions);
}
/**
* Initializes the {@code and} operator with none, one or several
* sub-conditions. The list must not be null.
*
* @param conditions
* the conditions to register (order is important)
*/
public AndCondition(List<Condition<T>> conditions) {
super(conditions);
}
@Override
public boolean accept(T obj) {
for (Condition<T> condition : getConditions()) {
// if the condition rejects the object => stop now
if (!condition.accept(obj)) {
return false;
}
}
// none condition has rejected the object => it is accepted
return true;
}
/**
* Adds a condition to the current condition. For example:
*
* <pre>
* AndCondition<String> myCondition = new AndCondition<>();
* myCondition.and(new FixedCondition<>(true));
* myCondition.apply("foo"); // will always return true
*
* myCondition.and(new FixedCondition<>(false));
* myCondition.apply("foo"); // will always return false
* </pre>
*
* The returned instance is the same as {@code myCondition} so you can also
* write this:
*
* <pre>
* AndCondition<String> myCondition = new AndCondition<>();
* myCondition = myCondition.and(new FixedCondition<>(true));
* myCondition.apply("foo"); // will always return true
*
* myCondition = myCondition.and(new FixedCondition<>(false));
* myCondition.apply("foo"); // will always return false
* </pre>
*
*
* @param condition
* the condition to add
* @return this instance for fluent chaining
*/
public AndCondition<T> and(Condition<T> condition) {
addCondition(condition);
return this;
}
/**
* Adds several conditions at once to the current condition. This can be
* useful when you register each sub-condition into a list:
*
* <pre>
* List<Condition<String>> conditions = new ArrayList<>();
* conditions.add(...);
* conditions.add(...);
* AndCondition<String> myCondition = new AndCondition<>();
* myCondition.and(conditions);
* </pre>
*
* @param conditions
* the list of conditions to register
* @return this instance for fluent chaining
*/
public AndCondition<T> and(List<Condition<T>> conditions) {
addConditions(conditions);
return this;
}
@Override
public String toString() {
return "{" + StringUtils.join(conditions, "} and {") + "}";
}
}