Coverage Report - org.apache.commons.functor.core.composite.UnaryNot
 
Classes in this File Line Coverage Branch Coverage Complexity
UnaryNot
92%
13/14
72%
16/22
2.143
 
 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.composite;
 18  
 
 19  
 import java.io.Serializable;
 20  
 
 21  
 import org.apache.commons.functor.UnaryPredicate;
 22  
 
 23  
 /**
 24  
  * {@link #test Tests} to the logical inverse
 25  
  * of some other predicate.
 26  
  * <p>
 27  
  * Note that although this class implements
 28  
  * {@link Serializable}, a given instance will
 29  
  * only be truly <code>Serializable</code> if the
 30  
  * underlying functor is.  Attempts to serialize
 31  
  * an instance whose delegate is not
 32  
  * <code>Serializable</code> will result in an exception.
 33  
  * </p>
 34  
  * @version $Revision: 1156320 $ $Date: 2011-08-10 21:14:50 +0200 (Wed, 10 Aug 2011) $
 35  
  * @author Rodney Waldhoff
 36  
  */
 37  
 public final class UnaryNot<A> implements UnaryPredicate<A>, Serializable {
 38  
     /**
 39  
      * serialVersionUID declaration.
 40  
      */
 41  
     private static final long serialVersionUID = -97785102566566058L;
 42  
     // attributes
 43  
     // ------------------------------------------------------------------------
 44  
     private final UnaryPredicate<? super A> predicate;
 45  
 
 46  
     // constructor
 47  
     // ------------------------------------------------------------------------
 48  
     /**
 49  
      * Create a new UnaryNot.
 50  
      * @param predicate UnaryPredicate to negate
 51  
      */
 52  38
     public UnaryNot(UnaryPredicate<? super A> predicate) {
 53  38
         if (predicate == null) {
 54  0
             throw new IllegalArgumentException("UnaryPredicate argument was null");
 55  
         }
 56  38
         this.predicate = predicate;
 57  38
     }
 58  
 
 59  
     // predicate interface
 60  
     // ------------------------------------------------------------------------
 61  
     /**
 62  
      * {@inheritDoc}
 63  
      */
 64  
     public boolean test(A obj) {
 65  150
         return !(predicate.test(obj));
 66  
     }
 67  
 
 68  
     /**
 69  
      * {@inheritDoc}
 70  
      */
 71  
     public boolean equals(Object that) {
 72  26
         return that == this || (that instanceof UnaryNot<?> && equals((UnaryNot<?>) that));
 73  
     }
 74  
 
 75  
     /**
 76  
      * Learn whether another UnaryNot is equal to this.
 77  
      * @param that UnaryNot to test
 78  
      * @return boolean
 79  
      */
 80  
     public boolean equals(UnaryNot<?> that) {
 81  18
         return null != that && (null == predicate ? null == that.predicate : predicate.equals(that.predicate));
 82  
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     public int hashCode() {
 88  28
         int hash = "UnaryNot".hashCode();
 89  28
         if (null != predicate) {
 90  28
             hash ^= predicate.hashCode();
 91  
         }
 92  28
         return hash;
 93  
     }
 94  
 
 95  
     /**
 96  
      * {@inheritDoc}
 97  
      */
 98  
     public String toString() {
 99  20
         return "UnaryNot<" + predicate + ">";
 100  
     }
 101  
 
 102  
     // static
 103  
     // ------------------------------------------------------------------------
 104  
     /**
 105  
      * Invert a UnaryPredicate.
 106  
      * @param pred UnaryPredicate to invert
 107  
      * @return UnaryPredicate<A
 108  
      */
 109  
     public static <A> UnaryPredicate<A> not(UnaryPredicate<? super A> pred) {
 110  20
         return null == pred ? null : new UnaryNot<A>(pred);
 111  
     }
 112  
 
 113  
 }