View Javadoc

1   /*
2    *  Copyright 2001-2004 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package net.sf.collections15.functors.transformer;
17  
18  import net.sf.collections15.Predicate;
19  import net.sf.collections15.PredicateDecorator;
20  import net.sf.collections15.Transformer;
21  
22  import java.io.Serializable;
23  
24  
25  /***
26   * <code>Transformer</code> implementation whose {@link #transform} method
27   * invokes a <code>Predicate</code> using the input object and then returns the
28   * resulting <code>boolean</code> as the output.
29   *
30   * @author Stephen Colebourne
31   * @author Chris Lambrou (port to Java 5.0)
32   * @since Collections15 1.0
33   */
34  public class PredicateTransformer <D, E extends D> implements Transformer<E, Boolean>, PredicateDecorator<D>, Serializable
35  {
36  
37      static final long serialVersionUID = 9019158126476178688L;
38  
39      /***
40       * The <code>Predicate</code> used to implement the transform.
41       */
42      private Predicate<D> predicate;
43  
44      /***
45       * Creates a new instance that uses the specified <code>Predicate</code> to
46       * implement the transform.
47       *
48       * @param predicate The <code>Predicate</code> used to implement the
49       *                  transform.
50       *
51       * @return An instance that uses the specified <code>Predicate</code> to
52       *         implement the transform.
53       *
54       * @throws IllegalArgumentException Thrown if the specified <code>Predicate</code>
55       *                                  is <code>null</code>.
56       */
57      public static <D, E extends D> PredicateTransformer<D, E> getInstance(Predicate<D> predicate)
58      {
59          return new PredicateTransformer<D, E>(predicate);
60      }
61  
62      /***
63       * Creates a new instance that uses the specified <code>Predicate</code> to
64       * implement the transform.
65       *
66       * @param predicate The <code>Predicate</code> used to implement the
67       *                  transform.
68       *
69       * @throws IllegalArgumentException Thrown if the specified <code>Predicate</code>
70       *                                  is <code>null</code>.
71       */
72      public PredicateTransformer(Predicate<D> predicate)
73      {
74          if (predicate == null) {
75              throw new IllegalArgumentException("Predicate must not be null");
76          }
77          this.predicate = predicate;
78      }
79  
80      /***
81       * Transforms the input to result by evaluating the input using the wrapped
82       * <code>Predicate</code>, and returning the resulting <code>boolean</code>
83       * as output.
84       *
85       * @param input The input object to transform.
86       *
87       * @return The result of the <code>evaluate</code> method of the wrapped
88       *         <code>Predicate</code>.
89       */
90      public Boolean transform(E input)
91      {
92          return predicate.evaluate(input);
93      }
94  
95      /***
96       * Returns the <code>Predicate</code> decorated by this
97       * <code>Predicate</code>.
98       *
99       * @return The decorated <code>Predicate</code>.
100      */
101     public Predicate<D> getDecoratedPredicate()
102     {
103         return predicate;
104     }
105 
106 }