View Javadoc
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.lang.reflect.Array;
20  import java.util.Collection;
21  
22  import org.apache.commons.functor.Function;
23  import org.apache.commons.lang3.Validate;
24  
25  /**
26   * Returns the size of the specified Collection, or the length
27   * of the specified array or String.
28   *
29   * @param <A> the function argument type.
30   * @version $Revision: 1537901 $ $Date: 2013-11-01 12:30:19 +0100 (Fr, 01 Nov 2013) $
31   */
32  public final class Size<A> implements Function<A, Integer> {
33  
34      /**
35       * A static {@code Size} instance reference.
36       */
37      private static final Size<Object> INSTANCE = new Size<Object>();
38  
39      // constructor
40      // ------------------------------------------------------------------------
41      /**
42       * Create a new Size.
43       */
44      public Size() { }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public Integer evaluate(Object obj) {
50          Validate.notNull(obj, "Argument must not be null");
51          if (obj instanceof Collection<?>) {
52              return evaluate((Collection<?>) obj);
53          }
54          if (obj instanceof String) {
55              return evaluate((String) obj);
56          }
57          if (obj.getClass().isArray()) {
58              return evaluateArray(obj);
59          }
60          throw new IllegalArgumentException("Expected Collection, String or Array, found " + obj);
61      }
62  
63      /**
64       * {@inheritDoc}
65       */
66      @Override
67      public boolean equals(Object that) {
68          return that instanceof Size<?>;
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      public int hashCode() {
76          return "Size".hashCode();
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public String toString() {
84          return "Size()";
85      }
86  
87      /**
88       * Get a Size instance.
89       * @return Size
90       */
91      public static Size<Object> instance() {
92          return INSTANCE;
93      }
94  
95      /**
96       * Evaluate a Collection.
97       * @param col to evaluate
98       * @return Integer
99       */
100     private Integer evaluate(Collection<?> col) {
101         return Integer.valueOf(col.size());
102     }
103 
104     /**
105      * Evaluate a String.
106      * @param str to evaluate
107      * @return Integer
108      */
109     private Integer evaluate(String str) {
110         return Integer.valueOf(str.length());
111     }
112 
113     /**
114      * Evaluate an array.
115      * @param array to evaluate
116      * @return Integer
117      */
118     private Integer evaluateArray(Object array) {
119         return Integer.valueOf(Array.getLength(array));
120     }
121 
122 }