1 7 8 package javax.naming.directory; 9 10 import java.util.Vector ; 11 import java.util.Enumeration ; 12 import java.util.NoSuchElementException ; 13 import java.lang.reflect.Array ; 14 15 import javax.naming.NamingException ; 16 import javax.naming.NamingEnumeration ; 17 import javax.naming.OperationNotSupportedException ; 18 19 55 public class BasicAttribute implements Attribute { 56 62 protected String attrID; 63 64 69 protected transient Vector <Object > values; 70 71 75 protected boolean ordered = false; 76 77 public Object clone() { 78 BasicAttribute attr; 79 try { 80 attr = (BasicAttribute )super.clone(); 81 } catch (CloneNotSupportedException e) { 82 attr = new BasicAttribute (attrID, ordered); 83 } 84 attr.values = (Vector )values.clone(); 85 return attr; 86 } 87 88 114 public boolean equals(Object obj) { 115 if ((obj != null) && (obj instanceof Attribute )) { 116 Attribute target = (Attribute )obj; 117 118 if (isOrdered() != target.isOrdered()) { 120 return false; 121 } 122 int len; 123 if (attrID.equals(target.getID()) && 124 (len=size()) == target.size()) { 125 try { 126 if (isOrdered()) { 127 for (int i = 0; i < len; i++) { 129 if (!valueEquals(get(i), target.get(i))) { 130 return false; 131 } 132 } 133 } else { 134 Enumeration theirs = target.getAll(); 136 while (theirs.hasMoreElements()) { 137 if (find(theirs.nextElement()) < 0) 138 return false; 139 } 140 } 141 } catch (NamingException e) { 142 return false; 143 } 144 return true; 145 } 146 } 147 return false; 148 } 149 150 164 public int hashCode() { 165 int hash = attrID.hashCode(); 166 int num = values.size(); 167 Object val; 168 for (int i = 0; i < num; i ++) { 169 val = values.elementAt(i); 170 if (val != null) { 171 if (val.getClass().isArray()) { 172 Object it; 173 int len = Array.getLength(val); 174 for (int j = 0 ; j < len ; j++) { 175 it = Array.get(val, j); 176 if (it != null) { 177 hash += it.hashCode(); 178 } 179 } 180 } else { 181 hash += val.hashCode(); 182 } 183 } 184 } 185 return hash; 186 } 187 188 195 public String toString() { 196 StringBuffer answer = new StringBuffer (attrID + ": "); 197 if (values.size() == 0) { 198 answer.append("No values"); 199 } else { 200 boolean start = true; 201 for (Enumeration e = values.elements(); e.hasMoreElements(); ) { 202 if (!start) 203 answer.append(", "); 204 answer.append(e.nextElement()); 205 start = false; 206 } 207 } 208 return answer.toString(); 209 } 210 211 216 public BasicAttribute(String id) { 217 this(id, false); 218 } 219 220 227 public BasicAttribute(String id, Object value) { 228 this(id, value, false); 229 } 230 231 238 public BasicAttribute(String id, boolean ordered) { 239 attrID = id; 240 values = new Vector (); 241 this.ordered = ordered; 242 } 243 244 254 public BasicAttribute(String id, Object value, boolean ordered) { 255 this(id, ordered); 256 values.addElement(value); 257 } 258 259 267 public NamingEnumeration <?> getAll() throws NamingException { 268 return new ValuesEnumImpl(); 269 } 270 271 279 public Object get() throws NamingException { 280 if (values.size() == 0) { 281 throw new 282 NoSuchElementException ("Attribute " + getID() + " has no value"); 283 } else { 284 return values.elementAt(0); 285 } 286 } 287 288 public int size() { 289 return values.size(); 290 } 291 292 public String getID() { 293 return attrID; 294 } 295 296 306 public boolean contains(Object attrVal) { 307 return (find(attrVal) >= 0); 308 } 309 310 private int find(Object target) { 313 Class cl; 314 if (target == null) { 315 int ct = values.size(); 316 for (int i = 0 ; i < ct ; i++) { 317 if (values.elementAt(i) == null) 318 return i; 319 } 320 } else if ((cl=target.getClass()).isArray()) { 321 int ct = values.size(); 322 Object it; 323 for (int i = 0 ; i < ct ; i++) { 324 it = values.elementAt(i); 325 if (it != null && cl == it.getClass() 326 && arrayEquals(target, it)) 327 return i; 328 } 329 } else { 330 return values.indexOf(target, 0); 331 } 332 return -1; } 334 335 339 private static boolean valueEquals(Object obj1, Object obj2) { 340 if (obj1 == obj2) { 341 return true; } 343 if (obj1 == null) { 344 return false; } 346 if (obj1.getClass().isArray() && 347 obj2.getClass().isArray()) { 348 return arrayEquals(obj1, obj2); 349 } 350 return (obj1.equals(obj2)); 351 } 352 353 357 private static boolean arrayEquals(Object a1, Object a2) { 358 int len; 359 if ((len = Array.getLength(a1)) != Array.getLength(a2)) 360 return false; 361 362 for (int j = 0; j < len; j++) { 363 Object i1 = Array.get(a1, j); 364 Object i2 = Array.get(a2, j); 365 if (i1 == null || i2 == null) { 366 if (i1 != i2) 367 return false; 368 } else if (!i1.equals(i2)) { 369 return false; 370 } 371 } 372 return true; 373 } 374 375 384 public boolean add(Object attrVal) { 385 if (isOrdered() || (find(attrVal) < 0)) { 386 values.addElement(attrVal); 387 return true; 388 } else { 389 return false; 390 } 391 } 392 393 402 public boolean remove(Object attrval) { 403 406 int i = find(attrval); 407 if (i >= 0) { 408 values.removeElementAt(i); 409 return true; 410 } 411 return false; 412 } 413 414 public void clear() { 415 values.setSize(0); 416 } 417 418 420 public boolean isOrdered() { 421 return ordered; 422 } 423 424 public Object get(int ix) throws NamingException { 425 return values.elementAt(ix); 426 } 427 428 public Object remove(int ix) { 429 Object answer = values.elementAt(ix); 430 values.removeElementAt(ix); 431 return answer; 432 } 433 434 public void add(int ix, Object attrVal) { 435 if (!isOrdered() && contains(attrVal)) { 436 throw new IllegalStateException ( 437 "Cannot add duplicate to unordered attribute"); 438 } 439 values.insertElementAt(attrVal, ix); 440 } 441 442 public Object set(int ix, Object attrVal) { 443 if (!isOrdered() && contains(attrVal)) { 444 throw new IllegalStateException ( 445 "Cannot add duplicate to unordered attribute"); 446 } 447 448 Object answer = values.elementAt(ix); 449 values.setElementAt(attrVal, ix); 450 return answer; 451 } 452 453 455 461 public DirContext getAttributeSyntaxDefinition() throws NamingException { 462 throw new OperationNotSupportedException ("attribute syntax"); 463 } 464 465 471 public DirContext getAttributeDefinition() throws NamingException { 472 throw new OperationNotSupportedException ("attribute definition"); 473 } 474 475 476 478 484 private void writeObject(java.io.ObjectOutputStream s) 485 throws java.io.IOException { 486 s.defaultWriteObject(); s.writeInt(values.size()); 488 for (int i = 0; i < values.size(); i++) { 489 s.writeObject(values.elementAt(i)); 490 } 491 } 492 493 496 private void readObject(java.io.ObjectInputStream s) 497 throws java.io.IOException , ClassNotFoundException { 498 s.defaultReadObject(); int n = s.readInt(); values = new Vector (n); 501 while (--n >= 0) { 502 values.addElement(s.readObject()); 503 } 504 } 505 506 507 class ValuesEnumImpl implements NamingEnumeration <Object > { 508 Enumeration list; 509 510 ValuesEnumImpl() { 511 list = values.elements(); 512 } 513 514 public boolean hasMoreElements() { 515 return list.hasMoreElements(); 516 } 517 518 public Object nextElement() { 519 return(list.nextElement()); 520 } 521 522 public Object next() throws NamingException { 523 return list.nextElement(); 524 } 525 526 public boolean hasMore() throws NamingException { 527 return list.hasMoreElements(); 528 } 529 530 public void close() throws NamingException { 531 list = null; 532 } 533 } 534 535 538 private static final long serialVersionUID = 6743528196119291326L; 539 } 540 | Popular Tags |