View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.functor.core.composite;
18  
19  import org.apache.commons.functor.Predicate;
20  
21  /**
22   * {@link #test Tests} <code>true</code> iff
23   * none of its children test <code>false</code>.
24   * Note that by this definition, the "and" of
25   * an empty collection of predicates tests <code>true</code>.
26   * <p>
27   * Note that although this class implements
28   * {@link java.io.Serializable Serializable}, a given instance will
29   * only be truly <code>Serializable</code> if all the
30   * underlying functors are.  Attempts to serialize
31   * an instance whose delegates are not all
32   * <code>Serializable</code> will result in an exception.
33   * </p>
34   * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
35   * @author Rodney Waldhoff
36   */
37  public final class And extends BasePredicateList {
38  
39      // constructor
40      // ------------------------------------------------------------------------
41  
42      /**
43       * serialVersionUID declaration.
44       */
45      private static final long serialVersionUID = -6053343095016685571L;
46  
47      /**
48       * Create a new And.
49       */
50      public And() {
51          super();
52      }
53  
54      /**
55       * Create a new And instance.
56       *
57       * @param predicates
58       */
59      public And(Iterable<Predicate> predicates) {
60          super(predicates);
61      }
62  
63      /**
64       * Create a new And instance.
65       *
66       * @param predicates
67       */
68      public And(Predicate... predicates) {
69          super(predicates);
70      }
71  
72      // modifiers
73      // ------------------------------------------------------------------------
74      /**
75       * Add a Predicate.
76       * @param p Predicate to add
77       * @return this
78       */
79      public And and(Predicate p) {
80          super.addPredicate(p);
81          return this;
82      }
83  
84      // predicate interface
85      // ------------------------------------------------------------------------
86      /**
87       * {@inheritDoc}
88       */
89      public boolean test() {
90          for (Predicate p : getPredicateList()) {
91              if (!p.test()) {
92                  return false;
93              }
94          }
95          return true;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     public boolean equals(Object that) {
102         return that == this || (that instanceof And && equals((And) that));
103     }
104 
105     /**
106      * Learn whether a given And is equal to this.
107      * @param that the And to test
108      * @return boolean
109      */
110     public boolean equals(And that) {
111         return getPredicateListEquals(that);
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     public int hashCode() {
118         return "And".hashCode() ^ getPredicateListHashCode();
119     }
120 
121     /**
122      * {@inheritDoc}
123      */
124     public String toString() {
125         return "And<" + getPredicateListToString() + ">";
126     }
127 
128 }