GNU Classpath (0.18) | ||
Frames | No Frames |
1: /* HashAttributeSet.java -- 2: Copyright (C) 2003, 2004 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: public class HashAttributeSet implements AttributeSet, Serializable 45: { 46: private static final long serialVersionUID = 5311560590283707917L; 47: 48: private Class interfaceName; 49: private HashMap attributeMap = new HashMap(); 50: 51: /** 52: * Creates an empty <code>HashAttributeSet</code> object. 53: */ 54: public HashAttributeSet() 55: { 56: this(Attribute.class); 57: } 58: 59: /** 60: * Creates a <code>HashAttributeSet</code> object with the given 61: * attribute in it. 62: * 63: * @param attribute the attribute to put into the set 64: * 65: * @exception NullPointerException if attribute is null 66: */ 67: public HashAttributeSet(Attribute attribute) 68: { 69: this(attribute, Attribute.class); 70: } 71: 72: /** 73: * Creates a <code>HashAttributeSet</code> object with the given 74: * attributes in it. 75: * 76: * @param attributes the attributes to put into the set 77: * 78: * @exception NullPointerException If attributes is null 79: */ 80: public HashAttributeSet(Attribute[] attributes) 81: { 82: this(attributes, Attribute.class); 83: } 84: 85: /** 86: * Creates a <code>HashAttributeSet</code> object with the given 87: * attributes in it. 88: * 89: * @param attributes the attributes to put into the set 90: * 91: * @exception NullPointerException If attributes is null 92: */ 93: public HashAttributeSet(AttributeSet attributes) 94: { 95: this(attributes, Attribute.class); 96: } 97: 98: /** 99: * Creates an empty <code>HashAttributeSet</code> object. 100: * 101: * @param interfaceName the interface that all members must implement 102: * 103: * @exception NullPointerException if interfaceName is null 104: */ 105: protected HashAttributeSet(Class interfaceName) 106: { 107: if (interfaceName == null) 108: throw new NullPointerException("interfaceName may not be null"); 109: 110: this.interfaceName = interfaceName; 111: } 112: 113: /** 114: * Creates an empty <code>HashAttributeSet</code> object. 115: * 116: * @exception ClassCastException if attribute is not an interface of 117: * interfaceName 118: * @exception NullPointerException if attribute or interfaceName is null 119: */ 120: protected HashAttributeSet(Attribute attribute, Class interfaceName) 121: { 122: this(interfaceName); 123: 124: if (attribute == null) 125: throw new NullPointerException(); 126: 127: addInternal(attribute, interfaceName); 128: } 129: 130: /** 131: * Creates an empty <code>HashAttributeSet</code> object. 132: * 133: * @exception ClassCastException if any element of attributes is not an 134: * interface of interfaceName 135: * @exception NullPointerException if attributes or interfaceName is null 136: */ 137: protected HashAttributeSet(Attribute[] attributes, Class interfaceName) 138: { 139: this(interfaceName); 140: 141: if (attributes == null) 142: throw new NullPointerException(); 143: 144: for (int index = 0; index < attributes.length; index++) 145: addInternal(attributes[index], interfaceName); 146: } 147: 148: /** 149: * Creates an empty <code>HashAttributeSet</code> object. 150: * 151: * @exception ClassCastException if any element of attributes is not an 152: * interface of interfaceName 153: */ 154: protected HashAttributeSet(AttributeSet attributes, Class interfaceName) 155: { 156: this(interfaceName); 157: 158: if (attributes != null) 159: addAllInternal(attributes, interfaceName); 160: } 161: 162: /** 163: * Adds the given attribute to the set. 164: * 165: * @param attribute the attribute to add 166: * 167: * @return true if the attribute set has changed, false otherwise 168: * 169: * @exception NullPointerException if attribute is null 170: * @exception UnmodifiableSetException if this attribute set does not 171: * support this action. 172: */ 173: public boolean add(Attribute attribute) 174: { 175: return addInternal(attribute, interfaceName); 176: } 177: 178: private boolean addInternal(Attribute attribute, Class interfaceName) 179: { 180: if (attribute == null) 181: throw new NullPointerException("attribute may not be null"); 182: 183: AttributeSetUtilities.verifyAttributeCategory(interfaceName, 184: this.interfaceName); 185: 186: Object old = attributeMap.put 187: (attribute.getCategory(), AttributeSetUtilities.verifyAttributeValue 188: (attribute, interfaceName)); 189: return !attribute.equals(old); 190: } 191: 192: /** 193: * Adds the given attributes to the set. 194: * 195: * @param attributes the attributes to add 196: * 197: * @return true if the attribute set has changed, false otherwise 198: * 199: * @exception UnmodifiableSetException if this attribute set does not 200: * support this action. 201: */ 202: public boolean addAll(AttributeSet attributes) 203: { 204: return addAllInternal(attributes, interfaceName); 205: } 206: 207: private boolean addAllInternal(AttributeSet attributes, Class interfaceName) 208: { 209: boolean modified = false; 210: Attribute[] array = attributes.toArray(); 211: 212: for (int index = 0; index < array.length; index++) 213: if (addInternal(array[index], interfaceName)) 214: modified = true; 215: 216: return modified; 217: } 218: 219: /** 220: * Removes all attributes from this attribute set. 221: * 222: * @exception UnmodifiableSetException if this attribute set does not 223: * support this action. 224: */ 225: public void clear() 226: { 227: attributeMap.clear(); 228: } 229: 230: /** 231: * Checks if this attribute set contains an entry with the given category. 232: * 233: * @param category the category to test for 234: * 235: * @return true if the category exists in this attribute set, false otherwise. 236: */ 237: public boolean containsKey(Class category) 238: { 239: return attributeMap.containsKey(category); 240: } 241: 242: /** 243: * Checks if this attribute set contains an entry with the given attribute. 244: * 245: * @param attribute the attribute to test for 246: * 247: * @return true if the attribute exists in this attribute set, 248: * false otherwise. 249: */ 250: public boolean containsValue(Attribute attribute) 251: { 252: return attributeMap.containsValue(attribute); 253: } 254: 255: /** 256: * Tests of obj is equal to this object. 257: * 258: * @param obj the object to test 259: * 260: * @return true if both objects are equal, false otherwise. 261: */ 262: public boolean equals(Object obj) 263: { 264: if (! (obj instanceof HashAttributeSet)) 265: return false; 266: 267: return attributeMap.equals(((HashAttributeSet) obj).attributeMap); 268: } 269: 270: /** 271: * Returns the attribute value that is connected to the given attribute 272: * category. If the attribute set does not contains the given category null 273: * will be returned. 274: * 275: * @param category the attribute category to return the attribute value for 276: * 277: * @return the attribute associated to category, or null 278: */ 279: public Attribute get(Class category) 280: { 281: return (Attribute) attributeMap.get(category); 282: } 283: 284: /** 285: * Returns the hashcode for this object. 286: * 287: * @return the hashcode 288: */ 289: public int hashCode() 290: { 291: return attributeMap.hashCode() + interfaceName.hashCode(); 292: } 293: 294: /** 295: * Checks if the attribute set is empty. 296: * 297: * @return true if the attribute set is empty, false otherwise 298: */ 299: public boolean isEmpty() 300: { 301: return attributeMap.isEmpty(); 302: } 303: 304: /** 305: * Removes the entry with the given attribute in it. 306: * 307: * @param attribute the attribute value of the entry to be removed 308: * 309: * @return true if the attribute set has changed, false otherwise. 310: * 311: * @exception UnmodifiableSetException if this attribute set does not 312: * support this action. 313: */ 314: public boolean remove(Attribute attribute) 315: { 316: if (attribute == null) 317: return false; 318: 319: return attributeMap.remove(attribute.getCategory()) != null; 320: } 321: 322: /** 323: * Removes the entry with the given category in it. 324: * 325: * @param category the category value of the entry to be removed 326: * 327: * @return true if the attribute set has changed, false otherwise. 328: */ 329: public boolean remove(Class category) 330: { 331: if (category == null) 332: return false; 333: 334: return attributeMap.remove(category) != null; 335: } 336: 337: /** 338: * Returns the number of elements in this attribute set. 339: * 340: * @return the number of elements. 341: */ 342: public int size() 343: { 344: return attributeMap.size(); 345: } 346: 347: /** 348: * Returns the content of the attribute set as an array 349: * 350: * @return an array of attributes 351: */ 352: public Attribute[] toArray() 353: { 354: int index = 0; 355: Iterator it = attributeMap.entrySet().iterator(); 356: Attribute[] array = new Attribute[size()]; 357: 358: while (it.hasNext()) 359: { 360: array[index] = (Attribute) it.next(); 361: index++; 362: } 363: 364: return array; 365: } 366: }
GNU Classpath (0.18) |