KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > util > ArrayObject


1 package dynaop.util;
2
3 import java.lang.reflect.Array JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 /**
7  * Implements <code>hashCode()</code> and <code>equals()</code>
8  * functionality for an array. Useful when keying a <code>Map</code>
9  * with arrays, comparing two arrays for equality, or implementing toString()
10  * for an array.
11  *
12  * <p>Does not make a defensive copy; behavior
13  * changes with associated array. Does not account for circularly
14  * linked arrays.
15  *
16  * @author Bob Lee (crazybob@crazybob.org)
17  */

18 public class ArrayObject {
19     
20     private final Object JavaDoc array;
21     
22     /**
23      * Constructs a object for a given array.
24      */

25     public ArrayObject(Object JavaDoc array) {
26         if (array == null) throw new NullPointerException JavaDoc();
27         this.array = array;
28     }
29
30     public int hashCode() {
31         int hashCode = 0;
32         for (Iterator JavaDoc i = this.iterator(); i.hasNext();) {
33             Object JavaDoc element = i.next();
34             if (element != null)
35                 hashCode = hashCode * 37 + element.hashCode();
36         }
37         return hashCode;
38     }
39
40     public boolean equals(Object JavaDoc o) {
41         if (o == this) return true;
42         if (!(o instanceof ArrayObject)) return false;
43         ArrayObject arrayObject = (ArrayObject) o;
44         if (this.length() != arrayObject.length()) return false;
45         Iterator JavaDoc i1 = this.iterator();
46         Iterator JavaDoc i2 = arrayObject.iterator();
47         while (i1.hasNext()) {
48             Object JavaDoc o1 = i1.next();
49             Object JavaDoc o2 = i2.next();
50             if (!(o1==null ? o2==null : o1.equals(o2)))
51                 return false;
52         }
53         return true;
54     }
55
56     public String JavaDoc toString() {
57         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
58         buffer.append('[');
59         for (Iterator JavaDoc i = this.iterator(); i.hasNext();) {
60             buffer.append(i.next());
61             if (i.hasNext()) buffer.append(", ");
62         }
63         buffer.append(']');
64         return buffer.toString();
65     }
66     
67     /**
68      * Constructs element iterator. Recursively wraps nested arrays.
69      */

70     private Iterator JavaDoc iterator() {
71         final int length = this.length();
72         return new Iterator JavaDoc() {
73
74             private int index = 0;
75
76             public boolean hasNext() {
77                 return index < length;
78             }
79
80             public Object JavaDoc next() {
81                 Object JavaDoc element = Array.get(ArrayObject.this.array, index++);
82                 return (element == null) ? null :
83                        (element.getClass().isArray()) ?
84                             new ArrayObject(element) : element;
85             }
86
87             public void remove() {
88                 throw new UnsupportedOperationException JavaDoc();
89             }
90
91         };
92     }
93
94     /**
95      * Gets length of enclosed array.
96      */

97     private int length() {
98         return Array.getLength(this.array);
99     }
100     
101     /**
102      * Creates a defensive copy of an array.
103      */

104     public static Object JavaDoc clone(Object JavaDoc array) {
105         int length = Array.getLength(array);
106         Object JavaDoc copy = Array.newInstance(array.getClass().getComponentType(),
107                 length);
108         System.arraycopy(array, 0, copy, 0, length);
109         return copy;
110     }
111 }
112
Popular Tags