|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.misc; |
|
27 |
|
28 import java.lang.reflect.Array; |
|
29 import com.oracle.solaris.vp.util.misc.converter.Converter; |
|
30 |
|
31 public class ArrayUtil { |
|
32 public static <T, F extends T> T[] concat(Class<T> clazz, F[]... arrays) { |
|
33 int length = 0; |
|
34 for (T[] array : arrays) { |
|
35 length += array.length; |
|
36 } |
|
37 |
|
38 @SuppressWarnings({"unchecked"}) |
|
39 T[] dest = (T[])Array.newInstance(clazz, length); |
|
40 |
|
41 int offset = 0; |
|
42 for (T[] array : arrays) { |
|
43 System.arraycopy(array, 0, dest, offset, array.length); |
|
44 offset += array.length; |
|
45 } |
|
46 |
|
47 return dest; |
|
48 } |
|
49 |
|
50 public static <T> boolean contains(T[] array, T element) { |
|
51 return indexOf(array, element) != -1; |
|
52 } |
|
53 |
|
54 public static boolean contains(boolean[] array, boolean element) { |
|
55 return indexOf(array, element) != -1; |
|
56 } |
|
57 |
|
58 public static boolean contains(byte[] array, byte element) { |
|
59 return indexOf(array, element) != -1; |
|
60 } |
|
61 |
|
62 public static boolean contains(char[] array, char element) { |
|
63 return indexOf(array, element) != -1; |
|
64 } |
|
65 |
|
66 public static boolean contains(double[] array, double element) { |
|
67 return indexOf(array, element) != -1; |
|
68 } |
|
69 |
|
70 public static boolean contains(float[] array, float element) { |
|
71 return indexOf(array, element) != -1; |
|
72 } |
|
73 |
|
74 public static boolean contains(int[] array, int element) { |
|
75 return indexOf(array, element) != -1; |
|
76 } |
|
77 |
|
78 public static boolean contains(long[] array, long element) { |
|
79 return indexOf(array, element) != -1; |
|
80 } |
|
81 |
|
82 public static boolean contains(short[] array, short element) { |
|
83 return indexOf(array, element) != -1; |
|
84 } |
|
85 |
|
86 @SuppressWarnings({"unchecked"}) |
|
87 public static <F, T> T[] convert(F[] from, Class<T> toClass, |
|
88 Converter<F, T> converter) { |
|
89 T[] to = (T[])Array.newInstance(toClass, from.length); |
|
90 for (int i = 0; i < from.length; i++) { |
|
91 to[i] = converter.convert(from[i]); |
|
92 } |
|
93 return to; |
|
94 } |
|
95 |
|
96 public static <T> int indexOf(T[] array, T element) { |
|
97 for (int i = 0; i < array.length; i++) { |
|
98 if (ObjectUtil.equals(array[i], element)) { |
|
99 return i; |
|
100 } |
|
101 } |
|
102 return -1; |
|
103 } |
|
104 |
|
105 public static int indexOf(boolean[] array, boolean element) { |
|
106 for (int i = 0; i < array.length; i++) { |
|
107 if (array[i] == element) { |
|
108 return i; |
|
109 } |
|
110 } |
|
111 return -1; |
|
112 } |
|
113 |
|
114 public static int indexOf(byte[] array, byte element) { |
|
115 for (int i = 0; i < array.length; i++) { |
|
116 if (array[i] == element) { |
|
117 return i; |
|
118 } |
|
119 } |
|
120 return -1; |
|
121 } |
|
122 |
|
123 public static int indexOf(char[] array, char element) { |
|
124 for (int i = 0; i < array.length; i++) { |
|
125 if (array[i] == element) { |
|
126 return i; |
|
127 } |
|
128 } |
|
129 return -1; |
|
130 } |
|
131 |
|
132 public static int indexOf(double[] array, double element) { |
|
133 for (int i = 0; i < array.length; i++) { |
|
134 if (array[i] == element) { |
|
135 return i; |
|
136 } |
|
137 } |
|
138 return -1; |
|
139 } |
|
140 |
|
141 public static int indexOf(float[] array, float element) { |
|
142 for (int i = 0; i < array.length; i++) { |
|
143 if (array[i] == element) { |
|
144 return i; |
|
145 } |
|
146 } |
|
147 return -1; |
|
148 } |
|
149 |
|
150 public static int indexOf(int[] array, int element) { |
|
151 for (int i = 0; i < array.length; i++) { |
|
152 if (array[i] == element) { |
|
153 return i; |
|
154 } |
|
155 } |
|
156 return -1; |
|
157 } |
|
158 |
|
159 public static int indexOf(long[] array, long element) { |
|
160 for (int i = 0; i < array.length; i++) { |
|
161 if (array[i] == element) { |
|
162 return i; |
|
163 } |
|
164 } |
|
165 return -1; |
|
166 } |
|
167 |
|
168 public static int indexOf(short[] array, short element) { |
|
169 for (int i = 0; i < array.length; i++) { |
|
170 if (array[i] == element) { |
|
171 return i; |
|
172 } |
|
173 } |
|
174 return -1; |
|
175 } |
|
176 } |