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.functor.core;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.functor.BinaryPredicate;
22  import org.apache.commons.functor.UnaryPredicate;
23  import org.apache.commons.functor.adapter.IgnoreLeftPredicate;
24  import org.apache.commons.functor.adapter.IgnoreRightPredicate;
25  
26  /**
27   * {@link #test Tests}
28   * <code>false</code> iff its argument
29   * is <code>null</code>.
30   *
31   * @param <T> the argument type.
32   * @version $Revision: 1345136 $ $Date: 2012-06-01 09:47:06 -0300 (Fri, 01 Jun 2012) $
33   */
34  public final class IsNotNull<T> implements UnaryPredicate<T>, Serializable {
35  
36      // static attributes
37      // ------------------------------------------------------------------------
38      /**
39       * Basic IsNotNull instance.
40       */
41      public static final IsNotNull<Object> INSTANCE = IsNotNull.<Object>instance();
42  
43      /**
44       * Left-handed BinaryPredicate.
45       */
46      public static final BinaryPredicate<Object, Object> LEFT = IsNotNull.<Object>left();
47  
48      /**
49       * Right-handed BinaryPredicate.
50       */
51      public static final BinaryPredicate<Object, Object> RIGHT = IsNotNull.<Object>right();
52  
53      /**
54       * serialVersionUID declaration.
55       */
56      private static final long serialVersionUID = -6856387958371590330L;
57  
58      // constructor
59      // ------------------------------------------------------------------------
60      /**
61       * Create a new IsNotNull.
62       */
63      public IsNotNull() {
64      }
65  
66      // predicate interface
67      // ------------------------------------------------------------------------
68      /**
69       * {@inheritDoc}
70       */
71      public boolean test(Object obj) {
72          return (null != obj);
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public boolean equals(Object that) {
80          return that instanceof IsNotNull<?>;
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @Override
87      public int hashCode() {
88          return "IsNotNull".hashCode();
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String toString() {
96          return "IsNotNull";
97      }
98  
99      // static methods
100     // ------------------------------------------------------------------------
101     /**
102      * Get an IsNotNull instance.
103      * @param <T> the predicate argument type.
104      * @return IsNotNull
105      */
106     public static <T> IsNotNull<T> instance() {
107         return new IsNotNull<T>();
108     }
109 
110     /**
111      * Get a BinaryPredicate that matches if the left argument is not null.
112      * @param <A> the left {@code BinaryPredicate} argument type.
113      * @return BinaryPredicate<A, Object>
114      */
115     public static <A> BinaryPredicate<A, Object> left() {
116         return IgnoreRightPredicate.adapt(new IsNotNull<A>());
117     }
118 
119     /**
120      * Get a BinaryPredicate that matches if the right argument is null.
121      * @param <A> the right {@code BinaryPredicate} argument type.
122      * @return BinaryPredicate<Object, A>
123      */
124     public static <A> BinaryPredicate<Object, A> right() {
125         return IgnoreLeftPredicate.adapt(new IsNotNull<A>());
126     }
127 
128 }