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 @ 2640

History | View | Annotate | Download (7.36 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 java.util.Objects;
32
import org.apache.commons.lang3.ObjectUtils;
33
import org.apache.commons.lang3.StringUtils;
34

    
35
/**
36
 *
37
 * @author gvSIG Team
38
 */
39
public class ContainerUtils {
40

    
41
    private ContainerUtils() {
42
        
43
    }
44
    
45
    public static long size64(Size64 x) {
46
        return x.size64();
47
    }
48

    
49
    public static long size64(Size x) {
50
        return x.size();
51
    }
52

    
53
    public static long size64(Collection x) {
54
        return x.size();
55
    }
56

    
57
    public static long size64(Map x) {
58
        return x.size();
59
    }
60

    
61
    public static long size64(Object[] x) {
62
        return x.length;
63
    }
64

    
65
    public static long size64(Object x) {
66
        if( x instanceof Size64 ) {
67
            return ((Size64)x).size64();
68
        }
69
        if( x instanceof Collection) {
70
            return ((Collection)x).size();
71
        }
72
        if( x instanceof Map) {
73
            return ((Map)x).size();
74
        }
75
        if( x instanceof Size) {
76
            return ((Size)x).size();
77
        }
78
        if( x instanceof Object[] ) {
79
            return ((Object[])x).length;
80
        }
81
        return -1;
82
    }
83
    
84
    public static int size(Size x) {
85
        return x.size();
86
    }
87

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

    
92
    public static long size(Collection x) {
93
        return x.size();
94
    }
95

    
96
    public static long size(Map x) {
97
        return x.size();
98
    }
99

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

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

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

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

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