CompositeCondition.java

  1. package fr.sii.ogham.core.condition;

  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;

  5. import fr.sii.ogham.core.util.EqualsBuilder;
  6. import fr.sii.ogham.core.util.HashCodeBuilder;

  7. /**
  8.  * Base class for operators that handle several sub-conditions like AND operator
  9.  * and OR operator.
  10.  *
  11.  * @author AurĂ©lien Baudet
  12.  *
  13.  * @param <T>
  14.  *            the type of the object to test
  15.  */
  16. public abstract class CompositeCondition<T> implements Condition<T> {
  17.     protected final List<Condition<T>> conditions;

  18.     protected CompositeCondition(List<Condition<T>> conditions) {
  19.         super();
  20.         this.conditions = conditions;
  21.     }

  22.     @SafeVarargs
  23.     protected CompositeCondition(Condition<T>... conditions) {
  24.         this(new ArrayList<>(Arrays.asList(conditions)));
  25.     }

  26.     protected List<Condition<T>> getConditions() {
  27.         return conditions;
  28.     }

  29.     protected CompositeCondition<T> addCondition(Condition<T> condition) {
  30.         conditions.add(condition);
  31.         return this;
  32.     }

  33.     protected CompositeCondition<T> addConditions(List<Condition<T>> conditions) {
  34.         this.conditions.addAll(conditions);
  35.         return this;
  36.     }

  37.     @Override
  38.     public boolean equals(Object obj) {
  39.         return new EqualsBuilder(this, obj).appendFields("conditions").isEqual();
  40.     }

  41.     @Override
  42.     public int hashCode() {
  43.         return new HashCodeBuilder().append(conditions).hashCode();
  44.     }

  45.     @Override
  46.     public String toString() {
  47.         return conditions.toString();
  48.     }

  49. }