1 package dynaop.util; 2 3 import java.lang.reflect.Array ; 4 import java.util.Iterator ; 5 6 18 public class ArrayObject { 19 20 private final Object array; 21 22 25 public ArrayObject(Object array) { 26 if (array == null) throw new NullPointerException (); 27 this.array = array; 28 } 29 30 public int hashCode() { 31 int hashCode = 0; 32 for (Iterator i = this.iterator(); i.hasNext();) { 33 Object element = i.next(); 34 if (element != null) 35 hashCode = hashCode * 37 + element.hashCode(); 36 } 37 return hashCode; 38 } 39 40 public boolean equals(Object 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 i1 = this.iterator(); 46 Iterator i2 = arrayObject.iterator(); 47 while (i1.hasNext()) { 48 Object o1 = i1.next(); 49 Object o2 = i2.next(); 50 if (!(o1==null ? o2==null : o1.equals(o2))) 51 return false; 52 } 53 return true; 54 } 55 56 public String toString() { 57 StringBuffer buffer = new StringBuffer (); 58 buffer.append('['); 59 for (Iterator 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 70 private Iterator iterator() { 71 final int length = this.length(); 72 return new Iterator () { 73 74 private int index = 0; 75 76 public boolean hasNext() { 77 return index < length; 78 } 79 80 public Object next() { 81 Object 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 (); 89 } 90 91 }; 92 } 93 94 97 private int length() { 98 return Array.getLength(this.array); 99 } 100 101 104 public static Object clone(Object array) { 105 int length = Array.getLength(array); 106 Object copy = Array.newInstance(array.getClass().getComponentType(), 107 length); 108 System.arraycopy(array, 0, copy, 0, length); 109 return copy; 110 } 111 } 112 | Popular Tags |