KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > element > AnyArrayStore


1 package jfun.yan.element;
2
3 import java.lang.reflect.Array JavaDoc;
4
5 import jfun.util.Misc;
6 import jfun.yan.util.Utils;
7
8
9 /**
10  * This class stores component instances into any array.
11  * For a primitive array such as int[], wrapper objects
12  * are automatically unboxed.
13  * <p>
14  * @author Ben Yu
15  * Nov 12, 2005 2:19:48 PM
16  */

17 public class AnyArrayStore<T> implements ElementStore<T> {
18   private final Object JavaDoc arr;
19   private final int begin;
20   private final int end;
21   private final Class JavaDoc<?> etype;
22   
23   /**
24    * To create an AnyArrayStore object.
25    * @param arr the array object
26    * @param begin the begin index of the array.
27    * Elements are stored from this index.
28    * @param end the non-inclusive end index.
29    * Elements cannot be stored at or after this index.
30    */

31   public AnyArrayStore(Object JavaDoc arr, int begin, int end) {
32     if(end > Array.getLength(arr))
33       throw new ArrayIndexOutOfBoundsException JavaDoc(end);
34     if(!arr.getClass().isArray())
35       throw new IllegalArgumentException JavaDoc("array expected, "
36           + Utils.getObjTypeName(arr, null) + " encountered.");
37     this.arr = arr;
38     this.begin = begin;
39     this.end = end;
40     this.etype = arr.getClass().getComponentType();
41   }
42   /**
43    * To create an AnyArrayStore object.
44    * @param arr the array object
45    * @param begin the begin index of the array.
46    * Elements are stored from this index.
47    *
48    */

49   public AnyArrayStore(Object JavaDoc arr, int begin){
50     this.arr = arr;
51     this.begin = begin;
52     this.end = Array.getLength(arr);
53     this.etype = arr.getClass().getComponentType();
54   }
55   /**
56    * To create an AnyArrayStore object.
57    * @param arr the array object
58    */

59   public AnyArrayStore(Object JavaDoc arr){
60     this(arr, 0);
61   }
62   /**
63    * Get the array object that stores the elements.
64    */

65   public Object JavaDoc getArrayObject() {
66     return arr;
67   }
68
69   /**
70    * Get the begin index.
71    */

72   public int getBegin() {
73     return begin;
74   }
75
76   /**
77    * Get the non-inclusive end index.
78    */

79   public int getEnd() {
80     return end;
81   }
82
83   public void storeElement(int ind, T obj) {
84     Array.set(arr, getIndex(ind), obj);
85   }
86   /*
87   public void checkElement(int ind, Class type) {
88     getIndex(ind);
89     if(type!=null && !ReflectionUtil.isAssignableFrom(etype, type)){
90       throw new TypeMismatchException(etype, type,
91           "type mismatch for #"+ind+" element.");
92     }
93   }*/

94   /**
95    * Map an element to the real index on the array.
96    * @param ind the index.
97    * @return the real index on the physical array.
98    * @throws ArrayIndexOutOfBoundsException if the index is not within the [begin,end) range.
99    */

100   protected final int getIndex(int ind){
101     final int k = ind+begin;
102     if(k >= end){
103       throw new ArrayIndexOutOfBoundsException JavaDoc(k);
104     }
105     return k;
106   }
107   public boolean equals(Object JavaDoc obj) {
108     if(obj instanceof AnyArrayStore){
109       final AnyArrayStore other = (AnyArrayStore)obj;
110       return arr==other.arr&&begin==other.begin&&end==other.end;
111     }
112     else return false;
113   }
114   public int hashCode() {
115     return (begin*31+end)*31+System.identityHashCode(arr);
116   }
117   public String JavaDoc toString() {
118     return Misc.getTypeName(arr.getClass());
119   }
120 }
121
Popular Tags