KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBArray


1 package com.ca.commons.cbutil;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Vector JavaDoc;
5
6
7 /**
8  * Utility class for array operations. Not particularly
9  * fast - most operations implemented as linear searches.
10  * XXX most of these may be redundant - see java 'util.Array' class...
11  *
12  * @author Chris Betts
13  */

14
15
16 public class CBArray
17 {
18
19     /**
20      * Returns the list of elements that appear at least once
21      * in one of the passed arrays. An element appearing in
22      * both arrays only appears once in the final array.
23      * Behaviour is undefined if Elements are repeated within
24      * an array; they may be trimmed, or not.
25      *
26      * @param a the first array to unionise
27      * @param b the second array to unionise.
28      * @return the resultant union.
29      */

30
31     public static Object JavaDoc[] union(Object JavaDoc[] a, Object JavaDoc[] b)
32     {
33 //System.out.println("union starting; lengths " + a.length + " and " + b.length);
34
Object JavaDoc[] temp = new Object JavaDoc[a.length + b.length];
35         int i = 0;
36         for (i = 0; i < a.length; i++) temp[i] = a[i];
37         for (int j = 0; j < b.length; j++)
38             if (contains(a, b[j]) == false) // no duplicates
39
temp[i++] = b[j];
40                 
41 //for (int k=0; k<i; k++)
42
// System.out.println(" " + k + " " + temp[k].toString());
43

44         if (a.length + b.length == i)
45             return temp;
46         else
47         {
48             Object JavaDoc[] ret = new Object JavaDoc[i];
49             for (int k = 0; k < i; k++) ret[k] = temp[k];
50
51             return ret;
52         }
53     }
54
55     /**
56      * Returns the list of elements that appear in a but
57      * not in b.
58      * Behaviour is undefined if Elements are repeated within
59      * an array; they may be trimmed, or not.
60      *
61      * @param a the primary array
62      * @param b the array to 'subtract'.
63      * @return the resultant difference.
64      */

65
66     public static Object JavaDoc[] difference(Object JavaDoc[] a, Object JavaDoc[] b)
67     {
68 //System.out.println("difference starting; lengths " + a.length + " and " + b.length);
69
if (a == null || b == null) return null;
70         if (a.length == 0) return ((Object JavaDoc[]) a.clone());
71         if (b.length == 0) return ((Object JavaDoc[]) a.clone());
72
73         Object JavaDoc[] temp = new Object JavaDoc[a.length];
74         int len = 0;
75         for (int i = 0; i < a.length; i++)
76             if (contains(b, a[i]) == false)
77                 temp[len++] = a[i];
78
79         Object JavaDoc ret[] = new Object JavaDoc[len];
80         for (int i = 0; i < len; i++) ret[i] = temp[i];
81         
82 //for (int k=0; k<len; k++)
83
// System.out.println(" " + k + " " + ret[k].toString() + " (" + ret.getClass().toString() + ")");
84

85         return ret;
86     }
87
88     /**
89      * Returns the list of elements that appear in both
90      * of the passed arrays. An element appearing in
91      * both arrays only appears once in the final array.
92      * Behaviour is undefined if Elements are repeated within
93      * an array; they may be trimmed, or not.
94      *
95      * @param a the first array to unionise
96      * @param b the second array to unionise.
97      * @return the resultant union.
98      */

99
100     public static Object JavaDoc[] intersection(Object JavaDoc[] a, Object JavaDoc[] b)
101     {
102 //System.out.println("intersection starting; lengths " + a.length + " and " + b.length);
103
if (a == null || b == null) return null;
104         if (a.length == 0) return ((Object JavaDoc[]) a.clone());
105         if (b.length == 0) return ((Object JavaDoc[]) b.clone());
106
107         if (a.length > b.length) // make a the smallest array
108
{
109             Object JavaDoc[] c = a;
110             a = b;
111             b = c;
112         }
113
114         Object JavaDoc[] temp = new Object JavaDoc[a.length];
115
116         int len = 0;
117         for (int i = 0; i < a.length; i++)
118             if (contains(b, a[i]) == true) temp[len++] = a[i];
119
120         Object JavaDoc[] ret = new Object JavaDoc[len];
121         for (int i = 0; i < len; i++) ret[i] = temp[i];
122         return ret;
123     }
124
125     /**
126      * Returns true if arrays have the same elements, in the same
127      * order.
128      */

129
130     public static boolean isOrderedEqual(Object JavaDoc[] a, Object JavaDoc[] b)
131     {
132         if (a == null && b == null) return true;
133         if (a == null || b == null) return false;
134         if (a.length != b.length) return false;
135         for (int i = 0; i < a.length; i++)
136             if (a[i].equals(b[i]) == false) return false;
137         return true;
138     }
139
140     /**
141      * Returns true if arrays have the same elements, in any
142      * order. Does *not* check for different numbers of
143      * repeating elements - i.e. {A,A,B} == {A,B,B} is true.
144      */

145     public static boolean isUnorderedEqual(Object JavaDoc[] a, Object JavaDoc[] b)
146     {
147         if (a == null && b == null) return true;
148         if (a == null || b == null) return false;
149         if (a.length != b.length) return false;
150         for (int i = 0; i < a.length; i++)
151             if (contains(b, a[i]) == false) return false;
152         return true;
153     }
154
155     /**
156      * Returns true if the passed object is not null,
157      * and is equal to (at least one) element of the array.
158      *
159      * @param array the array that may contain the object.
160      * @param o the object to check the existence of.
161      */

162     public static boolean contains(Object JavaDoc array[], Object JavaDoc o)
163     {
164         if (o == null) return false;
165         for (int i = 0; i < array.length; i++)
166             if (o.equals(array[i])) return true;
167         return false;
168     }
169
170     /**
171      * Prints an array, using 'toString()' on each component.
172      */

173
174     public static void print(Object JavaDoc array[])
175     {
176         for (int i = 0; i < array.length; i++)
177             System.out.println(i + ": " + array[i].toString());
178     }
179
180
181     public static Object JavaDoc[] enumerationToArray(Enumeration JavaDoc e)
182     {
183         Vector JavaDoc v = new Vector JavaDoc();
184         while (e.hasMoreElements()) v.add(e.nextElement());
185         return v.toArray();
186     }
187
188     public static Enumeration JavaDoc arrayToEnumeration(Object JavaDoc[] o)
189     {
190         Vector JavaDoc v = new Vector JavaDoc(o.length);
191         for (int i = 0; i < o.length; i++) v.add(o[i]);
192         return v.elements();
193     }
194
195     public static Object JavaDoc[] trimNulls(Object JavaDoc[] o)
196     {
197         Vector JavaDoc v = new Vector JavaDoc(o.length);
198         for (int i = 0; i < o.length; i++)
199             if (o[i] != null)
200                 v.add(o[i]);
201         return v.toArray();
202     }
203 }
Popular Tags