1 package jfun.yan.element; 2 3 import java.lang.reflect.Array ; 4 5 import jfun.util.Misc; 6 import jfun.yan.util.Utils; 7 8 9 17 public class AnyArrayStore<T> implements ElementStore<T> { 18 private final Object arr; 19 private final int begin; 20 private final int end; 21 private final Class <?> etype; 22 23 31 public AnyArrayStore(Object arr, int begin, int end) { 32 if(end > Array.getLength(arr)) 33 throw new ArrayIndexOutOfBoundsException (end); 34 if(!arr.getClass().isArray()) 35 throw new IllegalArgumentException ("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 49 public AnyArrayStore(Object 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 59 public AnyArrayStore(Object arr){ 60 this(arr, 0); 61 } 62 65 public Object getArrayObject() { 66 return arr; 67 } 68 69 72 public int getBegin() { 73 return begin; 74 } 75 76 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 94 100 protected final int getIndex(int ind){ 101 final int k = ind+begin; 102 if(k >= end){ 103 throw new ArrayIndexOutOfBoundsException (k); 104 } 105 return k; 106 } 107 public boolean equals(Object 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 toString() { 118 return Misc.getTypeName(arr.getClass()); 119 } 120 } 121 | Popular Tags |