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.collections4.functors;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.collections4.Predicate;
22  
23  /**
24   * Predicate implementation that returns true if both the predicates return true.
25   *
26   * @since 3.0
27   */
28  public final class AndPredicate<T> implements PredicateDecorator<T>, Serializable {
29  
30      /** Serial version UID */
31      private static final long serialVersionUID = 4189014213763186912L;
32  
33      /** The array of predicates to call */
34      private final Predicate<? super T> iPredicate1;
35      /** The array of predicates to call */
36      private final Predicate<? super T> iPredicate2;
37  
38      /**
39       * Factory to create the predicate.
40       *
41       * @param <T> the type that the predicate queries
42       * @param predicate1  the first predicate to check, not null
43       * @param predicate2  the second predicate to check, not null
44       * @return the <code>and</code> predicate
45       * @throws NullPointerException if either predicate is null
46       */
47      public static <T> Predicate<T> andPredicate(final Predicate<? super T> predicate1,
48                                                  final Predicate<? super T> predicate2) {
49          if (predicate1 == null || predicate2 == null) {
50              throw new NullPointerException("Predicate must not be null");
51          }
52          return new AndPredicate<>(predicate1, predicate2);
53      }
54  
55      /**
56       * Constructor that performs no validation.
57       * Use <code>andPredicate</code> if you want that.
58       *
59       * @param predicate1  the first predicate to check, not null
60       * @param predicate2  the second predicate to check, not null
61       */
62      public AndPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) {
63          super();
64          iPredicate1 = predicate1;
65          iPredicate2 = predicate2;
66      }
67  
68      /**
69       * Evaluates the predicate returning true if both predicates return true.
70       *
71       * @param object  the input object
72       * @return true if both decorated predicates return true
73       */
74      @Override
75      public boolean evaluate(final T object) {
76         return iPredicate1.evaluate(object) && iPredicate2.evaluate(object);
77      }
78  
79      /**
80       * Gets the two predicates being decorated as an array.
81       *
82       * @return the predicates
83       * @since 3.1
84       */
85      @Override
86      @SuppressWarnings("unchecked")
87      public Predicate<? super T>[] getPredicates() {
88          return new Predicate[] {iPredicate1, iPredicate2};
89      }
90  
91  }