Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / util / ContainerUtils.java @ 2647

History | View | Annotate | Download (7.29 KB)

1
/*
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
import java.util.Comparator;
28
import java.util.Iterator;
29
import java.util.List;
30
import java.util.Map;
31
import org.apache.commons.lang3.StringUtils;
32

    
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
        return -1;
80
    }
81
    
82
    public static int size(Size x) {
83
        return x.size();
84
    }
85

    
86
    public static long size(Size64 x) {
87
        return x.size64();
88
    }
89

    
90
    public static long size(Collection x) {
91
        return x.size();
92
    }
93

    
94
    public static long size(Map x) {
95
        return x.size();
96
    }
97

    
98
    public static long size(Object[] x) {
99
        return x.length;
100
    }
101

    
102
    public static int size(Object x) {
103
        if( x instanceof Size ) {
104
            return ((Size)x).size();
105
        }
106
        if( x instanceof Collection) {
107
            return ((Collection)x).size();
108
        }
109
        if( x instanceof Map) {
110
            return ((Map)x).size();
111
        }
112
        if( x instanceof Size64) {
113
            long sz = ((Size64)x).size64();
114
            if( sz <= Integer.MAX_VALUE ) {
115
                return (int) sz;
116
            }
117
        }
118
        if( x instanceof Object[] ) {
119
            return ((Object[])x).length;
120
        }
121
        return -1;
122
    }
123
    
124
    public static Boolean isEmpty(IsEmpty x, boolean defaultValue) {
125
        return x.isEmpty();
126
    }
127
    
128
    public static Boolean isEmpty(Collection x, boolean defaultValue) {
129
        return x.isEmpty();
130
    }
131
    
132
    public static Boolean isEmpty(Map x, boolean defaultValue) {
133
        return x.isEmpty();
134
    }
135
    
136
    public static Boolean isEmpty(Object x, boolean defaultValue) {
137
        if( x instanceof IsEmpty ) {
138
            return ((IsEmpty)x).isEmpty();
139
        }
140
        if( x instanceof Collection) {
141
            return ((Collection)x).isEmpty();
142
        }
143
        if( x instanceof Map) {
144
            return ((Map)x).isEmpty();
145
        }
146
        return defaultValue;
147
    }
148
    
149
    public static Object get64(GetItem64 x, long position) {
150
        return x.get64(position);
151
    }
152
    
153
    public static Object get64(GetItem x, long position) {
154
        if( position>Integer.MAX_VALUE ) {
155
            return null;
156
        }
157
        return x.get((int) position);
158
    }
159
    
160
    public static Object get64(List x, long position) {
161
        if( position>Integer.MAX_VALUE ) {
162
            return null;
163
        }
164
        return x.get((int) position);
165
    }
166
    
167
    public static Object get64(Object[] x, long position) {
168
        if( position>Integer.MAX_VALUE ) {
169
            return null;
170
        }
171
        return x[(int)position];
172
    }
173
    
174
    public static Object get64(Object x, long position) {
175
        if( x instanceof GetItem64 ) {
176
            return ((GetItem64)x).get64(position);
177
        }
178
        if( position>Integer.MAX_VALUE ) {
179
            return null;
180
        }
181
        if( x instanceof List) {
182
            return ((List)x).get((int) position);
183
        }
184
        if( x instanceof GetItem) {
185
            return ((GetItem)x).get((int) position);
186
        }
187
        if( x instanceof Object[] ) {
188
            return ((Object[])x)[(int)position];
189
        }
190
        return null;
191
    }
192
    
193
    public static Object get(GetItem x, int position) {
194
        return x.get(position);
195
    }
196
    
197
    public static Object get(Object x, int position) {
198
        if( x instanceof GetItem ) {
199
            return ((GetItem)x).get(position);
200
        }
201
        if( x instanceof List) {
202
            return ((List)x).get(position);
203
        }
204
        if( x instanceof GetItem64) {
205
            return ((GetItem64)x).get64(position);
206
        }
207
        if( x instanceof Object[] ) {
208
            return ((Object[])x)[position];
209
        }
210
        return null;
211
    }
212
    
213
    public static Object get(Object x, Object key) {
214
        if( x instanceof GetItemByKey ) {
215
            return ((GetItemByKey)x).get(key);
216
        }
217
        if( x instanceof Map ) {
218
            return ((Map)x).get(key);
219
        }
220
        return null;
221
    }
222
    
223
    public static List getKeys(GetKeys x) {
224
        return x.getKeys();
225
    }
226

    
227
    public static List getKeys(Object x) {
228
        if( x instanceof GetKeys ) {
229
            return ((GetKeys)x).getKeys();
230
        }
231
        if( x instanceof Map ) {
232
            return new ArrayList(((Map) x).keySet());
233
        }
234
        return null;
235
    }
236

    
237
    public static Iterator getKeysIterator(Object x) {
238
        if( x instanceof GetKeys ) {
239
            return ((GetKeys)x).getKeys().iterator();
240
        }
241
        if( x instanceof Map ) {
242
            return ((Map) x).keySet().iterator();
243
        }
244
        return null;
245
    }
246

    
247
    public static final Comparator<String> EQUALS_IGNORECASE_COMPARATOR = (Comparator<String>) (String o1, String o2) -> StringUtils.compareIgnoreCase(o1, o2);
248
            
249
    public static boolean notContains(Collection collection, Object value, Comparator comparator) {
250
        if(collection==null) {
251
            return false;
252
        }
253
        if(comparator==null) {
254
            comparator=EQUALS_IGNORECASE_COMPARATOR;
255
        }
256
        for (Object item : collection) {
257
            if( comparator.compare(item, value)==0 ) {
258
                return true;
259
            }
260
        }
261
        return false;
262
    }
263
    
264
    public static boolean contains(Collection collection, Object value, Comparator comparator) {
265
        if(collection==null) {
266
            return false;
267
        }
268
        if(comparator==null) {
269
            comparator=EQUALS_IGNORECASE_COMPARATOR;
270
        }
271
        for (Object item : collection) {
272
            if( comparator.compare(item, value)==0 ) {
273
                return true;
274
            }
275
        }
276
        return false;
277
    }
278
}