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  import net.sf.collections15.functors.FunctorException;
20  
21  
22  /***
23   * <code>Predicate</code> implementation that decorates an existing
24   * <code>Predicate</code>. The {@link #evaluate} method throws an exception if
25   * the input is <code>null</code>, and otherwise defers to the decorated
26   * <code>Predicate</code>.
27   *
28   * @author Stephen Colebourne
29   * @author Chris Lambrou (port to Java 5.0)
30   * @since Commons Collections15 1.0
31   */
32  public final class NullIsExceptionPredicate <E> extends AbstractPredicateDecorator<E>
33  {
34  
35      static final long serialVersionUID = -113296965728147617L;
36  
37      /***
38       * Creates a new instance that decorates the specified
39       * <code>Predicate</code>.
40       *
41       * @param predicate The decorated <code>Predicate</code> that is delgated to
42       *                  if the <code>null</code> check doesn't throw an
43       *                  exception.
44       *
45       * @return A new instance that decorates the specified <code>Predicate</code>.
46       *
47       * @throws IllegalArgumentException Thrown if the specified <code>Predicate</code>
48       *                                  to decorate is <code>null</code>.
49       */
50      public static <E> NullIsExceptionPredicate<E> getInstance(Predicate<? super E> predicate)
51      {
52          return new NullIsExceptionPredicate<E>(predicate);
53      }
54  
55      /***
56       * Creats a new instance that decorates the specified
57       * <code>Predicate</code>.
58       *
59       * @param predicate The decorated <code>Predicate</code> that is delgated to
60       *                  if the <code>null</code> check doesn't throw an
61       *                  exception.
62       *
63       * @throws IllegalArgumentException Thrown if the specified <code>Predicate</code>
64       *                                  to decorate is <code>null</code>.
65       */
66      protected NullIsExceptionPredicate(Predicate<? super E> predicate)
67      {
68          super(predicate);
69      }
70  
71      /***
72       * Evaluates the <code>Predicate</code>, throwing an exception if the input
73       * object is <code>null</code> or otherwise delegating to the decorated
74       * <code>Predicate</code>.
75       *
76       * @param object The input object to evaluate.
77       *
78       * @return <code>true</code> if the decorated <code>Predicate</code> returns
79       *         <code>true</code>. <code>false</code> if the decorated
80       *         <code>Predicate</code> returns <code>false</code>.
81       *
82       * @throws FunctorException Thrown if the input object is <code>null</code>.
83       */
84      public boolean evaluate(E object)
85      {
86          if (object == null) {
87              throw new FunctorException("Input Object must not be null");
88          }
89          return decoratedPredicate.evaluate(object);
90      }
91  
92  }