001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.util;
022:
023: import java.util.ArrayList;
024: import java.util.Comparator;
025: import java.util.List;
026: import java.util.Set;
027: import java.util.TreeSet;
028:
029: /**
030: * <a href="ArrayUtil.java.html"><b><i>View Source</i></b></a>
031: *
032: * @author Brian Wing Shun Chan
033: *
034: */
035: public class ArrayUtil {
036:
037: public static Boolean[] append(Boolean[] array, Boolean obj) {
038: Boolean[] newArray = new Boolean[array.length + 1];
039:
040: System.arraycopy(array, 0, newArray, 0, array.length);
041:
042: newArray[newArray.length - 1] = obj;
043:
044: return newArray;
045: }
046:
047: public static Double[] append(Double[] array, Double obj) {
048: Double[] newArray = new Double[array.length + 1];
049:
050: System.arraycopy(array, 0, newArray, 0, array.length);
051:
052: newArray[newArray.length - 1] = obj;
053:
054: return newArray;
055: }
056:
057: public static Float[] append(Float[] array, Float obj) {
058: Float[] newArray = new Float[array.length + 1];
059:
060: System.arraycopy(array, 0, newArray, 0, array.length);
061:
062: newArray[newArray.length - 1] = obj;
063:
064: return newArray;
065: }
066:
067: public static Integer[] append(Integer[] array, Integer obj) {
068: Integer[] newArray = new Integer[array.length + 1];
069:
070: System.arraycopy(array, 0, newArray, 0, array.length);
071:
072: newArray[newArray.length - 1] = obj;
073:
074: return newArray;
075: }
076:
077: public static Long[] append(Long[] array, Long obj) {
078: Long[] newArray = new Long[array.length + 1];
079:
080: System.arraycopy(array, 0, newArray, 0, array.length);
081:
082: newArray[newArray.length - 1] = obj;
083:
084: return newArray;
085: }
086:
087: public static Object[] append(Object[] array, Object obj) {
088: Object[] newArray = new Object[array.length + 1];
089:
090: System.arraycopy(array, 0, newArray, 0, array.length);
091:
092: newArray[newArray.length - 1] = obj;
093:
094: return newArray;
095: }
096:
097: public static Object[][] append(Object[][] array, Object[] obj) {
098: Object[][] newArray = new Object[array.length + 1][];
099:
100: System.arraycopy(array, 0, newArray, 0, array.length);
101:
102: newArray[newArray.length - 1] = obj;
103:
104: return newArray;
105: }
106:
107: public static Short[] append(Short[] array, Short obj) {
108: Short[] newArray = new Short[array.length + 1];
109:
110: System.arraycopy(array, 0, newArray, 0, array.length);
111:
112: newArray[newArray.length - 1] = obj;
113:
114: return newArray;
115: }
116:
117: public static String[] append(String[] array, String obj) {
118: String[] newArray = new String[array.length + 1];
119:
120: System.arraycopy(array, 0, newArray, 0, array.length);
121:
122: newArray[newArray.length - 1] = obj;
123:
124: return newArray;
125: }
126:
127: public static String[][] append(String[][] array, String[] obj) {
128: String[][] newArray = new String[array.length + 1][];
129:
130: System.arraycopy(array, 0, newArray, 0, array.length);
131:
132: newArray[newArray.length - 1] = obj;
133:
134: return newArray;
135: }
136:
137: public static Boolean[] append(Boolean[] array1, Boolean[] array2) {
138: Boolean[] newArray = new Boolean[array1.length + array2.length];
139:
140: System.arraycopy(array1, 0, newArray, 0, array1.length);
141: System.arraycopy(array2, 0, newArray, array1.length,
142: array2.length);
143:
144: return newArray;
145: }
146:
147: public static Double[] append(Double[] array1, Double[] array2) {
148: Double[] newArray = new Double[array1.length + array2.length];
149:
150: System.arraycopy(array1, 0, newArray, 0, array1.length);
151: System.arraycopy(array2, 0, newArray, array1.length,
152: array2.length);
153:
154: return newArray;
155: }
156:
157: public static Float[] append(Float[] array1, Float[] array2) {
158: Float[] newArray = new Float[array1.length + array2.length];
159:
160: System.arraycopy(array1, 0, newArray, 0, array1.length);
161: System.arraycopy(array2, 0, newArray, array1.length,
162: array2.length);
163:
164: return newArray;
165: }
166:
167: public static Integer[] append(Integer[] array1, Integer[] array2) {
168: Integer[] newArray = new Integer[array1.length + array2.length];
169:
170: System.arraycopy(array1, 0, newArray, 0, array1.length);
171: System.arraycopy(array2, 0, newArray, array1.length,
172: array2.length);
173:
174: return newArray;
175: }
176:
177: public static Long[] append(Long[] array1, Long[] array2) {
178: Long[] newArray = new Long[array1.length + array2.length];
179:
180: System.arraycopy(array1, 0, newArray, 0, array1.length);
181: System.arraycopy(array2, 0, newArray, array1.length,
182: array2.length);
183:
184: return newArray;
185: }
186:
187: public static Object[] append(Object[] array1, Object[] array2) {
188: Object[] newArray = new Object[array1.length + array2.length];
189:
190: System.arraycopy(array1, 0, newArray, 0, array1.length);
191: System.arraycopy(array2, 0, newArray, array1.length,
192: array2.length);
193:
194: return newArray;
195: }
196:
197: public static Object[][] append(Object[][] array1, Object[][] array2) {
198: Object[][] newArray = new Object[array1.length + array2.length][];
199:
200: System.arraycopy(array1, 0, newArray, 0, array1.length);
201: System.arraycopy(array2, 0, newArray, array1.length,
202: array2.length);
203:
204: return newArray;
205: }
206:
207: public static Short[] append(Short[] array1, Short[] array2) {
208: Short[] newArray = new Short[array1.length + array2.length];
209:
210: System.arraycopy(array1, 0, newArray, 0, array1.length);
211: System.arraycopy(array2, 0, newArray, array1.length,
212: array2.length);
213:
214: return newArray;
215: }
216:
217: public static String[] append(String[] array1, String[] array2) {
218: String[] newArray = new String[array1.length + array2.length];
219:
220: System.arraycopy(array1, 0, newArray, 0, array1.length);
221: System.arraycopy(array2, 0, newArray, array1.length,
222: array2.length);
223:
224: return newArray;
225: }
226:
227: public static String[][] append(String[][] array1, String[][] array2) {
228: String[][] newArray = new String[array1.length + array2.length][];
229:
230: System.arraycopy(array1, 0, newArray, 0, array1.length);
231: System.arraycopy(array2, 0, newArray, array1.length,
232: array2.length);
233:
234: return newArray;
235: }
236:
237: public static void combine(Object[] array1, Object[] array2,
238: Object[] combinedArray) {
239:
240: System.arraycopy(array1, 0, combinedArray, 0, array1.length);
241:
242: System.arraycopy(array2, 0, combinedArray, array1.length,
243: array2.length);
244: }
245:
246: public static boolean contains(boolean[] array, boolean value) {
247: if ((array == null) || (array.length == 0)) {
248: return false;
249: } else {
250: for (int i = 0; i < array.length; i++) {
251: if (value == array[i]) {
252: return true;
253: }
254: }
255:
256: return false;
257: }
258: }
259:
260: public static boolean contains(char[] array, char value) {
261: if ((array == null) || (array.length == 0)) {
262: return false;
263: } else {
264: for (int i = 0; i < array.length; i++) {
265: if (value == array[i]) {
266: return true;
267: }
268: }
269:
270: return false;
271: }
272: }
273:
274: public static boolean contains(double[] array, double value) {
275: if ((array == null) || (array.length == 0)) {
276: return false;
277: } else {
278: for (int i = 0; i < array.length; i++) {
279: if (value == array[i]) {
280: return true;
281: }
282: }
283:
284: return false;
285: }
286: }
287:
288: public static boolean contains(long[] array, long value) {
289: if ((array == null) || (array.length == 0)) {
290: return false;
291: } else {
292: for (int i = 0; i < array.length; i++) {
293: if (value == array[i]) {
294: return true;
295: }
296: }
297:
298: return false;
299: }
300: }
301:
302: public static boolean contains(int[] array, int value) {
303: if ((array == null) || (array.length == 0)) {
304: return false;
305: } else {
306: for (int i = 0; i < array.length; i++) {
307: if (value == array[i]) {
308: return true;
309: }
310: }
311:
312: return false;
313: }
314: }
315:
316: public static boolean contains(short[] array, short value) {
317: if ((array == null) || (array.length == 0)) {
318: return false;
319: } else {
320: for (int i = 0; i < array.length; i++) {
321: if (value == array[i]) {
322: return true;
323: }
324: }
325:
326: return false;
327: }
328: }
329:
330: public static boolean contains(Object[] array, Object value) {
331: if ((array == null) || (array.length == 0) || (value == null)) {
332: return false;
333: } else {
334: for (int i = 0; i < array.length; i++) {
335: if (value.equals(array[i])) {
336: return true;
337: }
338: }
339:
340: return false;
341: }
342: }
343:
344: public static String[] distinct(String[] array) {
345: return distinct(array, null);
346: }
347:
348: public static String[] distinct(String[] array,
349: Comparator comparator) {
350: if ((array == null) || (array.length == 0)) {
351: return array;
352: }
353:
354: Set set = null;
355:
356: if (comparator == null) {
357: set = new TreeSet();
358: } else {
359: set = new TreeSet(comparator);
360: }
361:
362: for (int i = 0; i < array.length; i++) {
363: Object obj = array[i];
364:
365: if (!set.contains(obj)) {
366: set.add(obj);
367: }
368: }
369:
370: return (String[]) set.toArray(new String[0]);
371: }
372:
373: public static int getLength(Object[] array) {
374: if (array == null) {
375: return 0;
376: } else {
377: return array.length;
378: }
379: }
380:
381: public static Object getValue(Object[] array, int pos) {
382: if ((array == null) || (array.length <= pos)) {
383: return null;
384: } else {
385: return array[pos];
386: }
387: }
388:
389: public static String[] removeByPrefix(String[] array, String prefix) {
390: List list = new ArrayList();
391:
392: for (int i = 0; i < array.length; i++) {
393: String s = array[i];
394:
395: if (!s.startsWith(prefix)) {
396: list.add(s);
397: }
398: }
399:
400: return (String[]) list.toArray(new String[list.size()]);
401: }
402:
403: public static Boolean[] toObjectArray(boolean[] array) {
404: Boolean[] objArray = new Boolean[array.length];
405:
406: for (int i = 0; i < array.length; i++) {
407: objArray[i] = Boolean.valueOf(array[i]);
408: }
409:
410: return objArray;
411: }
412:
413: public static Double[] toObjectArray(double[] array) {
414: Double[] objArray = new Double[array.length];
415:
416: for (int i = 0; i < array.length; i++) {
417: objArray[i] = new Double(array[i]);
418: }
419:
420: return objArray;
421: }
422:
423: public static Float[] toObjectArray(float[] array) {
424: Float[] objArray = new Float[array.length];
425:
426: for (int i = 0; i < array.length; i++) {
427: objArray[i] = new Float(array[i]);
428: }
429:
430: return objArray;
431: }
432:
433: public static Integer[] toObjectArray(int[] array) {
434: Integer[] objArray = new Integer[array.length];
435:
436: for (int i = 0; i < array.length; i++) {
437: objArray[i] = new Integer(array[i]);
438: }
439:
440: return objArray;
441: }
442:
443: public static Long[] toObjectArray(long[] array) {
444: Long[] objArray = new Long[array.length];
445:
446: for (int i = 0; i < array.length; i++) {
447: objArray[i] = new Long(array[i]);
448: }
449:
450: return objArray;
451: }
452:
453: public static Short[] toObjectArray(short[] array) {
454: Short[] objArray = new Short[array.length];
455:
456: for (int i = 0; i < array.length; i++) {
457: objArray[i] = new Short(array[i]);
458: }
459:
460: return objArray;
461: }
462:
463: }
|