Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Size |
|
| 2.111111111111111;2.111 |
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 | ||
23 | import org.apache.commons.functor.UnaryFunction; | |
24 | ||
25 | /** | |
26 | * Returns the size of the specified Collection, or the length | |
27 | * of the specified array or String. | |
28 | * @version $Revision: 1166334 $ $Date: 2011-09-07 21:37:49 +0200 (Wed, 07 Sep 2011) $ | |
29 | * @author Rodney Waldhoff | |
30 | */ | |
31 | 108 | public final class Size<A> implements UnaryFunction<A, Integer>, Serializable { |
32 | ||
33 | /** | |
34 | * serialVersionUID declaration. | |
35 | */ | |
36 | private static final long serialVersionUID = -12374650738412129L; | |
37 | 2 | private static final Size<Object> INSTANCE = new Size<Object>(); |
38 | ||
39 | // constructor | |
40 | // ------------------------------------------------------------------------ | |
41 | /** | |
42 | * Create a new Size. | |
43 | */ | |
44 | 18 | public Size() { } |
45 | ||
46 | /** | |
47 | * {@inheritDoc} | |
48 | */ | |
49 | public Integer evaluate(Object obj) { | |
50 | 142 | if (obj instanceof Collection<?>) { |
51 | 24 | return evaluate((Collection<?>) obj); |
52 | } | |
53 | 118 | if (obj instanceof String) { |
54 | 110 | return evaluate((String) obj); |
55 | } | |
56 | 8 | if (null != obj && obj.getClass().isArray()) { |
57 | 4 | return evaluateArray(obj); |
58 | } | |
59 | 4 | if (null == obj) { |
60 | 2 | throw new IllegalArgumentException("Argument must not be null"); |
61 | } | |
62 | 2 | throw new IllegalArgumentException("Expected Collection, String or Array, found " + obj); |
63 | } | |
64 | ||
65 | /** | |
66 | * {@inheritDoc} | |
67 | */ | |
68 | public boolean equals(Object that) { | |
69 | 24 | return that instanceof Size<?>; |
70 | } | |
71 | ||
72 | /** | |
73 | * {@inheritDoc} | |
74 | */ | |
75 | public int hashCode() { | |
76 | 24 | return "Size".hashCode(); |
77 | } | |
78 | ||
79 | /** | |
80 | * {@inheritDoc} | |
81 | */ | |
82 | public String toString() { | |
83 | 16 | return "Size()"; |
84 | } | |
85 | ||
86 | /** | |
87 | * Get a Size instance. | |
88 | * @return Size | |
89 | */ | |
90 | public static Size<Object> instance() { | |
91 | 48 | return INSTANCE; |
92 | } | |
93 | ||
94 | /** | |
95 | * Evaluate a Collection. | |
96 | * @param col to evaluate | |
97 | * @return Integer | |
98 | */ | |
99 | private int evaluate(Collection<?> col) { | |
100 | 24 | return col.size(); |
101 | } | |
102 | ||
103 | /** | |
104 | * Evaluate a String. | |
105 | * @param str to evaluate | |
106 | * @return Integer | |
107 | */ | |
108 | private int evaluate(String str) { | |
109 | 110 | return str.length(); |
110 | } | |
111 | ||
112 | /** | |
113 | * Evaluate an array. | |
114 | * @param array to evaluate | |
115 | * @return Integer | |
116 | */ | |
117 | private int evaluateArray(Object array) { | |
118 | 4 | return Array.getLength(array); |
119 | } | |
120 | ||
121 | } |