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.predicate;
17  
18  import net.sf.collections15.Predicate;
19  
20  import java.io.Serializable;
21  
22  
23  /***
24   * <code>Predicate</code> implementation that evaluates to <code>true</code> if
25   * the input is an instanceof the type stored in this <code>Predicate</code>.
26   *
27   * @author Stephen Colebourne
28   * @author Chris Lambrou (port to Java 5.0)
29   * @since Commons Collections15 1.0
30   */
31  public final class InstanceofPredicate <E> implements Predicate<E>, Serializable
32  {
33  
34      static final long serialVersionUID = -3791196046315427224L;
35  
36      /***
37       * The type to compare input objects to.
38       */
39      private final Class<? extends E> type;
40  
41      /***
42       * Creates a new instance that evaluates input object to determine whether
43       * or not they are instances of the specified type.
44       *
45       * @param type The type to compare input objects against.
46       *
47       * @return A <code>Predicate</code> instance that evaluates to
48       *         <code>true</code> if the input object is an instance of the
49       *         specified type.
50       *
51       * @throws IllegalArgumentException Thrown if the specified type is
52       *                                  <code>null</code>.
53       */
54      public static <T> InstanceofPredicate<T> getInstance(Class<? extends T> type)
55      {
56          return new InstanceofPredicate<T>(type);
57      }
58  
59      /***
60       * Creates a new instance that evaluates input object to determine whether
61       * or not they are instances of the specified type.
62       *
63       * @param type The type to compare input objects against.
64       *
65       * @throws IllegalArgumentException Thrown if the specified type is
66       *                                  <code>null</code>.
67       */
68      protected InstanceofPredicate(Class<? extends E> type)
69      {
70          if (type == null) {
71              throw new IllegalArgumentException("null type specified");
72          }
73          this.type = type;
74      }
75  
76      /***
77       * Evaluates the <code>Predicate</code>, returning <code>true</code> if the
78       * input object is an instance of the stored type.
79       *
80       * @param object The input object to evaluate.
81       *
82       * @return <code>true</code> if the input is an instance of the stored
83       *         type.
84       */
85      public boolean evaluate(E object)
86      {
87          return (type.isInstance(object));
88      }
89  
90      /***
91       * Gets the type against which input objects are evaluated.
92       *
93       * @return The type against which input objects are evaluated.
94       *
95       * @since Collections15 1.0
96       */
97      public Class<? extends E> getType()
98      {
99          return type;
100     }
101 
102 }