Debian Codegraph @

src/com/bulenkov/iconloader/util/ArrayUtilRt.java

raw symbols SCIP
1 /*
2 * Copyright 2000-2014 JetBrains s.r.o.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.bulenkov.iconloader.util;
18
19 import java.util.Collection;
20
21 /**
22 * @author Konstantin Bulenkov
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 * This is a replacement for {@link Collection#toArray(Object[])}. For small collections it is faster to stay at java level and refrain
38 * from calling JNI {@link System#arraycopy(Object, int, Object, int, int)}
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 * @param src source array.
56 * @param obj object to be found.
57 * @return index of <code>obj</code> in the <code>src</code> array.
58 * Returns <code>-1</code> if passed object isn't found. This method uses
59 * <code>equals</code> of arrays elements to compare <code>obj</code> with
60 * these elements.
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 }