Source for javax.print.attribute.HashAttributeSet

   1: /* HashAttributeSet.java -- 
   2:    Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package javax.print.attribute;
  39: 
  40: import java.io.Serializable;
  41: import java.util.HashMap;
  42: import java.util.Iterator;
  43: 
  44: /**
  45:  * <code>HashAttributeSet</code> provides an implementation of
  46:  * {@link javax.print.attribute.AttributeSet}.
  47:  */
  48: public class HashAttributeSet implements AttributeSet, Serializable
  49: {
  50:   private static final long serialVersionUID = 5311560590283707917L;
  51:   
  52:   private Class interfaceName;
  53:   private HashMap attributeMap = new HashMap();
  54: 
  55:   /**
  56:    * Creates an empty <code>HashAttributeSet</code> object.
  57:    */
  58:   public HashAttributeSet()
  59:   {
  60:     this(Attribute.class);
  61:   }
  62: 
  63:   /**
  64:    * Creates a <code>HashAttributeSet</code> object with the given
  65:    * attribute in it.
  66:    *
  67:    * @param attribute the attribute to put into the set
  68:    *
  69:    * @exception NullPointerException if attribute is null
  70:    */
  71:   public HashAttributeSet(Attribute attribute)
  72:   {
  73:     this(attribute, Attribute.class);
  74:   }
  75: 
  76:   /**
  77:    * Creates a <code>HashAttributeSet</code> object with the given
  78:    * attributes in it.
  79:    *
  80:    * @param attributes the array of attributes to put into the set. If
  81:    * <code>null</code> an empty set is created.
  82:    *
  83:    * @exception NullPointerException if one of the attributes of the given
  84:    * array is null.
  85:    */
  86:   public HashAttributeSet(Attribute[] attributes)
  87:   {
  88:     this(attributes, Attribute.class);
  89:   }
  90: 
  91:   /**
  92:    * Creates a <code>HashAttributeSet</code> object with attributes
  93:    * of the given attributes set in it.
  94:    *
  95:    * @param attributes the attributes set to put into the set. If 
  96:    * <code>null</code> an empty set is created.
  97:    */
  98:   public HashAttributeSet(AttributeSet attributes)
  99:   {
 100:     this(attributes, Attribute.class);
 101:   }
 102: 
 103:   /**
 104:    * Creates an empty <code>HashAttributeSet</code> object.
 105:    *
 106:    * @param interfaceName the interface that all members must implement
 107:    *
 108:    * @exception NullPointerException if interfaceName is null
 109:    */
 110:   protected HashAttributeSet(Class interfaceName)
 111:   {
 112:     if (interfaceName == null)
 113:       throw new NullPointerException("interfaceName may not be null");
 114:     
 115:     this.interfaceName = interfaceName;
 116:   }
 117:   
 118:   /**
 119:    * Creates a <code>HashAttributeSet</code> object with the given
 120:    * attribute in it.
 121:    * 
 122:    * @param attribute the attribute to put into the set.
 123:    * @param interfaceName the interface that all members must implement.
 124:    *
 125:    * @exception ClassCastException if attribute is not an interface of
 126:    * interfaceName
 127:    * @exception NullPointerException if attribute or interfaceName is null
 128:    */
 129:   protected HashAttributeSet(Attribute attribute, Class interfaceName)
 130:   {
 131:     this(interfaceName);
 132:     
 133:     if (attribute == null)
 134:       throw new NullPointerException();
 135:     
 136:     addInternal(attribute, interfaceName);
 137:   }
 138: 
 139:   /**
 140:    * Creates a <code>HashAttributeSet</code> object with the given
 141:    * attributes in it.
 142:    *
 143:    * @param attributes the array of attributes to put into the set. If
 144:    * <code>null</code> an empty set is created.
 145:    * @param interfaceName the interface that all members must implement.
 146:    *
 147:    * @exception ClassCastException if any element of attributes is not an
 148:    * interface of interfaceName
 149:    * @exception NullPointerException if attributes or interfaceName is null
 150:    */
 151:   protected HashAttributeSet(Attribute[] attributes, Class interfaceName)
 152:   {
 153:     this(interfaceName);
 154:     
 155:     if (attributes != null)
 156:       {
 157:         for (int index = 0; index < attributes.length; index++)
 158:           addInternal(attributes[index], interfaceName);
 159:       }
 160:   }
 161: 
 162:   /**
 163:    * Creates a <code>HashAttributeSet</code> object with attributes
 164:    * of the given attributes set in it.
 165:    *
 166:    * @param attributes the attributes set to put into the set. If 
 167:    * <code>null</code> an empty set is created.
 168:    * @param interfaceName the interface that all members must implement.
 169:    *
 170:    * @exception ClassCastException if any element of attributes is not an
 171:    * interface of interfaceName
 172:    */
 173:   protected HashAttributeSet(AttributeSet attributes, Class interfaceName)
 174:   {
 175:     this(interfaceName);
 176:     
 177:     if (attributes != null)
 178:       addAllInternal(attributes, interfaceName);
 179:   }
 180: 
 181:   /**
 182:    * Adds the specified attribute value to this attribute set 
 183:    * if it is not already present.
 184:    * 
 185:    * This operation removes any existing attribute of the same category 
 186:    * before adding the given attribute to the set. 
 187:    * 
 188:    * @param attribute the attribute to add.
 189:    * @return <code>true</code> if the set is changed, false otherwise.
 190:    * @throws NullPointerException if the attribute is <code>null</code>.
 191:    * @throws UnmodifiableSetException if the set does not support modification.
 192:    */
 193:   public boolean add(Attribute attribute)
 194:   {
 195:     return addInternal(attribute, interfaceName);
 196:   }
 197: 
 198:   private boolean addInternal(Attribute attribute, Class interfaceName)
 199:   {
 200:     if (attribute == null)
 201:       throw new NullPointerException("attribute may not be null");
 202: 
 203:     AttributeSetUtilities.verifyAttributeCategory(interfaceName,
 204:                           this.interfaceName);
 205: 
 206:     Object old = attributeMap.put
 207:       (attribute.getCategory(), AttributeSetUtilities.verifyAttributeValue
 208:                                   (attribute, interfaceName));
 209:     return !attribute.equals(old);
 210:   }
 211: 
 212:   /**
 213:    * Adds all of the elements in the specified set to this attribute set.
 214:    * 
 215:    * @param attributes the set of attributes to add.
 216:    * @return <code>true</code> if the set is changed, false otherwise.
 217:    * @throws UnmodifiableSetException if the set does not support modification.
 218:    * 
 219:    * @see #add(Attribute)
 220:    */
 221:   public boolean addAll(AttributeSet attributes)
 222:   {
 223:     return addAllInternal(attributes, interfaceName);
 224:   }
 225: 
 226:   private boolean addAllInternal(AttributeSet attributes, Class interfaceName)
 227:   {
 228:     boolean modified = false;
 229:     Attribute[] array = attributes.toArray();
 230: 
 231:     for (int index = 0; index < array.length; index++)
 232:       if (addInternal(array[index], interfaceName))
 233:         modified = true;
 234: 
 235:     return modified;
 236:   }
 237: 
 238:   /**
 239:    * Removes all attributes from this attribute set.
 240:    * 
 241:    * @throws UnmodifiableSetException if the set does not support modification.
 242:    */
 243:   public void clear()
 244:   {
 245:     attributeMap.clear();
 246:   }
 247: 
 248:   /**
 249:    * Checks if this attributes set contains an attribute with the given 
 250:    * category.
 251:    * 
 252:    * @param category the category to test for.
 253:    * @return <code>true</code> if an attribute of the category is contained
 254:    * in the set, <code>false</code> otherwise.
 255:    */
 256:   public boolean containsKey(Class category)
 257:   {
 258:     return attributeMap.containsKey(category);
 259:   }
 260: 
 261:   /**
 262:    * Checks if this attribute set contains the given attribute.
 263:    * 
 264:    * @param attribute the attribute to test for.
 265:    * @return <code>true</code> if the attribute is contained in the set,
 266:    * <code>false</code> otherwise.
 267:    */
 268:   public boolean containsValue(Attribute attribute)
 269:   {
 270:     return attributeMap.containsValue(attribute);
 271:   }
 272: 
 273:   /**
 274:    * Tests this set for equality with the given object. <code>true</code> is
 275:    * returned, if the given object is also of type <code>AttributeSet</code>
 276:    * and the contained attributes are the same as in this set.
 277:    * 
 278:    * @param obj the Object to test.
 279:    * @return <code>true</code> if equal, false otherwise.
 280:    */
 281:   public boolean equals(Object obj)
 282:   {
 283:     if (! (obj instanceof HashAttributeSet))
 284:       return false;
 285: 
 286:     return attributeMap.equals(((HashAttributeSet) obj).attributeMap);
 287:   }
 288: 
 289:   /**
 290:    * Returns the attribute object contained in this set for the given attribute
 291:    * category. 
 292:    * 
 293:    * @param category the category of the attribute. A <code>Class</code> 
 294:    * instance of a class implementing the <code>Attribute</code> interface. 
 295:    * @return The attribute for this category or <code>null</code> if no 
 296:    * attribute is contained for the given category. 
 297:    * @throws NullPointerException if category is null.
 298:    * @throws ClassCastException if category is not implementing 
 299:    * <code>Attribute</code>.
 300:    */
 301:   public Attribute get(Class category)
 302:   {
 303:     if (category == null)
 304:       throw new NullPointerException("category may not be null");
 305:     
 306:     return (Attribute) attributeMap.get(category);
 307:   }
 308:   
 309:   /**
 310:    * Returns the hashcode value. The hashcode value is the sum of all hashcodes
 311:    * of the attributes contained in this set.
 312:    * 
 313:    * @return The hashcode for this attribute set.
 314:    */
 315:   public int hashCode()
 316:   {
 317:     int hashcode = 0;
 318:     Iterator it = attributeMap.values().iterator();
 319:     while (it.hasNext())
 320:       hashcode = hashcode + it.next().hashCode();
 321:           
 322:     return hashcode;
 323:   }
 324: 
 325:   /**
 326:    * Checks if the attribute set is empty.
 327:    *
 328:    * @return <code>true</code> if the attribute set is empty, false otherwise.
 329:    */
 330:   public boolean isEmpty()
 331:   {
 332:     return attributeMap.isEmpty();
 333:   }
 334: 
 335:   /**
 336:    * Removes the given attribute from the set. If the given attribute is <code>null</code>
 337:    * nothing is done and <code>false</code> is returned.
 338:    * 
 339:    * @param attribute the attribute to remove.  
 340:    * @return <code>true</code> if removed, false in all other cases. 
 341:    * @throws UnmodifiableSetException if the set does not support modification.
 342:    */
 343:   public boolean remove(Attribute attribute)
 344:   {
 345:     if (attribute == null)
 346:       return false;
 347: 
 348:     return attributeMap.remove(attribute.getCategory()) != null;
 349:   }
 350: 
 351:   /**
 352:    * Removes the attribute entry of the given category from the set. If the given
 353:    * category is <code>null</code> nothing is done and <code>false</code> is returned.
 354:    * 
 355:    * @param category the category of the entry to be removed.
 356:    * @return <code>true</code> if an attribute is removed, false in all other cases. 
 357:    * @throws UnmodifiableSetException if the set does not support modification.
 358:    */
 359:   public boolean remove(Class category)
 360:   {
 361:     if (category == null)
 362:       return false;
 363: 
 364:     return attributeMap.remove(category) != null;
 365:   }
 366: 
 367:   /**
 368:    * Returns the number of elements in this attribute set.
 369:    *
 370:    * @return The number of elements.
 371:    */
 372:   public int size()
 373:   {
 374:     return attributeMap.size();
 375:   }
 376: 
 377:   /**
 378:    * Returns the content of the attribute set as an array
 379:    *
 380:    * @return An array of attributes.
 381:    */
 382:   public Attribute[] toArray()
 383:   {
 384:     int index = 0;
 385:     Iterator it = attributeMap.values().iterator();
 386:     Attribute[] array = new Attribute[size()];
 387: 
 388:     while (it.hasNext())
 389:       {
 390:         array[index] = (Attribute) it.next();
 391:         index++;
 392:       }
 393:     
 394:     return array;
 395:   }
 396: }