-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathArrayUtils.java
More file actions
372 lines (339 loc) · 12.3 KB
/
ArrayUtils.java
File metadata and controls
372 lines (339 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2026 SciJava developers.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
// Portions of this class were derived from the loci.common.DataTools class of
// the Bio-Formats library, licensed according to Simplified BSD, as follows:
//
// Copyright (C) 2005 - 2015 Open Microscopy Environment:
// - Board of Regents of the University of Wisconsin-Madison
// - Glencoe Software, Inc.
// - University of Dundee
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
package org.scijava.util;
import java.util.Collection;
import java.util.List;
/**
* Utility class for working with arrays, particularly arrays of primitives.
*
* @author Mark Hiner
* @author Curtis Rueden
* @author Melissa Linkert
* @author Chris Allan
*/
public final class ArrayUtils {
private ArrayUtils() {
// prevent instantiation of utility class
}
// -- ArrayUtils methods --
/** Creates an array of the given type, containing the specified values. */
@SafeVarargs
public static <T> T[] array(final T... values) {
return values;
}
/**
* Converts the provided Object to a {@link Collection} implementation. If the
* object is an array type, a {@link PrimitiveArray} wrapper will be created.
*/
public static Collection<?> toCollection(final Object value) {
// If the value is null or we have a collection, just return it
if (value == null || Collection.class.isAssignableFrom(value.getClass())) {
return (Collection<?>) value;
}
// Check for primitive array types
if (value instanceof char[]) {
return new CharArray((char[]) value);
}
if (value instanceof byte[]) {
return new ByteArray((byte[]) value);
}
if (value instanceof boolean[]) {
return new BoolArray((boolean[]) value);
}
if (value instanceof short[]) {
return new ShortArray((short[]) value);
}
if (value instanceof int[]) {
return new IntArray((int[]) value);
}
if (value instanceof long[]) {
return new LongArray((long[]) value);
}
if (value instanceof float[]) {
return new FloatArray((float[]) value);
}
if (value instanceof double[]) {
return new DoubleArray((double[]) value);
}
if (value instanceof Object[]) {
return new ObjectArray<>((Object[]) value);
}
// This object is neither an array nor a collection.
// So we wrap it in a list and return.
final List<Object> list = new ObjectArray<>(Object.class);
list.add(value);
return list;
}
/**
* Allocates a 1-dimensional byte array matching the product of the given
* sizes.
*
* @param sizes list of sizes from which to allocate the array
* @return a byte array of the appropriate size
* @throws IllegalArgumentException if the total size exceeds 2GB, which is
* the maximum size of an array in Java; or if any size argument is
* zero or negative
*/
public static byte[] allocate(final long... sizes)
throws IllegalArgumentException
{
if (sizes == null) return null;
if (sizes.length == 0) return new byte[0];
final int total = safeMultiply32(sizes);
return new byte[total];
}
/**
* Checks that the product of the given sizes does not exceed the 32-bit
* integer limit (i.e., {@link Integer#MAX_VALUE}).
*
* @param sizes list of sizes from which to compute the product
* @return the product of the given sizes
* @throws IllegalArgumentException if the total size exceeds 2GiB, which is
* the maximum size of an int in Java; or if any size argument is
* zero or negative
*/
public static int safeMultiply32(final long... sizes)
throws IllegalArgumentException
{
if (sizes.length == 0) return 0;
long total = 1;
for (final long size : sizes) {
if (size < 1) {
throw new IllegalArgumentException("Invalid array size: " +
sizeAsProduct(sizes));
}
total *= size;
if (total > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Array size too large: " +
sizeAsProduct(sizes));
}
}
// NB: The downcast to int is safe here, due to the checks above.
return (int) total;
}
/**
* Checks that the product of the given sizes does not exceed the 64-bit
* integer limit (i.e., {@link Long#MAX_VALUE}).
*
* @param sizes list of sizes from which to compute the product
* @return the product of the given sizes
* @throws IllegalArgumentException if the total size exceeds 8EiB, which is
* the maximum size of a long in Java; or if any size argument is
* zero or negative
*/
public static long safeMultiply64(final long... sizes)
throws IllegalArgumentException
{
if (sizes.length == 0) return 0;
long total = 1;
for (final long size : sizes) {
if (size < 1) {
throw new IllegalArgumentException("Invalid array size: " +
sizeAsProduct(sizes));
}
if (willOverflow(total, size)) {
throw new IllegalArgumentException("Array size too large: " +
sizeAsProduct(sizes));
}
total *= size;
}
return total;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final byte[] array, final byte value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final boolean[] array, final boolean value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final char[] array, final char value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final double[] array, final double value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final float[] array, final float value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final int[] array, final int value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final long[] array, final long value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final short[] array, final short value) {
return indexOf(array, value) != -1;
}
/** Returns true if the given value is contained in the specified array. */
public static boolean contains(final Object[] array, final Object value) {
return indexOf(array, value) != -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final boolean[] array, final boolean value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final byte[] array, final byte value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final char[] array, final char value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final double[] array, final double value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final float[] array, final float value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final int[] array, final int value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final long[] array, final long value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* array. If the value is not in the array, returns -1.
*/
public static int indexOf(final short[] array, final short value) {
for (int i = 0; i < array.length; i++) {
if (array[i] == value) return i;
}
return -1;
}
/**
* Returns the index of the first occurrence of the given value in the given
* Object array. If the value is not in the array, returns -1.
*/
public static int indexOf(final Object[] array, final Object value) {
for (int i = 0; i < array.length; i++) {
if (value == null) {
if (array[i] == null) return i;
}
else if (value.equals(array[i])) return i;
}
return -1;
}
// -- Helper methods --
private static String sizeAsProduct(final long... sizes) {
final StringBuilder sb = new StringBuilder();
boolean first = true;
for (final long size : sizes) {
if (first) first = false;
else sb.append(" x ");
sb.append(size);
}
return sb.toString();
}
private static boolean willOverflow(final long v1, final long v2) {
return Long.MAX_VALUE / v1 < v2;
}
}