gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / ContainerUtils.java @ 2648
History | View | Annotate | Download (9.07 KB)
1 | 2386 | jjdelcerro | /*
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2020 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
18 | *
|
||
19 | * For any additional information, do not hesitate to contact us
|
||
20 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
21 | */
|
||
22 | |||
23 | package org.gvsig.tools.util; |
||
24 | |||
25 | import java.util.ArrayList; |
||
26 | import java.util.Collection; |
||
27 | 2433 | jjdelcerro | import java.util.Comparator; |
28 | 2386 | jjdelcerro | import java.util.Iterator; |
29 | import java.util.List; |
||
30 | import java.util.Map; |
||
31 | 2433 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
32 | 2386 | jjdelcerro | |
33 | /**
|
||
34 | *
|
||
35 | * @author gvSIG Team
|
||
36 | */
|
||
37 | public class ContainerUtils { |
||
38 | |||
39 | private ContainerUtils() {
|
||
40 | |||
41 | } |
||
42 | |||
43 | public static long size64(Size64 x) { |
||
44 | return x.size64();
|
||
45 | } |
||
46 | |||
47 | public static long size64(Size x) { |
||
48 | return x.size();
|
||
49 | } |
||
50 | |||
51 | public static long size64(Collection x) { |
||
52 | return x.size();
|
||
53 | } |
||
54 | |||
55 | public static long size64(Map x) { |
||
56 | return x.size();
|
||
57 | } |
||
58 | |||
59 | public static long size64(Object[] x) { |
||
60 | return x.length;
|
||
61 | } |
||
62 | |||
63 | public static long size64(Object x) { |
||
64 | if( x instanceof Size64 ) { |
||
65 | return ((Size64)x).size64();
|
||
66 | } |
||
67 | if( x instanceof Collection) { |
||
68 | return ((Collection)x).size(); |
||
69 | } |
||
70 | if( x instanceof Map) { |
||
71 | return ((Map)x).size(); |
||
72 | } |
||
73 | if( x instanceof Size) { |
||
74 | return ((Size)x).size();
|
||
75 | } |
||
76 | if( x instanceof Object[] ) { |
||
77 | return ((Object[])x).length; |
||
78 | } |
||
79 | 2648 | jjdelcerro | if( x instanceof Iterator ) { |
80 | long i = 0; |
||
81 | while( ((Iterator) x).hasNext() ) { |
||
82 | ((Iterator) x).next();
|
||
83 | i++; |
||
84 | } |
||
85 | return i;
|
||
86 | } |
||
87 | if( x instanceof Iterable ) { |
||
88 | Iterator it = ((Iterable)x).iterator(); |
||
89 | long i = 0; |
||
90 | while( ((Iterator) it).hasNext() ) { |
||
91 | ((Iterator) it).next();
|
||
92 | i++; |
||
93 | } |
||
94 | return i;
|
||
95 | } |
||
96 | 2386 | jjdelcerro | return -1; |
97 | } |
||
98 | |||
99 | public static int size(Size x) { |
||
100 | return x.size();
|
||
101 | } |
||
102 | |||
103 | public static long size(Size64 x) { |
||
104 | return x.size64();
|
||
105 | } |
||
106 | |||
107 | public static long size(Collection x) { |
||
108 | return x.size();
|
||
109 | } |
||
110 | |||
111 | public static long size(Map x) { |
||
112 | return x.size();
|
||
113 | } |
||
114 | |||
115 | public static long size(Object[] x) { |
||
116 | return x.length;
|
||
117 | } |
||
118 | |||
119 | public static int size(Object x) { |
||
120 | if( x instanceof Size ) { |
||
121 | return ((Size)x).size();
|
||
122 | } |
||
123 | if( x instanceof Collection) { |
||
124 | return ((Collection)x).size(); |
||
125 | } |
||
126 | if( x instanceof Map) { |
||
127 | return ((Map)x).size(); |
||
128 | } |
||
129 | if( x instanceof Size64) { |
||
130 | long sz = ((Size64)x).size64();
|
||
131 | if( sz <= Integer.MAX_VALUE ) { |
||
132 | return (int) sz; |
||
133 | } |
||
134 | } |
||
135 | if( x instanceof Object[] ) { |
||
136 | return ((Object[])x).length; |
||
137 | } |
||
138 | 2648 | jjdelcerro | if( x instanceof Iterator ) { |
139 | int i = 0; |
||
140 | while( ((Iterator) x).hasNext() ) { |
||
141 | ((Iterator) x).next();
|
||
142 | i++; |
||
143 | } |
||
144 | return i;
|
||
145 | } |
||
146 | if( x instanceof Iterable ) { |
||
147 | Iterator it = ((Iterable)x).iterator(); |
||
148 | int i = 0; |
||
149 | while( ((Iterator) it).hasNext() ) { |
||
150 | ((Iterator) it).next();
|
||
151 | i++; |
||
152 | } |
||
153 | return i;
|
||
154 | } |
||
155 | 2386 | jjdelcerro | return -1; |
156 | } |
||
157 | |||
158 | public static Boolean isEmpty(IsEmpty x, boolean defaultValue) { |
||
159 | return x.isEmpty();
|
||
160 | } |
||
161 | |||
162 | public static Boolean isEmpty(Collection x, boolean defaultValue) { |
||
163 | return x.isEmpty();
|
||
164 | } |
||
165 | |||
166 | public static Boolean isEmpty(Map x, boolean defaultValue) { |
||
167 | return x.isEmpty();
|
||
168 | } |
||
169 | |||
170 | public static Boolean isEmpty(Object x, boolean defaultValue) { |
||
171 | if( x instanceof IsEmpty ) { |
||
172 | return ((IsEmpty)x).isEmpty();
|
||
173 | } |
||
174 | if( x instanceof Collection) { |
||
175 | return ((Collection)x).isEmpty(); |
||
176 | } |
||
177 | if( x instanceof Map) { |
||
178 | return ((Map)x).isEmpty(); |
||
179 | } |
||
180 | 2648 | jjdelcerro | if( x instanceof Iterator ) { |
181 | return ((Iterator) x).hasNext(); |
||
182 | } |
||
183 | if( x instanceof Iterable ) { |
||
184 | Iterator it = ((Iterable)x).iterator(); |
||
185 | return it.hasNext();
|
||
186 | } |
||
187 | 2386 | jjdelcerro | return defaultValue;
|
188 | } |
||
189 | |||
190 | public static Object get64(GetItem64 x, long position) { |
||
191 | return x.get64(position);
|
||
192 | } |
||
193 | |||
194 | public static Object get64(GetItem x, long position) { |
||
195 | if( position>Integer.MAX_VALUE ) { |
||
196 | return null; |
||
197 | } |
||
198 | return x.get((int) position); |
||
199 | } |
||
200 | |||
201 | public static Object get64(List x, long position) { |
||
202 | if( position>Integer.MAX_VALUE ) { |
||
203 | return null; |
||
204 | } |
||
205 | return x.get((int) position); |
||
206 | } |
||
207 | |||
208 | public static Object get64(Object[] x, long position) { |
||
209 | if( position>Integer.MAX_VALUE ) { |
||
210 | return null; |
||
211 | } |
||
212 | return x[(int)position]; |
||
213 | } |
||
214 | |||
215 | public static Object get64(Object x, long position) { |
||
216 | if( x instanceof GetItem64 ) { |
||
217 | return ((GetItem64)x).get64(position);
|
||
218 | } |
||
219 | if( position>Integer.MAX_VALUE ) { |
||
220 | return null; |
||
221 | } |
||
222 | if( x instanceof List) { |
||
223 | return ((List)x).get((int) position); |
||
224 | } |
||
225 | if( x instanceof GetItem) { |
||
226 | return ((GetItem)x).get((int) position); |
||
227 | } |
||
228 | if( x instanceof Object[] ) { |
||
229 | return ((Object[])x)[(int)position]; |
||
230 | } |
||
231 | return null; |
||
232 | } |
||
233 | |||
234 | 2648 | jjdelcerro | public static Object get64(Object x, Object key) { |
235 | return get(x,key);
|
||
236 | } |
||
237 | |||
238 | 2386 | jjdelcerro | public static Object get(GetItem x, int position) { |
239 | return x.get(position);
|
||
240 | } |
||
241 | |||
242 | public static Object get(Object x, int position) { |
||
243 | if( x instanceof GetItem ) { |
||
244 | return ((GetItem)x).get(position);
|
||
245 | } |
||
246 | if( x instanceof List) { |
||
247 | return ((List)x).get(position); |
||
248 | } |
||
249 | if( x instanceof GetItem64) { |
||
250 | return ((GetItem64)x).get64(position);
|
||
251 | } |
||
252 | if( x instanceof Object[] ) { |
||
253 | return ((Object[])x)[position]; |
||
254 | } |
||
255 | return null; |
||
256 | } |
||
257 | |||
258 | public static Object get(Object x, Object key) { |
||
259 | if( x instanceof GetItemByKey ) { |
||
260 | return ((GetItemByKey)x).get(key);
|
||
261 | } |
||
262 | if( x instanceof Map ) { |
||
263 | return ((Map)x).get(key); |
||
264 | } |
||
265 | 2648 | jjdelcerro | if( key instanceof Number ) { |
266 | if( x instanceof GetItem ) { |
||
267 | return ((GetItem)x).get(((Number) key).intValue()); |
||
268 | } |
||
269 | if( x instanceof List) { |
||
270 | return ((List)x).get(((Number) key).intValue()); |
||
271 | } |
||
272 | if( x instanceof GetItem64) { |
||
273 | return ((GetItem64)x).get64(((Number) key).longValue()); |
||
274 | } |
||
275 | if( x instanceof Object[] ) { |
||
276 | return ((Object[])x)[((Number) key).intValue()]; |
||
277 | } |
||
278 | } |
||
279 | 2386 | jjdelcerro | return null; |
280 | } |
||
281 | |||
282 | public static List getKeys(GetKeys x) { |
||
283 | return x.getKeys();
|
||
284 | } |
||
285 | |||
286 | public static List getKeys(Object x) { |
||
287 | if( x instanceof GetKeys ) { |
||
288 | return ((GetKeys)x).getKeys();
|
||
289 | } |
||
290 | if( x instanceof Map ) { |
||
291 | return new ArrayList(((Map) x).keySet()); |
||
292 | } |
||
293 | return null; |
||
294 | } |
||
295 | |||
296 | public static Iterator getKeysIterator(Object x) { |
||
297 | if( x instanceof GetKeys ) { |
||
298 | return ((GetKeys)x).getKeys().iterator();
|
||
299 | } |
||
300 | if( x instanceof Map ) { |
||
301 | return ((Map) x).keySet().iterator(); |
||
302 | } |
||
303 | return null; |
||
304 | } |
||
305 | |||
306 | 2433 | jjdelcerro | public static final Comparator<String> EQUALS_IGNORECASE_COMPARATOR = (Comparator<String>) (String o1, String o2) -> StringUtils.compareIgnoreCase(o1, o2); |
307 | |||
308 | 2640 | omartinez | public static boolean notContains(Collection collection, Object value, Comparator comparator) { |
309 | if(collection==null) { |
||
310 | return false; |
||
311 | } |
||
312 | if(comparator==null) { |
||
313 | comparator=EQUALS_IGNORECASE_COMPARATOR; |
||
314 | } |
||
315 | for (Object item : collection) { |
||
316 | if( comparator.compare(item, value)==0 ) { |
||
317 | return true; |
||
318 | } |
||
319 | } |
||
320 | return false; |
||
321 | } |
||
322 | |||
323 | 2433 | jjdelcerro | public static boolean contains(Collection collection, Object value, Comparator comparator) { |
324 | 2640 | omartinez | if(collection==null) { |
325 | return false; |
||
326 | } |
||
327 | if(comparator==null) { |
||
328 | comparator=EQUALS_IGNORECASE_COMPARATOR; |
||
329 | } |
||
330 | 2433 | jjdelcerro | for (Object item : collection) { |
331 | if( comparator.compare(item, value)==0 ) { |
||
332 | return true; |
||
333 | } |
||
334 | } |
||
335 | return false; |
||
336 | } |
||
337 | 2386 | jjdelcerro | } |