1 7 8 package javax.print.attribute; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.Serializable ; 14 import java.util.HashMap ; 15 16 23 public class HashAttributeSet implements AttributeSet , Serializable { 24 25 private static final long serialVersionUID = 5311560590283707917L; 26 27 33 private Class myInterface; 34 35 39 private transient HashMap attrMap = new HashMap (); 40 41 50 private void writeObject(ObjectOutputStream s) throws IOException { 51 52 s.defaultWriteObject(); 53 Attribute [] attrs = toArray(); 54 s.writeInt(attrs.length); 55 for (int i = 0; i < attrs.length; i++) { 56 s.writeObject(attrs[i]); 57 } 58 } 59 60 63 private void readObject(ObjectInputStream s) 64 throws ClassNotFoundException , IOException { 65 66 s.defaultReadObject(); 67 attrMap = new HashMap (); 68 int count = s.readInt(); 69 Attribute attr; 70 for (int i = 0; i < count; i++) { 71 attr = (Attribute )s.readObject(); 72 add(attr); 73 } 74 } 75 76 79 public HashAttributeSet() { 80 this(Attribute .class); 81 } 82 83 92 public HashAttributeSet(Attribute attribute) { 93 this (attribute, Attribute .class); 94 } 95 96 112 public HashAttributeSet(Attribute [] attributes) { 113 this (attributes, Attribute .class); 114 } 115 116 124 public HashAttributeSet(AttributeSet attributes) { 125 this (attributes, Attribute .class); 126 } 127 128 138 protected HashAttributeSet(Class <?> interfaceName) { 139 if (interfaceName == null) { 140 throw new NullPointerException ("null interface"); 141 } 142 myInterface = interfaceName; 143 } 144 145 163 protected HashAttributeSet(Attribute attribute, Class <?> interfaceName) { 164 if (interfaceName == null) { 165 throw new NullPointerException ("null interface"); 166 } 167 myInterface = interfaceName; 168 add (attribute); 169 } 170 171 196 protected HashAttributeSet(Attribute [] attributes, Class <?> interfaceName) { 197 if (interfaceName == null) { 198 throw new NullPointerException ("null interface"); 199 } 200 myInterface = interfaceName; 201 int n = attributes == null ? 0 : attributes.length; 202 for (int i = 0; i < n; ++ i) { 203 add (attributes[i]); 204 } 205 } 206 207 224 protected HashAttributeSet(AttributeSet attributes, Class <?> interfaceName) { 225 myInterface = interfaceName; 226 if (attributes != null) { 227 Attribute [] attribArray = attributes.toArray(); 228 int n = attribArray == null ? 0 : attribArray.length; 229 for (int i = 0; i < n; ++ i) { 230 add (attribArray[i]); 231 } 232 } 233 } 234 235 258 public Attribute get(Class <?> category) { 259 return (Attribute ) 260 attrMap.get(AttributeSetUtilities. 261 verifyAttributeCategory(category, 262 Attribute .class)); 263 } 264 265 282 public boolean add(Attribute attribute) { 283 Object oldAttribute = 284 attrMap.put(attribute.getCategory(), 285 AttributeSetUtilities. 286 verifyAttributeValue(attribute, myInterface)); 287 return (!attribute.equals(oldAttribute)); 288 } 289 290 306 public boolean remove(Class <?> category) { 307 return 308 category != null && 309 AttributeSetUtilities. 310 verifyAttributeCategory(category, Attribute .class) != null && 311 attrMap.remove(category) != null; 312 } 313 314 329 public boolean remove(Attribute attribute) { 330 return 331 attribute != null && 332 attrMap.remove(attribute.getCategory()) != null; 333 } 334 335 345 public boolean containsKey(Class <?> category) { 346 return 347 category != null && 348 AttributeSetUtilities. 349 verifyAttributeCategory(category, Attribute .class) != null && 350 attrMap.get(category) != null; 351 } 352 353 363 public boolean containsValue(Attribute attribute) { 364 return 365 attribute != null && 366 attribute instanceof Attribute && 367 attribute.equals(attrMap.get(((Attribute )attribute).getCategory())); 368 } 369 370 400 public boolean addAll(AttributeSet attributes) { 401 402 Attribute []attrs = attributes.toArray(); 403 boolean result = false; 404 for (int i=0; i<attrs.length; i++) { 405 Attribute newValue = 406 AttributeSetUtilities.verifyAttributeValue(attrs[i], 407 myInterface); 408 Object oldValue = attrMap.put(newValue.getCategory(), newValue); 409 result = (! newValue.equals(oldValue)) || result; 410 } 411 return result; 412 } 413 414 421 public int size() { 422 return attrMap.size(); 423 } 424 425 430 public Attribute [] toArray() { 431 Attribute []attrs = new Attribute [size()]; 432 attrMap.values().toArray(attrs); 433 return attrs; 434 } 435 436 437 444 public void clear() { 445 attrMap.clear(); 446 } 447 448 453 public boolean isEmpty() { 454 return attrMap.isEmpty(); 455 } 456 457 470 471 public boolean equals(Object object) { 472 if (object == null || !(object instanceof AttributeSet )) { 473 return false; 474 } 475 476 AttributeSet aset = (AttributeSet )object; 477 if (aset.size() != size()) { 478 return false; 479 } 480 481 Attribute [] attrs = toArray(); 482 for (int i=0;i<attrs.length; i++) { 483 if (!aset.containsValue(attrs[i])) { 484 return false; 485 } 486 } 487 return true; 488 } 489 490 501 public int hashCode() { 502 int hcode = 0; 503 Attribute [] attrs = toArray(); 504 for (int i=0;i<attrs.length; i++) { 505 hcode += attrs[i].hashCode(); 506 } 507 return hcode; 508 } 509 510 } 511 | Popular Tags |