KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > print > attribute > HashAttributeSet


1 /*
2  * @(#)HashAttributeSet.java 1.12 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.print.attribute;
9
10 import java.io.IOException JavaDoc;
11 import java.io.ObjectInputStream JavaDoc;
12 import java.io.ObjectOutputStream JavaDoc;
13 import java.io.Serializable JavaDoc;
14 import java.util.HashMap JavaDoc;
15
16 /**
17  * Class HashAttributeSet provides an <code>AttributeSet</code>
18  * implementation with characteristics of a hash map.
19  * <P>
20  *
21  * @author Alan Kaminsky
22  */

23 public class HashAttributeSet implements AttributeSet JavaDoc, Serializable JavaDoc {
24
25     private static final long serialVersionUID = 5311560590283707917L;
26
27     /**
28      * The interface of which all members of this attribute set must be an
29      * instance. It is assumed to be interface {@link Attribute Attribute}
30      * or a subinterface thereof.
31      * @serial
32      */

33     private Class JavaDoc myInterface;
34
35     /*
36      * A HashMap used by the implementation.
37      * The serialised form doesn't include this instance variable.
38      */

39     private transient HashMap JavaDoc attrMap = new HashMap JavaDoc();
40
41     /**
42      * Write the instance to a stream (ie serialize the object)
43      *
44      * @serialData
45      * The serialized form of an attribute set explicitly writes the
46      * number of attributes in the set, and each of the attributes.
47      * This does not guarantee equality of serialized forms since
48      * the order in which the attributes are written is not defined.
49      */

50     private void writeObject(ObjectOutputStream JavaDoc s) throws IOException JavaDoc {
51
52     s.defaultWriteObject();
53     Attribute JavaDoc [] 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     /**
61      * Reconstitute an instance from a stream that is, deserialize it).
62      */

63     private void readObject(ObjectInputStream JavaDoc s)
64     throws ClassNotFoundException JavaDoc, IOException JavaDoc {
65     
66     s.defaultReadObject();
67     attrMap = new HashMap JavaDoc();
68     int count = s.readInt();
69     Attribute JavaDoc attr;
70     for (int i = 0; i < count; i++) {
71         attr = (Attribute JavaDoc)s.readObject();
72         add(attr);
73     }
74     }
75
76     /**
77      * Construct a new, empty attribute set.
78      */

79     public HashAttributeSet() {
80     this(Attribute JavaDoc.class);
81     }
82
83     /**
84      * Construct a new attribute set,
85      * initially populated with the given attribute.
86      *
87      * @param attribute Attribute value to add to the set.
88      *
89      * @exception NullPointerException
90      * (unchecked exception) Thrown if <CODE>attribute</CODE> is null.
91      */

92     public HashAttributeSet(Attribute JavaDoc attribute) {
93     this (attribute, Attribute JavaDoc.class);
94     }
95
96     /**
97      * Construct a new attribute set,
98      * initially populated with the values from the
99      * given array. The new attribute set is populated by
100      * adding the elements of <CODE>attributes</CODE> array to the set in
101      * sequence, starting at index 0. Thus, later array elements may replace
102      * earlier array elements if the array contains duplicate attribute
103      * values or attribute categories.
104      *
105      * @param attributes Array of attribute values to add to the set.
106      * If null, an empty attribute set is constructed.
107      *
108      * @exception NullPointerException
109      * (unchecked exception) Thrown if any element of
110      * <CODE>attributes</CODE> is null.
111      */

112     public HashAttributeSet(Attribute JavaDoc[] attributes) {
113     this (attributes, Attribute JavaDoc.class);
114     }
115
116     /**
117      * Construct a new attribute set,
118      * initially populated with the values from the given set.
119      *
120      * @param attributes Set of attributes from which to initialise this set.
121      * If null, an empty attribute set is constructed.
122      *
123      */

124     public HashAttributeSet(AttributeSet JavaDoc attributes) {
125     this (attributes, Attribute JavaDoc.class);
126     }
127     
128     /**
129      * Construct a new, empty attribute set, where the members of
130      * the attribute set are restricted to the given interface.
131      *
132      * @param interfaceName The interface of which all members of this
133      * attribute set must be an instance. It is assumed to
134      * be interface {@link Attribute Attribute} or a
135      * subinterface thereof.
136      * @exception NullPointerException if interfaceName is null.
137      */

138     protected HashAttributeSet(Class JavaDoc<?> interfaceName) {
139     if (interfaceName == null) {
140         throw new NullPointerException JavaDoc("null interface");
141     }
142     myInterface = interfaceName;
143     }
144
145     /**
146      * Construct a new attribute set, initially populated with the given
147      * attribute, where the members of the attribute set are restricted to the
148      * given interface.
149      *
150      * @param attribute Attribute value to add to the set.
151      * @param interfaceName The interface of which all members of this
152      * attribute set must be an instance. It is assumed to
153      * be interface {@link Attribute Attribute} or a
154      * subinterface thereof.
155      *
156      * @exception NullPointerException
157      * (unchecked exception) Thrown if <CODE>attribute</CODE> is null.
158      * @exception NullPointerException if interfaceName is null.
159      * @exception ClassCastException
160      * (unchecked exception) Thrown if <CODE>attribute</CODE> is not an
161      * instance of <CODE>interfaceName</CODE>.
162      */

163     protected HashAttributeSet(Attribute JavaDoc attribute, Class JavaDoc<?> interfaceName) {
164     if (interfaceName == null) {
165         throw new NullPointerException JavaDoc("null interface");
166     }
167     myInterface = interfaceName;
168     add (attribute);
169     }
170
171     /**
172      * Construct a new attribute set, where the members of the attribute
173      * set are restricted to the given interface.
174      * The new attribute set is populated
175      * by adding the elements of <CODE>attributes</CODE> array to the set in
176      * sequence, starting at index 0. Thus, later array elements may replace
177      * earlier array elements if the array contains duplicate attribute
178      * values or attribute categories.
179      *
180      * @param attributes Array of attribute values to add to the set. If
181      * null, an empty attribute set is constructed.
182      * @param interfaceName The interface of which all members of this
183      * attribute set must be an instance. It is assumed to
184      * be interface {@link Attribute Attribute} or a
185      * subinterface thereof.
186      *
187      * @exception NullPointerException
188      * (unchecked exception) Thrown if any element of
189      * <CODE>attributes</CODE> is null.
190      * @exception NullPointerException if interfaceName is null.
191      * @exception ClassCastException
192      * (unchecked exception) Thrown if any element of
193      * <CODE>attributes</CODE> is not an instance of
194      * <CODE>interfaceName</CODE>.
195      */

196     protected HashAttributeSet(Attribute JavaDoc[] attributes, Class JavaDoc<?> interfaceName) {
197     if (interfaceName == null) {
198         throw new NullPointerException JavaDoc("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     /**
208      * Construct a new attribute set, initially populated with the
209      * values from the given set where the members of the attribute
210      * set are restricted to the given interface.
211      *
212      * @param attributes set of attribute values to initialise the set. If
213      * null, an empty attribute set is constructed.
214      * @param interfaceName The interface of which all members of this
215      * attribute set must be an instance. It is assumed to
216      * be interface {@link Attribute Attribute} or a
217      * subinterface thereof.
218      *
219      * @exception ClassCastException
220      * (unchecked exception) Thrown if any element of
221      * <CODE>attributes</CODE> is not an instance of
222      * <CODE>interfaceName</CODE>.
223      */

224     protected HashAttributeSet(AttributeSet JavaDoc attributes, Class JavaDoc<?> interfaceName) {
225       myInterface = interfaceName;
226       if (attributes != null) {
227     Attribute JavaDoc[] 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     /**
236      * Returns the attribute value which this attribute set contains in the
237      * given attribute category. Returns <tt>null</tt> if this attribute set
238      * does not contain any attribute value in the given attribute category.
239      *
240      * @param category Attribute category whose associated attribute value
241      * is to be returned. It must be a
242      * {@link java.lang.Class Class}
243      * that implements interface {@link Attribute
244      * Attribute}.
245      *
246      * @return The attribute value in the given attribute category contained
247      * in this attribute set, or <tt>null</tt> if this attribute set
248      * does not contain any attribute value in the given attribute
249      * category.
250      *
251      * @throws NullPointerException
252      * (unchecked exception) Thrown if the <CODE>category</CODE> is null.
253      * @throws ClassCastException
254      * (unchecked exception) Thrown if the <CODE>category</CODE> is not a
255      * {@link java.lang.Class Class} that implements interface {@link
256      * Attribute Attribute}.
257      */

258     public Attribute JavaDoc get(Class JavaDoc<?> category) {
259     return (Attribute JavaDoc)
260         attrMap.get(AttributeSetUtilities.
261             verifyAttributeCategory(category,
262                         Attribute JavaDoc.class));
263     }
264
265     /**
266      * Adds the specified attribute to this attribute set if it is not
267      * already present, first removing any existing in the same
268      * attribute category as the specified attribute value.
269      *
270      * @param attribute Attribute value to be added to this attribute set.
271      *
272      * @return <tt>true</tt> if this attribute set changed as a result of the
273      * call, i.e., the given attribute value was not already a
274      * member of this attribute set.
275      *
276      * @throws NullPointerException
277      * (unchecked exception) Thrown if the <CODE>attribute</CODE> is null.
278      * @throws UnmodifiableSetException
279      * (unchecked exception) Thrown if this attribute set does not support
280      * the <CODE>add()</CODE> operation.
281      */

282     public boolean add(Attribute JavaDoc attribute) {
283     Object JavaDoc oldAttribute =
284         attrMap.put(attribute.getCategory(),
285             AttributeSetUtilities.
286             verifyAttributeValue(attribute, myInterface));
287     return (!attribute.equals(oldAttribute));
288     }
289
290     /**
291      * Removes any attribute for this category from this attribute set if
292      * present. If <CODE>category</CODE> is null, then
293      * <CODE>remove()</CODE> does nothing and returns <tt>false</tt>.
294      *
295      * @param category Attribute category to be removed from this
296      * attribute set.
297      *
298      * @return <tt>true</tt> if this attribute set changed as a result of the
299      * call, i.e., the given attribute category had been a member of
300      * this attribute set.
301      *
302      * @throws UnmodifiableSetException
303      * (unchecked exception) Thrown if this attribute set does not
304      * support the <CODE>remove()</CODE> operation.
305      */

306     public boolean remove(Class JavaDoc<?> category) {
307     return
308         category != null &&
309         AttributeSetUtilities.
310         verifyAttributeCategory(category, Attribute JavaDoc.class) != null &&
311         attrMap.remove(category) != null;
312     }
313
314     /**
315      * Removes the specified attribute from this attribute set if
316      * present. If <CODE>attribute</CODE> is null, then
317      * <CODE>remove()</CODE> does nothing and returns <tt>false</tt>.
318      *
319      * @param attribute Attribute value to be removed from this attribute set.
320      *
321      * @return <tt>true</tt> if this attribute set changed as a result of the
322      * call, i.e., the given attribute value had been a member of
323      * this attribute set.
324      *
325      * @throws UnmodifiableSetException
326      * (unchecked exception) Thrown if this attribute set does not
327      * support the <CODE>remove()</CODE> operation.
328      */

329     public boolean remove(Attribute JavaDoc attribute) {
330     return
331         attribute != null &&
332         attrMap.remove(attribute.getCategory()) != null;
333     }
334
335     /**
336      * Returns <tt>true</tt> if this attribute set contains an
337      * attribute for the specified category.
338      *
339      * @param category whose presence in this attribute set is
340      * to be tested.
341      *
342      * @return <tt>true</tt> if this attribute set contains an attribute
343      * value for the specified category.
344      */

345     public boolean containsKey(Class JavaDoc<?> category) {
346     return
347         category != null &&
348         AttributeSetUtilities.
349         verifyAttributeCategory(category, Attribute JavaDoc.class) != null &&
350         attrMap.get(category) != null;
351     }
352
353     /**
354      * Returns <tt>true</tt> if this attribute set contains the given
355      * attribute.
356      *
357      * @param attribute value whose presence in this attribute set is
358      * to be tested.
359      *
360      * @return <tt>true</tt> if this attribute set contains the given
361      * attribute value.
362      */

363     public boolean containsValue(Attribute JavaDoc attribute) {
364     return
365        attribute != null &&
366        attribute instanceof Attribute JavaDoc &&
367        attribute.equals(attrMap.get(((Attribute JavaDoc)attribute).getCategory()));
368     }
369
370     /**
371      * Adds all of the elements in the specified set to this attribute.
372      * The outcome is the same as if the
373      * {@link #add(Attribute) <CODE>add(Attribute)</CODE>}
374      * operation had been applied to this attribute set successively with
375      * each element from the specified set.
376      * The behavior of the <CODE>addAll(AttributeSet)</CODE>
377      * operation is unspecified if the specified set is modified while
378      * the operation is in progress.
379      * <P>
380      * If the <CODE>addAll(AttributeSet)</CODE> operation throws an exception,
381      * the effect on this attribute set's state is implementation dependent;
382      * elements from the specified set before the point of the exception may
383      * or may not have been added to this attribute set.
384      *
385      * @param attributes whose elements are to be added to this attribute
386      * set.
387      *
388      * @return <tt>true</tt> if this attribute set changed as a result of the
389      * call.
390      *
391      * @throws UnmodifiableSetException
392      * (Unchecked exception) Thrown if this attribute set does not
393      * support the <tt>addAll(AttributeSet)</tt> method.
394      * @throws NullPointerException
395      * (Unchecked exception) Thrown if some element in the specified
396      * set is null, or the set is null.
397      *
398      * @see #add(Attribute)
399      */

400     public boolean addAll(AttributeSet JavaDoc attributes) {
401     
402     Attribute JavaDoc []attrs = attributes.toArray();
403     boolean result = false;
404     for (int i=0; i<attrs.length; i++) {
405         Attribute JavaDoc newValue =
406         AttributeSetUtilities.verifyAttributeValue(attrs[i],
407                                myInterface);
408         Object JavaDoc oldValue = attrMap.put(newValue.getCategory(), newValue);
409         result = (! newValue.equals(oldValue)) || result;
410     }
411     return result;
412     }
413  
414     /**
415      * Returns the number of attributes in this attribute set. If this
416      * attribute set contains more than <tt>Integer.MAX_VALUE</tt> elements,
417      * returns <tt>Integer.MAX_VALUE</tt>.
418      *
419      * @return The number of attributes in this attribute set.
420      */

421     public int size() {
422     return attrMap.size();
423     }
424     
425     /**
426      *
427      * @return the Attributes contained in this set as an array, zero length
428      * if the AttributeSet is empty.
429      */

430     public Attribute JavaDoc[] toArray() {
431     Attribute JavaDoc []attrs = new Attribute JavaDoc[size()];
432     attrMap.values().toArray(attrs);
433     return attrs;
434     }
435
436
437     /**
438      * Removes all attributes from this attribute set.
439      *
440      * @throws UnmodifiableSetException
441      * (unchecked exception) Thrown if this attribute set does not support
442      * the <CODE>clear()</CODE> operation.
443      */

444     public void clear() {
445     attrMap.clear();
446     }
447
448    /**
449      * Returns true if this attribute set contains no attributes.
450      *
451      * @return true if this attribute set contains no attributes.
452      */

453     public boolean isEmpty() {
454     return attrMap.isEmpty();
455     }
456
457     /**
458      * Compares the specified object with this attribute set for equality.
459      * Returns <tt>true</tt> if the given object is also an attribute set and
460      * the two attribute sets contain the same attribute category-attribute
461      * value mappings. This ensures that the
462      * <tt>equals()</tt> method works properly across different
463      * implementations of the AttributeSet interface.
464      *
465      * @param object to be compared for equality with this attribute set.
466      *
467      * @return <tt>true</tt> if the specified object is equal to this
468      * attribute set.
469      */

470  
471     public boolean equals(Object JavaDoc object) {
472     if (object == null || !(object instanceof AttributeSet JavaDoc)) {
473         return false;
474     }
475
476     AttributeSet JavaDoc aset = (AttributeSet JavaDoc)object;
477     if (aset.size() != size()) {
478         return false;
479     }
480
481     Attribute JavaDoc[] 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     /**
491      * Returns the hash code value for this attribute set.
492      * The hash code of an attribute set is defined to be the sum
493      * of the hash codes of each entry in the AttributeSet.
494      * This ensures that <tt>t1.equals(t2)</tt> implies that
495      * <tt>t1.hashCode()==t2.hashCode()</tt> for any two attribute sets
496      * <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of
497      * {@link java.lang.Object#hashCode() <CODE>Object.hashCode()</CODE>}.
498      *
499      * @return The hash code value for this attribute set.
500      */

501     public int hashCode() {
502     int hcode = 0;
503     Attribute JavaDoc[] 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