Coverage Report - org.apache.commons.functor.core.collection.IsEmpty
 
Classes in this File Line Coverage Branch Coverage Complexity
IsEmpty
100%
22/22
100%
16/16
2.2
 
 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.collection;
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.lang.reflect.Array;
 21  
 import java.util.Collection;
 22  
 import java.util.Map;
 23  
 
 24  
 import org.apache.commons.functor.UnaryPredicate;
 25  
 
 26  
 /**
 27  
  * @version $Revision: 1166333 $ $Date: 2011-09-07 21:36:42 +0200 (Wed, 07 Sep 2011) $
 28  
  * @author Rodney Waldhoff
 29  
  */
 30  
 public final class IsEmpty<A> implements UnaryPredicate<A>, Serializable {
 31  
 
 32  
     // class variables
 33  
     // ------------------------------------------------------------------------
 34  
 
 35  
     /**
 36  
      * Basic IsEmpty instance.
 37  
      */
 38  2
     public static final IsEmpty<Object> INSTANCE = new IsEmpty<Object>();
 39  
 
 40  
     /**
 41  
      * serialVersionUID declaration.
 42  
      */
 43  
     private static final long serialVersionUID = 6555097970639642373L;
 44  
 
 45  
     // constructor
 46  
     // ------------------------------------------------------------------------
 47  
     /**
 48  
      * Create a new IsEmpty.
 49  
      */
 50  52
     public IsEmpty() {
 51  52
     }
 52  
 
 53  
     // instance methods
 54  
     // ------------------------------------------------------------------------
 55  
     /**
 56  
      * {@inheritDoc}
 57  
      */
 58  
     public boolean test(A obj) {
 59  58
         if (obj instanceof Collection<?>) {
 60  38
             return testCollection((Collection<?>) obj);
 61  
         }
 62  20
         if (obj instanceof Map<?, ?>) {
 63  4
             return testMap((Map<?, ?>) obj);
 64  
         }
 65  16
         if (obj instanceof String) {
 66  4
             return testString((String) obj);
 67  
         }
 68  12
         if (null != obj && obj.getClass().isArray()) {
 69  8
             return testArray(obj);
 70  
         }
 71  4
         if (null == obj) {
 72  2
             throw new IllegalArgumentException("Argument must not be null");
 73  
         }
 74  2
         throw new IllegalArgumentException("Expected Collection, Map, String or Array, found " + obj.getClass());
 75  
     }
 76  
 
 77  
     /**
 78  
      * {@inheritDoc}
 79  
      */
 80  
     public boolean equals(Object that) {
 81  22
         return that instanceof IsEmpty<?>;
 82  
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     public int hashCode() {
 88  24
         return "IsEmpty".hashCode();
 89  
     }
 90  
 
 91  
     /**
 92  
      * {@inheritDoc}
 93  
      */
 94  
     public String toString() {
 95  16
         return "IsEmpty()";
 96  
     }
 97  
 
 98  
     /**
 99  
      * Test a collection.
 100  
      * @param col to test
 101  
      * @return boolean
 102  
      */
 103  
     private boolean testCollection(Collection<?> col) {
 104  38
         return col.isEmpty();
 105  
     }
 106  
 
 107  
     /**
 108  
      * Test a map.
 109  
      * @param map to test
 110  
      * @return boolean
 111  
      */
 112  
     private boolean testMap(Map<?, ?> map) {
 113  4
         return map.isEmpty();
 114  
     }
 115  
 
 116  
     /**
 117  
      * Test a string.
 118  
      * @param str to test
 119  
      * @return boolean
 120  
      */
 121  
     private boolean testString(String str) {
 122  4
         return 0 == str.length();
 123  
     }
 124  
 
 125  
     /**
 126  
      * Test an array.
 127  
      * @param array to test
 128  
      * @return boolean
 129  
      */
 130  
     private boolean testArray(Object array) {
 131  8
         return 0 == Array.getLength(array);
 132  
     }
 133  
 
 134  
     // static
 135  
     // ------------------------------------------------------------------------
 136  
     /**
 137  
      * Get an IsEmpty instance.
 138  
      * @return IsEmpty
 139  
      */
 140  
     public static <A> IsEmpty<A> instance() {
 141  34
         return new IsEmpty<A>();
 142  
     }
 143  
 
 144  
 }