| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.bulenkov.iconloader.util; |
| 18 |
|
| 19 |
import java.util.Collection; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
@SuppressWarnings({"UtilityClassWithoutPrivateConstructor", "SSBasedInspection"}) |
| 25 |
public class ArrayUtilRt { |
| 26 |
private static final int ARRAY_COPY_THRESHOLD = 20; |
| 27 |
|
| 28 |
public static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 29 |
|
| 30 |
|
| 31 |
public static String[] toStringArray(Collection<String> collection) { |
| 32 |
return collection == null || collection.isEmpty() |
| 33 |
? EMPTY_STRING_ARRAY : toArray(collection, new String[collection.size()]); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public static <T> T[] toArray(Collection<T> c, T[] sample) { |
| 42 |
final int size = c.size(); |
| 43 |
if (size == sample.length && size < ARRAY_COPY_THRESHOLD) { |
| 44 |
int i = 0; |
| 45 |
for (T t : c) { |
| 46 |
sample[i++] = t; |
| 47 |
} |
| 48 |
return sample; |
| 49 |
} |
| 50 |
|
| 51 |
return c.toArray(sample); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public static <T> int find(final T[] src, final T obj) { |
| 63 |
for (int i = 0; i < src.length; i++) { |
| 64 |
final T o = src[i]; |
| 65 |
if (o == null) { |
| 66 |
if (obj == null) { |
| 67 |
return i; |
| 68 |
} |
| 69 |
} else { |
| 70 |
if (o.equals(obj)) { |
| 71 |
return i; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
return -1; |
| 76 |
} |
| 77 |
} |