1 7 8 9 package javax.management.openmbean; 10 11 12 import java.io.Serializable ; 15 16 19 20 30 public class ArrayType 31 extends OpenType 32 implements Serializable { 33 34 35 static final long serialVersionUID = 720504429830309770L; 36 37 40 private int dimension; 41 42 46 private OpenType elementType; 47 48 private transient Integer myHashCode = null; private transient String myToString = null; 51 52 53 54 97 public ArrayType(int dimension, 98 OpenType elementType) throws OpenDataException { 99 100 super(buildArrayClassName(dimension, elementType.getClassName()), 103 buildArrayClassName(dimension, elementType.getClassName()), 104 String.valueOf(dimension) +"-dimension array of "+ elementType.getClassName()); 105 106 this.dimension = dimension; this.elementType = elementType; } 111 112 115 private static String buildArrayClassName(int dimension, String elementClassName) throws OpenDataException { 116 117 if (dimension < 1) { 118 throw new IllegalArgumentException ("Value of argument dimension must be greater than 0"); 119 } 120 121 StringBuffer result = new StringBuffer (); 122 123 for (int i=1; i<dimension; i++) { result.append('['); 125 } 126 result.append("[L"); 127 result.append(elementClassName); 128 result.append(';'); 129 130 return result.toString(); 131 } 132 133 134 135 136 141 public int getDimension() { 142 143 return dimension; 144 } 145 146 151 public OpenType getElementOpenType() { 152 153 return elementType; 154 } 155 156 179 public boolean isValue(Object obj) { 180 181 if (obj == null) { 184 return false; 185 } 186 187 Class objClass = obj.getClass(); 188 String objClassName = objClass.getName(); 189 190 if ( ! objClass.isArray() ) { 193 return false; 194 } 195 196 if ( this.getClassName().equals(objClassName) ) { 200 return true; 201 } 202 203 if ( (this.elementType.getClassName().equals(TabularData .class.getName())) || 217 (this.elementType.getClassName().equals(CompositeData .class.getName())) ) { 218 219 225 Class targetClass; 226 try { 227 targetClass = Class.forName(this.getClassName()); 228 } catch (ClassNotFoundException e) { return false; 230 } 231 if ( ! targetClass.isAssignableFrom(objClass) ) { 233 return false; 234 } 235 236 if ( ! checkElementsType( (Object []) obj, this.dimension) ) { return false; 239 } 240 241 return true; 242 } 243 244 return false; 246 } 247 248 255 private boolean checkElementsType(Object [] x_dim_Array, int dim) { 256 257 if ( dim > 1 ) { 259 for (int i=0; i<x_dim_Array.length; i++) { 260 if ( ! checkElementsType((Object [])x_dim_Array[i], dim-1) ) { 261 return false; 262 } 263 } 264 return true; 265 } 266 else { 268 for (int i=0; i<x_dim_Array.length; i++) { 269 if ( (x_dim_Array[i] != null) && (! this.getElementOpenType().isValue(x_dim_Array[i])) ) { 270 return false; 271 } 272 } 273 return true; 274 } 275 } 276 277 278 279 280 281 293 public boolean equals(Object obj) { 294 295 if (obj == null) { 298 return false; 299 } 300 301 ArrayType other; 304 try { 305 other = (ArrayType ) obj; 306 } catch (ClassCastException e) { 307 return false; 308 } 309 310 if (other.dimension != this.dimension) { 313 return false; 314 } 315 316 return this.elementType.equals(other.elementType); 319 } 320 321 337 public int hashCode() { 338 339 if (myHashCode == null) { 342 int value = 0; 343 value += this.dimension; 344 value += this.elementType.hashCode(); 345 myHashCode = new Integer (value); 346 } 347 348 return myHashCode.intValue(); 351 } 352 353 365 public String toString() { 366 367 if (myToString == null) { 370 StringBuffer result = new StringBuffer (); 371 result.append(this.getClass().getName()); 372 result.append("(name="); 373 result.append(getTypeName()); 374 result.append(",dimension="); 375 result.append(String.valueOf(this.dimension)); 376 result.append(",elementType="); 377 result.append(this.elementType.toString()); 378 result.append(")"); 379 myToString = result.toString(); 380 } 381 382 return myToString; 385 } 386 387 } 388 | Popular Tags |