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  /***
21   * <code>Predicate</code> implementation that decorates an existing
22   * <code>Predicate</code>, returning <code>true</code> if the input is
23   * <code>null</code>, and otherwise delegating to the decorated
24   * <code>Predicate</code>.
25   *
26   * @author Stephen Colebourne
27   * @author Chris Lambrou (port to Java 5.0)
28   * @since Collections15 1.0
29   */
30  public final class NullIsTruePredicate <E> extends AbstractPredicateDecorator<E>
31  {
32  
33      static final long serialVersionUID = -1618729285165144040L;
34  
35      /***
36       * Returns an instance whose {@link #evaluate} method evaluates to
37       * <code>true</code> if the input is <code>null</code>, and otherwise
38       * delegates to the decorated <code>Predicate</code>.
39       *
40       * @param predicate The decorated <code>Predicate</code>.
41       *
42       * @return A <code>Predicate</code> that evaluates to <code>true</code> for
43       *         a <code>null</code> input object, or delegates to the specified
44       *         <code>Predicate</code> for non-<code>null</code> input.
45       *
46       * @throws IllegalArgumentException Thrown if the <code>Predicate</code> is
47       *                                  <code>null</code>.
48       */
49      public static <E> NullIsTruePredicate<E> getInstance(Predicate<? super E> predicate)
50      {
51          return new NullIsTruePredicate<E>(predicate);
52      }
53  
54      /***
55       * Creates a new instance whose {@link #evaluate} method evaluates to
56       * <code>true</code> if the input is <code>null</code>, and otherwise
57       * delegates to the decorated <code>Predicate</code>.
58       *
59       * @param predicate The decorated <code>Predicate</code>.
60       *
61       * @throws IllegalArgumentException Thrown if the <code>Predicate</code> is
62       *                                  <code>null</code>.
63       */
64      protected NullIsTruePredicate(Predicate<? super E> predicate)
65      {
66          super(predicate);
67      }
68  
69      /***
70       * Evaluates the <code>Predicate</code>, returning <code>true</code> if the
71       * input object is <code>null</code>, or delegating to the decorated
72       * <code>Predicate</code> if the input is non-<code>null</code>.
73       *
74       * @param object The input object to evaluate.
75       *
76       * @return <code>true</code> if the input if <code>null</code>, or the
77       *         result of the decorated <code>Predicate</code> if the input is
78       *         non-<code>null</code>.
79       */
80      public boolean evaluate(E object)
81      {
82          if (object == null) {
83              return true;
84          }
85          return decoratedPredicate.evaluate(object);
86      }
87  
88  }