KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > ArrayHelper


1 /*
2  * Convenience methods for working with Java arrays.
3  * Copyright (C) 2005 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18
19 package com.Ostermiller.util;
20
21 import java.lang.reflect.*;
22 import java.io.*;
23
24 /**
25  * Convenience methods for working with Java arrays.
26  * More information about this class is available from <a target="_top" HREF=
27  * "http://ostermiller.org/utils/ArrayHelper.html">ostermiller.org</a>.
28  *
29  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
30  * @since ostermillerutils 1.06.00
31  */

32 public final class ArrayHelper {
33
34     /**
35      * Concatenates the given arrays into a single long array.
36      * The returned array will be of the common super type of the
37      * two given arrays.
38      * <p>
39      * For example it can be called like this:<br>
40      * <pre>
41      * String[] arr = (String[])ArrayHelper.cat(new String[]{"one","two"}, new String[]{"three","four"});
42      * </pre>
43      *
44      * @param arr1 first array
45      * @param arr2 second array
46      * @return an array whose length is the sum of the given array's lengths and contains all the elements in the given arrays.
47      * @since ostermillerutils 1.06.00
48      */

49     public static Object JavaDoc[] cat(Object JavaDoc[] arr1, Object JavaDoc[] arr2){
50         // Use reflection to find the super class of both arrays
51
Class JavaDoc commonSuperClass = Object JavaDoc.class;
52         boolean foundcommonSuperClass=false;
53         for (Class JavaDoc c1 = arr1.getClass().getComponentType(); !foundcommonSuperClass && !c1.equals(Object JavaDoc.class); c1 = c1.getSuperclass()){
54             for (Class JavaDoc c2 = arr2.getClass().getComponentType(); !foundcommonSuperClass && !c2.equals(Object JavaDoc.class); c2 = c2.getSuperclass()){
55                 if (c2.equals(c1)){
56                     foundcommonSuperClass = true;
57                     commonSuperClass = c1;
58                 }
59             }
60         }
61         // Create a new array of the correct type
62
Object JavaDoc[] result = (Object JavaDoc[])Array.newInstance(commonSuperClass, arr1.length + arr2.length);
63         // Copy the two arrays into the large array
64
System.arraycopy(arr1, 0, result, 0, arr1.length);
65         System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
66         return result;
67     }
68
69     /**
70      * Prints out a comma separated list of all the objects
71      * in the array on a single line in CSV format.
72      *
73      * @param arr Array to print in comma separated value format
74      * @since ostermillerutils 1.06.00
75      */

76     public static void print(Object JavaDoc[] arr){
77         try {
78             CSVPrinter csvp = new CSVPrinter(System.out);
79             for (int i=0; i<arr.length; i++){
80                 csvp.write(arr[i].toString());
81             }
82             csvp.writeln();
83             csvp.flush();
84         } catch (IOException iox){
85             iox.printStackTrace(System.err);
86         }
87     }
88
89     /**
90      * Tests two arrays to see if the arrays are equal.
91      * Two arrays will be equal only if they are the same length
92      * and contain objects that are equal in the same order.
93      *
94      * @param arr1 first array
95      * @param arr2 second array
96      * @since ostermillerutils 1.06.00
97      */

98     public static boolean equal(Object JavaDoc[] arr1, Object JavaDoc[] arr2){
99         if (arr1 == null && arr2 == null) return true;
100         if (arr1 == null || arr2 == null) return false;
101         if (arr1.length != arr2.length) return false;
102         for (int i=0; i<arr1.length; i++){
103             if (!equalObjects(arr1[i], arr2[i])) return false;
104         }
105         return true;
106     }
107
108     /**
109      * Tests if the two objects are equal
110      *
111      * @param o1 first object
112      * @param o2 second object
113      * @since ostermillerutils 1.06.00
114      */

115     private static boolean equalObjects(Object JavaDoc o1, Object JavaDoc o2){
116         if (o1 == null && o2 == null) return true;
117         if (o1 == null || o2 == null) return false;
118         return o1.equals(o2);
119     }
120 }
121
Popular Tags