1 |
/* HashAttributeSet.java -- |
/* HashAttributeSet.java -- |
2 |
Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
41 |
import java.util.HashMap; |
import java.util.HashMap; |
42 |
import java.util.Iterator; |
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 |
public class HashAttributeSet implements AttributeSet, Serializable |
49 |
{ |
{ |
50 |
private static final long serialVersionUID = 5311560590283707917L; |
private static final long serialVersionUID = 5311560590283707917L; |
77 |
* Creates a <code>HashAttributeSet</code> object with the given |
* Creates a <code>HashAttributeSet</code> object with the given |
78 |
* attributes in it. |
* attributes in it. |
79 |
* |
* |
80 |
* @param attributes the attributes to put into the set |
* @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 attributes is null |
* @exception NullPointerException if one of the attributes of the given |
84 |
|
* array is null. |
85 |
*/ |
*/ |
86 |
public HashAttributeSet(Attribute[] attributes) |
public HashAttributeSet(Attribute[] attributes) |
87 |
{ |
{ |
89 |
} |
} |
90 |
|
|
91 |
/** |
/** |
92 |
* Creates a <code>HashAttributeSet</code> object with the given |
* Creates a <code>HashAttributeSet</code> object with attributes |
93 |
* attributes in it. |
* of the given attributes set in it. |
|
* |
|
|
* @param attributes the attributes to put into the set |
|
94 |
* |
* |
95 |
* @exception NullPointerException If attributes is null |
* @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) |
public HashAttributeSet(AttributeSet attributes) |
99 |
{ |
{ |
116 |
} |
} |
117 |
|
|
118 |
/** |
/** |
119 |
* Creates an empty <code>HashAttributeSet</code> object. |
* 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 |
* @exception ClassCastException if attribute is not an interface of |
126 |
* interfaceName |
* interfaceName |
137 |
} |
} |
138 |
|
|
139 |
/** |
/** |
140 |
* Creates an empty <code>HashAttributeSet</code> object. |
* 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 |
* @exception ClassCastException if any element of attributes is not an |
148 |
* interface of interfaceName |
* interface of interfaceName |
152 |
{ |
{ |
153 |
this(interfaceName); |
this(interfaceName); |
154 |
|
|
155 |
if (attributes == null) |
if (attributes != null) |
156 |
throw new NullPointerException(); |
{ |
157 |
|
for (int index = 0; index < attributes.length; index++) |
158 |
for (int index = 0; index < attributes.length; index++) |
addInternal(attributes[index], interfaceName); |
159 |
addInternal(attributes[index], interfaceName); |
} |
160 |
} |
} |
161 |
|
|
162 |
/** |
/** |
163 |
* Creates an empty <code>HashAttributeSet</code> object. |
* 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 |
* @exception ClassCastException if any element of attributes is not an |
171 |
* interface of interfaceName |
* interface of interfaceName |
179 |
} |
} |
180 |
|
|
181 |
/** |
/** |
182 |
* Adds the given attribute to the set. |
* Adds the specified attribute value to this attribute set |
183 |
* |
* if it is not already present. |
184 |
* @param attribute the attribute to add |
* |
185 |
* |
* This operation removes any existing attribute of the same category |
186 |
* @return true if the attribute set has changed, false otherwise |
* before adding the given attribute to the set. |
187 |
* |
* |
188 |
* @exception NullPointerException if attribute is null |
* @param attribute the attribute to add. |
189 |
* @exception UnmodifiableSetException if this attribute set does not |
* @return <code>true</code> if the set is changed, false otherwise. |
190 |
* support this action. |
* @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) |
public boolean add(Attribute attribute) |
194 |
{ |
{ |
210 |
} |
} |
211 |
|
|
212 |
/** |
/** |
213 |
* Adds the given attributes to the set. |
* Adds all of the elements in the specified set to this attribute set. |
214 |
* |
* |
215 |
* @param attributes the attributes to add |
* @param attributes the set of attributes to add. |
216 |
* |
* @return <code>true</code> if the set is changed, false otherwise. |
217 |
* @return true if the attribute set has changed, false otherwise |
* @throws UnmodifiableSetException if the set does not support modification. |
218 |
* |
* |
219 |
* @exception UnmodifiableSetException if this attribute set does not |
* @see #add(Attribute) |
|
* support this action. |
|
220 |
*/ |
*/ |
221 |
public boolean addAll(AttributeSet attributes) |
public boolean addAll(AttributeSet attributes) |
222 |
{ |
{ |
237 |
|
|
238 |
/** |
/** |
239 |
* Removes all attributes from this attribute set. |
* Removes all attributes from this attribute set. |
240 |
* |
* |
241 |
* @exception UnmodifiableSetException if this attribute set does not |
* @throws UnmodifiableSetException if the set does not support modification. |
|
* support this action. |
|
242 |
*/ |
*/ |
243 |
public void clear() |
public void clear() |
244 |
{ |
{ |
246 |
} |
} |
247 |
|
|
248 |
/** |
/** |
249 |
* Checks if this attribute set contains an entry with the given category. |
* Checks if this attributes set contains an attribute with the given |
250 |
* |
* category. |
251 |
* @param category the category to test for |
* |
252 |
* |
* @param category the category to test for. |
253 |
* @return true if the category exists in this attribute set, false otherwise. |
* @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) |
public boolean containsKey(Class category) |
257 |
{ |
{ |
259 |
} |
} |
260 |
|
|
261 |
/** |
/** |
262 |
* Checks if this attribute set contains an entry with the given attribute. |
* Checks if this attribute set contains the given attribute. |
263 |
* |
* |
264 |
* @param attribute the attribute to test for |
* @param attribute the attribute to test for. |
265 |
* |
* @return <code>true</code> if the attribute is contained in the set, |
266 |
* @return true if the attribute exists in this attribute set, |
* <code>false</code> otherwise. |
|
* false otherwise. |
|
267 |
*/ |
*/ |
268 |
public boolean containsValue(Attribute attribute) |
public boolean containsValue(Attribute attribute) |
269 |
{ |
{ |
271 |
} |
} |
272 |
|
|
273 |
/** |
/** |
274 |
* Tests of obj is equal to this object. |
* 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 |
* @param obj the object to test |
* and the contained attributes are the same as in this set. |
277 |
* |
* |
278 |
* @return true if both objects are equal, false otherwise. |
* @param obj the Object to test. |
279 |
|
* @return <code>true</code> if equal, false otherwise. |
280 |
*/ |
*/ |
281 |
public boolean equals(Object obj) |
public boolean equals(Object obj) |
282 |
{ |
{ |
287 |
} |
} |
288 |
|
|
289 |
/** |
/** |
290 |
* Returns the attribute value that is connected to the given attribute |
* Returns the attribute object contained in this set for the given attribute |
291 |
* category. If the attribute set does not contains the given category null |
* category. |
292 |
* will be returned. |
* |
293 |
* |
* @param category the category of the attribute. A <code>Class</code> |
294 |
* @param category the attribute category to return the attribute value for |
* instance of a class implementing the <code>Attribute</code> interface. |
295 |
* |
* @return The attribute for this category or <code>null</code> if no |
296 |
* @return the attribute associated to category, or null |
* 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) |
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); |
return (Attribute) attributeMap.get(category); |
307 |
} |
} |
308 |
|
|
309 |
/** |
/** |
310 |
* Returns the hashcode for this object. |
* Returns the hashcode value. The hashcode value is the sum of all hashcodes |
311 |
* |
* of the attributes contained in this set. |
312 |
* @return the hashcode |
* |
313 |
|
* @return The hashcode for this attribute set. |
314 |
*/ |
*/ |
315 |
public int hashCode() |
public int hashCode() |
316 |
{ |
{ |
317 |
return attributeMap.hashCode() + interfaceName.hashCode(); |
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. |
* Checks if the attribute set is empty. |
327 |
* |
* |
328 |
* @return true if the attribute set is empty, false otherwise |
* @return <code>true</code> if the attribute set is empty, false otherwise. |
329 |
*/ |
*/ |
330 |
public boolean isEmpty() |
public boolean isEmpty() |
331 |
{ |
{ |
333 |
} |
} |
334 |
|
|
335 |
/** |
/** |
336 |
* Removes the entry with the given attribute in it. |
* 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 |
* @param attribute the attribute value of the entry to be removed |
* |
339 |
* |
* @param attribute the attribute to remove. |
340 |
* @return true if the attribute set has changed, false otherwise. |
* @return <code>true</code> if removed, false in all other cases. |
341 |
* |
* @throws UnmodifiableSetException if the set does not support modification. |
|
* @exception UnmodifiableSetException if this attribute set does not |
|
|
* support this action. |
|
342 |
*/ |
*/ |
343 |
public boolean remove(Attribute attribute) |
public boolean remove(Attribute attribute) |
344 |
{ |
{ |
349 |
} |
} |
350 |
|
|
351 |
/** |
/** |
352 |
* Removes the entry with the given category in it. |
* 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 |
* @param category the category value of the entry to be removed |
* |
355 |
* |
* @param category the category of the entry to be removed. |
356 |
* @return true if the attribute set has changed, false otherwise. |
* @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) |
public boolean remove(Class category) |
360 |
{ |
{ |
367 |
/** |
/** |
368 |
* Returns the number of elements in this attribute set. |
* Returns the number of elements in this attribute set. |
369 |
* |
* |
370 |
* @return the number of elements. |
* @return The number of elements. |
371 |
*/ |
*/ |
372 |
public int size() |
public int size() |
373 |
{ |
{ |
377 |
/** |
/** |
378 |
* Returns the content of the attribute set as an array |
* Returns the content of the attribute set as an array |
379 |
* |
* |
380 |
* @return an array of attributes |
* @return An array of attributes. |
381 |
*/ |
*/ |
382 |
public Attribute[] toArray() |
public Attribute[] toArray() |
383 |
{ |
{ |
384 |
int index = 0; |
int index = 0; |
385 |
Iterator it = attributeMap.entrySet().iterator(); |
Iterator it = attributeMap.values().iterator(); |
386 |
Attribute[] array = new Attribute[size()]; |
Attribute[] array = new Attribute[size()]; |
387 |
|
|
388 |
while (it.hasNext()) |
while (it.hasNext()) |