001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ArrayUtils.java 3846 2007-07-11 11:33:53Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: /**
011: * General purpose class containing common array manipulation methods.
012: *
013: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
014: * @version $Revision: 3846 $
015: * @since 1.0
016: */
017: import java.util.logging.Logger;
018:
019: import com.uwyn.rife.datastructures.EnumClass;
020: import com.uwyn.rife.site.ConstrainedProperty;
021:
022: public class ArrayUtils {
023: public static class ArrayType extends EnumClass<String> {
024: public static final ArrayType NO_ARRAY = new ArrayType(
025: "NO_ARRAY");
026: public static final ArrayType OBJECT_ARRAY = new ArrayType(
027: "OBJECT_ARRAY");
028: public static final ArrayType BYTE_ARRAY = new ArrayType(
029: "BYTE_ARRAY");
030: public static final ArrayType BOOLEAN_ARRAY = new ArrayType(
031: "BOOLEAN_ARRAY");
032: public static final ArrayType CHAR_ARRAY = new ArrayType(
033: "CHAR_ARRAY");
034: public static final ArrayType SHORT_ARRAY = new ArrayType(
035: "SHORT_ARRAY");
036: public static final ArrayType INT_ARRAY = new ArrayType(
037: "INT_ARRAY");
038: public static final ArrayType LONG_ARRAY = new ArrayType(
039: "LONG_ARRAY");
040: public static final ArrayType FLOAT_ARRAY = new ArrayType(
041: "FLOAT_ARRAY");
042: public static final ArrayType DOUBLE_ARRAY = new ArrayType(
043: "DOUBLE_ARRAY");
044:
045: ArrayType(String identifier) {
046: super (identifier);
047: }
048:
049: public static ArrayType getArrayType(String name) {
050: return getMember(ArrayType.class, name);
051: }
052: }
053:
054: public static ArrayType getArrayType(Object object) {
055: String classname = object.getClass().getName();
056:
057: // check if it's an array
058: if ('[' == classname.charAt(0)) {
059: for (int position = 1; position < classname.length(); position++) {
060: if ('[' == classname.charAt(position)) {
061: continue;
062: }
063:
064: switch (classname.charAt(position)) {
065: case 'L':
066: return ArrayType.OBJECT_ARRAY;
067: case 'Z':
068: return ArrayType.BOOLEAN_ARRAY;
069: case 'B':
070: return ArrayType.BYTE_ARRAY;
071: case 'C':
072: return ArrayType.CHAR_ARRAY;
073: case 'S':
074: return ArrayType.SHORT_ARRAY;
075: case 'I':
076: return ArrayType.INT_ARRAY;
077: case 'J':
078: return ArrayType.LONG_ARRAY;
079: case 'F':
080: return ArrayType.FLOAT_ARRAY;
081: case 'D':
082: return ArrayType.DOUBLE_ARRAY;
083: ///CLOVER:OFF
084: default:
085: Logger.getLogger("com.uwyn.rife.tools").severe(
086: "Unknown primitive array class: "
087: + classname);
088: return null;
089: ///CLOVER:ON
090: }
091: }
092: return null;
093: }
094:
095: return ArrayType.NO_ARRAY;
096: }
097:
098: /**
099: * Convert an <code>Object</code> to a textual representation in a
100: * <code>String</code> array.
101: * <p>
102: * Note that array of type byte[] are explicitely not converted since that
103: * would result in many binary data to create OutOfMemoryError exceptions.
104: *
105: * @param source The <code>Object</code> to convert.
106: * @param constrainedProperty The <code>ConstrainedProperty</code> that gives more information about the source, can be <code>null</code>.
107: *
108: * @return The resulting <code>String</code> array; or
109: * <p>
110: * <code>null</code> if <code>source</code> is <code>null</code>.
111: *
112: * @since 1.0
113: */
114: public static String[] createStringArray(Object source,
115: ConstrainedProperty constrainedProperty) {
116: if (null == source) {
117: return null;
118: }
119:
120: String[] result = null;
121:
122: ArrayType type = getArrayType(source);
123:
124: if (type == ArrayType.NO_ARRAY) {
125: result = new String[] { BeanUtils.formatPropertyValue(
126: source, constrainedProperty) };
127: } else if (type == ArrayType.BYTE_ARRAY) {
128: // explicitely don't convert byte arrays since they are often used
129: // to store binary data and converting them to a string array
130: // would result in an OutOfMemoryError exception easily
131: result = null;
132: } else if (type == ArrayType.OBJECT_ARRAY) {
133: result = ArrayUtils.createStringArray((Object[]) source);
134: } else if (type == ArrayType.BOOLEAN_ARRAY) {
135: result = ArrayUtils.createStringArray((boolean[]) source);
136: } else if (type == ArrayType.CHAR_ARRAY) {
137: result = ArrayUtils.createStringArray((char[]) source);
138: } else if (type == ArrayType.SHORT_ARRAY) {
139: result = ArrayUtils.createStringArray((short[]) source);
140: } else if (type == ArrayType.INT_ARRAY) {
141: result = ArrayUtils.createStringArray((int[]) source);
142: } else if (type == ArrayType.LONG_ARRAY) {
143: result = ArrayUtils.createStringArray((long[]) source);
144: } else if (type == ArrayType.FLOAT_ARRAY) {
145: result = ArrayUtils.createStringArray((float[]) source);
146: } else if (type == ArrayType.DOUBLE_ARRAY) {
147: result = ArrayUtils.createStringArray((double[]) source);
148: }
149:
150: return result;
151: }
152:
153: public static String[] createStringArray(Object[] array) {
154: if (null == array) {
155: return null;
156: }
157:
158: String[] new_array = new String[array.length];
159:
160: for (int i = 0; i < array.length; i++) {
161: new_array[i] = String.valueOf(array[i]);
162: }
163:
164: return new_array;
165: }
166:
167: public static String[] createStringArray(boolean[] array) {
168: if (null == array) {
169: return null;
170: }
171:
172: String[] new_array = new String[array.length];
173:
174: for (int i = 0; i < array.length; i++) {
175: new_array[i] = String.valueOf(array[i]);
176: }
177:
178: return new_array;
179: }
180:
181: public static String[] createStringArray(byte[] array) {
182: if (null == array) {
183: return null;
184: }
185:
186: String[] new_array = new String[array.length];
187:
188: for (int i = 0; i < array.length; i++) {
189: new_array[i] = String.valueOf(array[i]);
190: }
191:
192: return new_array;
193: }
194:
195: public static String[] createStringArray(char[] array) {
196: if (null == array) {
197: return null;
198: }
199:
200: String[] new_array = new String[array.length];
201:
202: for (int i = 0; i < array.length; i++) {
203: new_array[i] = String.valueOf(array[i]);
204: }
205:
206: return new_array;
207: }
208:
209: public static String[] createStringArray(short[] array) {
210: if (null == array) {
211: return null;
212: }
213:
214: String[] new_array = new String[array.length];
215:
216: for (int i = 0; i < array.length; i++) {
217: new_array[i] = String.valueOf(array[i]);
218: }
219:
220: return new_array;
221: }
222:
223: public static String[] createStringArray(int[] array) {
224: if (null == array) {
225: return null;
226: }
227:
228: String[] new_array = new String[array.length];
229:
230: for (int i = 0; i < array.length; i++) {
231: new_array[i] = String.valueOf(array[i]);
232: }
233:
234: return new_array;
235: }
236:
237: public static String[] createStringArray(long[] array) {
238: if (null == array) {
239: return null;
240: }
241:
242: String[] new_array = new String[array.length];
243:
244: for (int i = 0; i < array.length; i++) {
245: new_array[i] = String.valueOf(array[i]);
246: }
247:
248: return new_array;
249: }
250:
251: public static String[] createStringArray(float[] array) {
252: if (null == array) {
253: return null;
254: }
255:
256: String[] new_array = new String[array.length];
257:
258: for (int i = 0; i < array.length; i++) {
259: new_array[i] = String.valueOf(array[i]);
260: }
261:
262: return new_array;
263: }
264:
265: public static String[] createStringArray(double[] array) {
266: if (null == array) {
267: return null;
268: }
269:
270: String[] new_array = new String[array.length];
271:
272: for (int i = 0; i < array.length; i++) {
273: new_array[i] = String.valueOf(array[i]);
274: }
275:
276: return new_array;
277: }
278:
279: public static boolean[] createBooleanArray(Object[] array) {
280: if (null == array) {
281: return null;
282: }
283:
284: boolean[] new_array = new boolean[0];
285:
286: boolean converted_boolean = false;
287: for (Object element : array) {
288: if (element != null) {
289: converted_boolean = Boolean.valueOf(
290: String.valueOf(element)).booleanValue();
291: new_array = join(new_array, converted_boolean);
292: }
293: }
294:
295: return new_array;
296: }
297:
298: public static byte[] createByteArray(Object[] array) {
299: if (null == array) {
300: return null;
301: }
302:
303: byte[] new_array = new byte[0];
304:
305: byte converted_byte = -1;
306: for (Object element : array) {
307: try {
308: if (element != null) {
309: converted_byte = Byte.parseByte(String
310: .valueOf(element));
311: new_array = join(new_array, converted_byte);
312: }
313: } catch (NumberFormatException e) {
314: continue;
315: }
316: }
317:
318: return new_array;
319: }
320:
321: public static char[] createCharArray(Object[] array) {
322: if (null == array) {
323: return null;
324: }
325:
326: char[] new_array = new char[0];
327:
328: char converted_char = '\u0000';
329: for (Object element : array) {
330: if (element != null) {
331: String string_value = String.valueOf(element);
332: if (0 == string_value.length()
333: || string_value.length() > 1) {
334: continue;
335: }
336: converted_char = string_value.charAt(0);
337: new_array = join(new_array, converted_char);
338: }
339: }
340:
341: return new_array;
342: }
343:
344: public static short[] createShortArray(Object[] array) {
345: if (null == array) {
346: return null;
347: }
348:
349: short[] new_array = new short[0];
350:
351: short converted_short = -1;
352: for (Object element : array) {
353: try {
354: if (element != null) {
355: converted_short = Short.parseShort(String
356: .valueOf(element));
357: new_array = join(new_array, converted_short);
358: }
359: } catch (NumberFormatException e) {
360: continue;
361: }
362: }
363:
364: return new_array;
365: }
366:
367: public static int[] createIntArray(Object[] array) {
368: if (null == array) {
369: return null;
370: }
371:
372: int[] new_array = new int[0];
373:
374: int converted_int = -1;
375: for (Object element : array) {
376: try {
377: if (element != null) {
378: converted_int = Integer.parseInt(String
379: .valueOf(element));
380: new_array = join(new_array, converted_int);
381: }
382: } catch (NumberFormatException e) {
383: continue;
384: }
385: }
386:
387: return new_array;
388: }
389:
390: public static long[] createLongArray(Object[] array) {
391: if (null == array) {
392: return null;
393: }
394:
395: long[] new_array = new long[0];
396:
397: long converted_long = -1;
398: for (Object element : array) {
399: try {
400: if (element != null) {
401: converted_long = Long.parseLong(String
402: .valueOf(element));
403: new_array = join(new_array, converted_long);
404: }
405: } catch (NumberFormatException e) {
406: continue;
407: }
408: }
409:
410: return new_array;
411: }
412:
413: public static float[] createFloatArray(Object[] array) {
414: if (null == array) {
415: return null;
416: }
417:
418: float[] new_array = new float[0];
419:
420: float converted_float = -1;
421: for (Object element : array) {
422: try {
423: if (element != null) {
424: converted_float = Float.parseFloat(String
425: .valueOf(element));
426: new_array = join(new_array, converted_float);
427: }
428: } catch (NumberFormatException e) {
429: continue;
430: }
431: }
432:
433: return new_array;
434: }
435:
436: public static double[] createDoubleArray(Object[] array) {
437: if (null == array) {
438: return null;
439: }
440:
441: double[] new_array = new double[0];
442:
443: double converted_double = -1;
444: for (Object element : array) {
445: try {
446: if (element != null) {
447: converted_double = Double.parseDouble(String
448: .valueOf(element));
449: new_array = join(new_array, converted_double);
450: }
451: } catch (NumberFormatException e) {
452: continue;
453: }
454: }
455:
456: return new_array;
457: }
458:
459: public static String[] join(String[] first, String[] second) {
460: if (null == first && null == second) {
461: return null;
462: }
463: if (null == first) {
464: return second;
465: }
466: if (null == second) {
467: return first;
468: }
469:
470: String[] new_array = new String[first.length + second.length];
471:
472: System.arraycopy(first, 0, new_array, 0, first.length);
473: System.arraycopy(second, 0, new_array, first.length,
474: second.length);
475:
476: return new_array;
477: }
478:
479: public static String[] join(String[] first, String second) {
480: if (null == first && null == second) {
481: return null;
482: }
483: if (null == first) {
484: return new String[] { second };
485: }
486: if (null == second) {
487: return first;
488: }
489:
490: String[] new_array = new String[first.length + 1];
491:
492: System.arraycopy(first, 0, new_array, 0, first.length);
493: new_array[first.length] = second;
494:
495: return new_array;
496: }
497:
498: public static byte[] join(byte[] first, byte[] second) {
499: if (null == first && null == second) {
500: return null;
501: }
502: if (null == first) {
503: return second;
504: }
505: if (null == second) {
506: return first;
507: }
508:
509: byte[] new_array = new byte[first.length + second.length];
510:
511: System.arraycopy(first, 0, new_array, 0, first.length);
512: System.arraycopy(second, 0, new_array, first.length,
513: second.length);
514:
515: return new_array;
516: }
517:
518: public static byte[] join(byte[] first, byte second) {
519: if (null == first) {
520: return new byte[] { second };
521: }
522:
523: byte[] new_array = new byte[first.length + 1];
524:
525: System.arraycopy(first, 0, new_array, 0, first.length);
526: new_array[first.length] = second;
527:
528: return new_array;
529: }
530:
531: public static char[] join(char[] first, char[] second) {
532: if (null == first && null == second) {
533: return null;
534: }
535: if (null == first) {
536: return second;
537: }
538: if (null == second) {
539: return first;
540: }
541:
542: char[] new_array = new char[first.length + second.length];
543:
544: System.arraycopy(first, 0, new_array, 0, first.length);
545: System.arraycopy(second, 0, new_array, first.length,
546: second.length);
547:
548: return new_array;
549: }
550:
551: public static char[] join(char[] first, char second) {
552: if (null == first) {
553: return new char[] { second };
554: }
555:
556: char[] new_array = new char[first.length + 1];
557:
558: System.arraycopy(first, 0, new_array, 0, first.length);
559: new_array[first.length] = second;
560:
561: return new_array;
562: }
563:
564: public static short[] join(short[] first, short[] second) {
565: if (null == first && null == second) {
566: return null;
567: }
568: if (null == first) {
569: return second;
570: }
571: if (null == second) {
572: return first;
573: }
574:
575: short[] new_array = new short[first.length + second.length];
576:
577: System.arraycopy(first, 0, new_array, 0, first.length);
578: System.arraycopy(second, 0, new_array, first.length,
579: second.length);
580:
581: return new_array;
582: }
583:
584: public static short[] join(short[] first, short second) {
585: if (null == first) {
586: return new short[] { second };
587: }
588:
589: short[] new_array = new short[first.length + 1];
590:
591: System.arraycopy(first, 0, new_array, 0, first.length);
592: new_array[first.length] = second;
593:
594: return new_array;
595: }
596:
597: public static int[] join(int[] first, int[] second) {
598: if (null == first && null == second) {
599: return null;
600: }
601: if (null == first) {
602: return second;
603: }
604: if (null == second) {
605: return first;
606: }
607:
608: int[] new_array = new int[first.length + second.length];
609:
610: System.arraycopy(first, 0, new_array, 0, first.length);
611: System.arraycopy(second, 0, new_array, first.length,
612: second.length);
613:
614: return new_array;
615: }
616:
617: public static int[] join(int[] first, int second) {
618: if (null == first) {
619: return new int[] { second };
620: }
621:
622: int[] new_array = new int[first.length + 1];
623:
624: System.arraycopy(first, 0, new_array, 0, first.length);
625: new_array[first.length] = second;
626:
627: return new_array;
628: }
629:
630: public static long[] join(long[] first, long[] second) {
631: if (null == first && null == second) {
632: return null;
633: }
634: if (null == first) {
635: return second;
636: }
637: if (null == second) {
638: return first;
639: }
640:
641: long[] new_array = new long[first.length + second.length];
642:
643: System.arraycopy(first, 0, new_array, 0, first.length);
644: System.arraycopy(second, 0, new_array, first.length,
645: second.length);
646:
647: return new_array;
648: }
649:
650: public static long[] join(long[] first, long second) {
651: if (null == first) {
652: return new long[] { second };
653: }
654:
655: long[] new_array = new long[first.length + 1];
656:
657: System.arraycopy(first, 0, new_array, 0, first.length);
658: new_array[first.length] = second;
659:
660: return new_array;
661: }
662:
663: public static float[] join(float[] first, float[] second) {
664: if (null == first && null == second) {
665: return null;
666: }
667: if (null == first) {
668: return second;
669: }
670: if (null == second) {
671: return first;
672: }
673:
674: float[] new_array = new float[first.length + second.length];
675:
676: System.arraycopy(first, 0, new_array, 0, first.length);
677: System.arraycopy(second, 0, new_array, first.length,
678: second.length);
679:
680: return new_array;
681: }
682:
683: public static float[] join(float[] first, float second) {
684: if (null == first) {
685: return new float[] { second };
686: }
687:
688: float[] new_array = new float[first.length + 1];
689:
690: System.arraycopy(first, 0, new_array, 0, first.length);
691: new_array[first.length] = second;
692:
693: return new_array;
694: }
695:
696: public static double[] join(double[] first, double[] second) {
697: if (null == first && null == second) {
698: return null;
699: }
700: if (null == first) {
701: return second;
702: }
703: if (null == second) {
704: return first;
705: }
706:
707: double[] new_array = new double[first.length + second.length];
708:
709: System.arraycopy(first, 0, new_array, 0, first.length);
710: System.arraycopy(second, 0, new_array, first.length,
711: second.length);
712:
713: return new_array;
714: }
715:
716: public static double[] join(double[] first, double second) {
717: if (null == first) {
718: return new double[] { second };
719: }
720:
721: double[] new_array = new double[first.length + 1];
722:
723: System.arraycopy(first, 0, new_array, 0, first.length);
724: new_array[first.length] = second;
725:
726: return new_array;
727: }
728:
729: public static boolean[] join(boolean[] first, boolean[] second) {
730: if (null == first && null == second) {
731: return null;
732: }
733: if (null == first) {
734: return second;
735: }
736: if (null == second) {
737: return first;
738: }
739:
740: boolean[] new_array = new boolean[first.length + second.length];
741:
742: System.arraycopy(first, 0, new_array, 0, first.length);
743: System.arraycopy(second, 0, new_array, first.length,
744: second.length);
745:
746: return new_array;
747: }
748:
749: public static boolean[] join(boolean[] first, boolean second) {
750: if (null == first) {
751: return new boolean[] { second };
752: }
753:
754: boolean[] new_array = new boolean[first.length + 1];
755:
756: System.arraycopy(first, 0, new_array, 0, first.length);
757: new_array[first.length] = second;
758:
759: return new_array;
760: }
761: }
|