fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / libjava / javax / print / attribute / HashAttributeSet.java
blob63b4105cb42cf43619ca23839763d7458423ca20
1 /* HashAttributeSet.java --
2 Copyright (C) 2003 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
38 package javax.print.attribute;
40 import java.io.Serializable;
41 import java.util.HashMap;
42 import java.util.Iterator;
44 public class HashAttributeSet implements AttributeSet, Serializable
46 private static final long serialVersionUID = 5311560590283707917L;
48 private Class interfaceName;
49 private HashMap attributeMap = new HashMap();
51 /**
52 * Creates an empty <code>HashAttributeSet</code> object.
54 public HashAttributeSet()
56 this(Attribute.class);
59 /**
60 * Creates a <code>HashAttributeSet</code> object with the given
61 * attribute in it.
63 * @param attribute the attribute to put into the set
65 * @exception NullPointerException if attribute is null
67 public HashAttributeSet(Attribute attribute)
69 this(attribute, Attribute.class);
72 /**
73 * Creates a <code>HashAttributeSet</code> object with the given
74 * attributes in it.
76 * @param attributes the attributes to put into the set
78 * @exception NullPointerException If attributes is null
80 public HashAttributeSet(Attribute[] attributes)
82 this(attributes, Attribute.class);
85 /**
86 * Creates a <code>HashAttributeSet</code> object with the given
87 * attributes in it.
89 * @param attributes the attributes to put into the set
91 * @exception NullPointerException If attributes is null
93 public HashAttributeSet(AttributeSet attributes)
95 this(attributes, Attribute.class);
98 /**
99 * Creates an empty <code>HashAttributeSet</code> object.
101 * @param interfaceName the interface that all members must implement
103 * @exception NullPointerException if interfaceName is null
105 protected HashAttributeSet(Class interfaceName)
107 if (interfaceName == null)
108 throw new NullPointerException("interfaceName may not be null");
110 this.interfaceName = interfaceName;
114 * Creates an empty <code>HashAttributeSet</code> object.
116 * @exception ClassCastException if attribute is not an interface of
117 * interfaceName
118 * @exception NullPointerException if attribute or interfaceName is null
120 protected HashAttributeSet(Attribute attribute, Class interfaceName)
122 this(interfaceName);
124 if (attribute == null)
125 throw new NullPointerException();
127 addInternal(attribute, interfaceName);
131 * Creates an empty <code>HashAttributeSet</code> object.
133 * @exception ClassCastException if any element of attributes is not an
134 * interface of interfaceName
135 * @exception NullPointerException if attributes or interfaceName is null
137 protected HashAttributeSet(Attribute[] attributes, Class interfaceName)
139 this(interfaceName);
141 if (attributes == null)
142 throw new NullPointerException();
144 for (int index = 0; index < attributes.length; index++)
145 addInternal(attributes[index], interfaceName);
149 * Creates an empty <code>HashAttributeSet</code> object.
151 * @exception ClassCastException if any element of attributes is not an
152 * interface of interfaceName
154 public HashAttributeSet(AttributeSet attributes, Class interfaceName)
156 this(interfaceName);
158 if (attributes != null)
159 addAllInternal(attributes, interfaceName);
163 * Adds the given attribute to the set.
165 * @param attribute the attribute to add
167 * @return true if the attribute set has changed, false otherwise
169 * @exception NullPointerException if attribute is null
170 * @exception UnmodifiableSetException if this attribute set does not
171 * support this action.
173 public boolean add(Attribute attribute)
175 return addInternal(attribute, interfaceName);
178 private boolean addInternal(Attribute attribute, Class interfaceName)
180 if (attribute == null)
181 throw new NullPointerException("attribute may not be null");
183 AttributeSetUtilities.verifyAttributeCategory(interfaceName,
184 this.interfaceName);
186 Object old = attributeMap.put
187 (attribute.getCategory(), AttributeSetUtilities.verifyAttributeValue
188 (attribute, interfaceName));
189 return !attribute.equals(old);
193 * Adds the given attributes to the set.
195 * @param attributes the attributes to add
197 * @return true if the attribute set has changed, false otherwise
199 * @exception UnmodifiableSetException if this attribute set does not
200 * support this action.
202 public boolean addAll(AttributeSet attributes)
204 return addAllInternal(attributes, interfaceName);
207 private boolean addAllInternal(AttributeSet attributes, Class interfaceName)
209 boolean modified = false;
210 Attribute[] array = attributes.toArray();
212 for (int index = 0; index < array.length; index++)
213 if (addInternal(array[index], interfaceName))
214 modified = true;
216 return modified;
220 * Removes all attributes from this attribute set.
222 * @exception UnmodifiableSetException if this attribute set does not
223 * support this action.
225 public void clear()
227 attributeMap.clear();
231 * Checks if this attribute set contains an entry with the given category.
233 * @param category the category to test for
235 * @result true if the category exists in this attribute set, false otherwise.
237 public boolean containsKey(Class category)
239 return attributeMap.containsKey(category);
243 * Checks if this attribute set contains an entry with the given attribute.
245 * @param attribute the attribute to test for
247 * @result true if the attribute exists in this attribute set,
248 * false otherwise.
250 public boolean containsValue(Attribute attribute)
252 return attributeMap.containsValue(attribute);
256 * Tests of obj is equal to this object.
258 * @param obj the object to test
260 * @returns true if both objects are equal, false otherwise.
262 public boolean equals(Object obj)
264 if (! (obj instanceof HashAttributeSet))
265 return false;
267 return attributeMap.equals(((HashAttributeSet) obj).attributeMap);
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.
275 * @param category the attribute category to return the attribute value for
277 * @return the attribute associated to category, or null
279 public Attribute get(Class category)
281 return (Attribute) attributeMap.get(category);
285 * Returns the hashcode for this object.
287 * @return the hashcode
289 public int hashCode()
291 return attributeMap.hashCode() + interfaceName.hashCode();
295 * Checks if the attribute set is empty.
297 * @return true if the attribute set is empty, false otherwise
299 public boolean isEmpty()
301 return attributeMap.isEmpty();
305 * Removes the entry with the given attribute in it.
307 * @param attribute the attribute value of the entry to be removed
309 * @return true if the attribute set has changed, false otherwise.
311 * @exception UnmodifiableSetException if this attribute set does not
312 * support this action.
314 public boolean remove(Attribute attribute)
316 if (attribute == null)
317 return false;
319 return attributeMap.remove(attribute.getCategory()) != null;
323 * Removes the entry with the given category in it.
325 * @param category the category value of the entry to be removed
327 * @return true if the attribute set has changed, false otherwise.
329 public boolean remove(Class category)
331 if (category == null)
332 return false;
334 return attributeMap.remove(category) != null;
338 * Returns the number of elements in this attribute set.
340 * @return the number of elements.
342 public int size()
344 return attributeMap.size();
348 * Returns the content of the attribute set as an array
350 * @return an array of attributes
352 public Attribute[] toArray()
354 int index = 0;
355 Iterator it = attributeMap.entrySet().iterator();
356 Attribute[] array = new Attribute[size()];
358 while (it.hasNext())
360 array[index] = (Attribute) it.next();
361 index++;
364 return array;