API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.print.attribute. HashAttributeSet View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510

/*
 * @(#)HashAttributeSet.java	1.13 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.print.attribute;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;

/**
 * Class HashAttributeSet provides an <code>AttributeSet</code>
 * implementation with characteristics of a hash map.
 * <P>
 *
 * @author  Alan Kaminsky
 */
public class HashAttributeSet implements AttributeSet, Serializable {

    private static final long serialVersionUID = 5311560590283707917L;

    /**
     * The interface of which all members of this attribute set must be an 
     * instance. It is assumed to be interface {@link Attribute Attribute}
     * or a subinterface thereof. 
     * @serial
     */
    private Class myInterface;

    /*
     * A HashMap used by the implementation.
     * The serialised form doesn't include this instance variable.
     */
    private transient HashMap attrMap = new HashMap();

    /**
     * Write the instance to a stream (ie serialize the object)
     *
     * @serialData
     * The serialized form of an attribute set explicitly writes the
     * number of attributes in the set, and each of the attributes.
     * This does not guarantee equality of serialized forms since
     * the order in which the attributes are written is not defined.
     */
    private void writeObject(ObjectOutputStream s) throws IOException {

	s.defaultWriteObject();
	Attribute [] attrs = toArray();
	s.writeInt(attrs.length);
	for (int i = 0; i < attrs.length; i++) {
	    s.writeObject(attrs[i]);
	}
    }

    /**
     * Reconstitute an instance from a stream that is, deserialize it).
     */
    private void readObject(ObjectInputStream s)
	throws ClassNotFoundException, IOException {
	
	s.defaultReadObject();
	attrMap = new HashMap();
	int count = s.readInt();
	Attribute attr;
	for (int i = 0; i < count; i++) {
	    attr = (Attribute)s.readObject();
	    add(attr);
	}	
    }

    /**
     * Construct a new, empty attribute set. 
     */
    public HashAttributeSet() {
	this(Attribute.class);
    }

    /**
     * Construct a new attribute set,
     * initially populated with the given attribute. 
     *
     * @param  attribute  Attribute value to add to the set.
     *
     * @exception  NullPointerException
     *     (unchecked exception) Thrown if <CODE>attribute</CODE> is null.
     */
    public HashAttributeSet(Attribute attribute) {
	this (attribute, Attribute.class);
    }

    /**
     * Construct a new attribute set,
     * initially populated with the values from the 
     * given array. The new attribute set is populated by 
     * adding the elements of <CODE>attributes</CODE> array to the set in 
     * sequence, starting at index 0. Thus, later array elements may replace 
     * earlier array elements if the array contains duplicate attribute 
     * values or attribute categories. 
     *
     * @param  attributes  Array of attribute values to add to the set. 
     *                    If null, an empty attribute set is constructed.
     *
     * @exception  NullPointerException
     *     (unchecked exception) Thrown if any element of
     *     <CODE>attributes</CODE> is null. 
     */
    public HashAttributeSet(Attribute[] attributes) {
	this (attributes, Attribute.class);
    }

    /**
     * Construct a new attribute set,
     * initially populated with the values from the  given set.
     *
     * @param  attributes Set of attributes from which to initialise this set.
     *                 If null, an empty attribute set is constructed.
     *
     */
    public HashAttributeSet(AttributeSet attributes) {
	this (attributes, Attribute.class);
    }
    
    /**
     * Construct a new, empty attribute set, where the members of
     * the attribute set are restricted to the given interface. 
     *
     * @param  interfaceName  The interface of which all members of this
     *                     attribute set must be an instance. It is assumed to 
     *                     be interface {@link Attribute Attribute} or a 
     *                     subinterface thereof.
     * @exception NullPointerException if interfaceName is null.
     */
    protected HashAttributeSet(Class<?> interfaceName) {
	if (interfaceName == null) {
	    throw new NullPointerException("null interface");
	}
	myInterface = interfaceName;
    }

    /**
     * Construct a new attribute set, initially populated with the given
     * attribute, where the members of the attribute set are restricted to the
     * given interface. 
     *
     * @param  attribute      Attribute value to add to the set.
     * @param  interfaceName  The interface of which all members of this
     *                    attribute set must be an instance. It is assumed to 
     *                    be interface {@link Attribute Attribute} or a 
     *                    subinterface thereof. 
     *
     * @exception  NullPointerException
     *     (unchecked exception) Thrown if <CODE>attribute</CODE> is null.
     * @exception NullPointerException if interfaceName is null.
     * @exception  ClassCastException
     *     (unchecked exception) Thrown if <CODE>attribute</CODE> is not an 
     *     instance of <CODE>interfaceName</CODE>. 
     */
    protected HashAttributeSet(Attribute attribute, Class<?> interfaceName) {
	if (interfaceName == null) {
	    throw new NullPointerException("null interface");
	}
	myInterface = interfaceName;
	add (attribute);
    }

    /**
     * Construct a new attribute set, where the members of the attribute
     * set are restricted to the given interface.
     * The new attribute set is populated 
     * by adding the elements of <CODE>attributes</CODE> array to the set in 
     * sequence, starting at index 0. Thus, later array elements may replace 
     * earlier array elements if the array contains duplicate attribute 
     * values or attribute categories. 
     *
     * @param  attributes Array of attribute values to add to the set. If
     *                    null, an empty attribute set is constructed.
     * @param  interfaceName  The interface of which all members of this
     *                    attribute set must be an instance. It is assumed to 
     *                    be interface {@link Attribute Attribute} or a 
     *                    subinterface thereof. 
     *
     * @exception  NullPointerException
     *     (unchecked exception) Thrown if any element of
     * <CODE>attributes</CODE> is null.
     * @exception NullPointerException if interfaceName is null.
     * @exception  ClassCastException
     *     (unchecked exception) Thrown if any element of
     * <CODE>attributes</CODE> is not an instance of
     * <CODE>interfaceName</CODE>. 
     */
    protected HashAttributeSet(Attribute[] attributes, Class<?> interfaceName) {
	if (interfaceName == null) {
	    throw new NullPointerException("null interface");
	}
	myInterface = interfaceName;
	int n = attributes == null ? 0 : attributes.length;
	for (int i = 0; i < n; ++ i) {
	    add (attributes[i]);
	}
    }

    /**
     * Construct a new attribute set, initially populated with the
     * values from the  given set where the members of the attribute
     * set are restricted to the given interface.
     *
     * @param  attributes set of attribute values to initialise the set. If
     *                    null, an empty attribute set is constructed.
     * @param  interfaceName  The interface of which all members of this
     *                    attribute set must be an instance. It is assumed to 
     *                    be interface {@link Attribute Attribute} or a 
     *                    subinterface thereof. 
     *
     * @exception  ClassCastException
     *     (unchecked exception) Thrown if any element of
     * <CODE>attributes</CODE> is not an instance of
     * <CODE>interfaceName</CODE>. 
     */
    protected HashAttributeSet(AttributeSet attributes, Class<?> interfaceName) {
      myInterface = interfaceName;
      if (attributes != null) {
	Attribute[] attribArray = attributes.toArray();	    
	int n = attribArray == null ? 0 : attribArray.length;
	for (int i = 0; i < n; ++ i) {
	  add (attribArray[i]);
	}
      }
    }

    /**
     * Returns the attribute value which this attribute set contains in the 
     * given attribute category. Returns <tt>null</tt> if this attribute set 
     * does not contain any attribute value in the given attribute category.
     *
     * @param  category  Attribute category whose associated attribute value
     *                   is to be returned. It must be a
     *                   {@link java.lang.Class Class}
     *                   that implements interface {@link Attribute 
     *                   Attribute}. 
     *
     * @return  The attribute value in the given attribute category contained
     *          in this attribute set, or <tt>null</tt> if this attribute set
     *          does not contain any attribute value in the given attribute
     *          category. 
     * 
     * @throws  NullPointerException
     *     (unchecked exception) Thrown if the <CODE>category</CODE> is null.
     * @throws  ClassCastException
     *     (unchecked exception) Thrown if the <CODE>category</CODE> is not a 
     *     {@link java.lang.Class Class} that implements interface {@link 
     *     Attribute Attribute}. 
     */    
    public Attribute get(Class<?> category) {
	return (Attribute)
	    attrMap.get(AttributeSetUtilities.
			verifyAttributeCategory(category,
						Attribute.class));
    }

    /**
     * Adds the specified attribute to this attribute set if it is not 
     * already present, first removing any existing in the same
     * attribute category as the specified attribute value. 
     *
     * @param  attribute  Attribute value to be added to this attribute set.
     *
     * @return  <tt>true</tt> if this attribute set changed as a result of the
     *          call, i.e., the given attribute value was not already a 
     *          member of this attribute set. 
     * 
     * @throws  NullPointerException
     *    (unchecked exception) Thrown if the <CODE>attribute</CODE> is null. 
     * @throws  UnmodifiableSetException
     *    (unchecked exception) Thrown if this attribute set does not support 
     *     the <CODE>add()</CODE> operation. 
     */
    public boolean add(Attribute attribute) {
	Object oldAttribute = 
	    attrMap.put(attribute.getCategory(),
			AttributeSetUtilities.
			verifyAttributeValue(attribute, myInterface));
	return (!attribute.equals(oldAttribute));
    }

    /**
     * Removes any attribute for this category from this attribute set if  
     * present. If <CODE>category</CODE> is null, then 
     * <CODE>remove()</CODE> does nothing and returns <tt>false</tt>. 
     *
     * @param  category Attribute category to be removed from this
     *                  attribute set.
     *
     * @return  <tt>true</tt> if this attribute set changed as a result of the
     *         call, i.e., the given attribute category had been a member of 
     *         this attribute set. 
     * 
     * @throws  UnmodifiableSetException
     *     (unchecked exception) Thrown if this attribute set does not 
     *     support the <CODE>remove()</CODE> operation. 
     */
    public boolean remove(Class<?> category) {
	return
	    category != null &&
	    AttributeSetUtilities.
	    verifyAttributeCategory(category, Attribute.class) != null &&
	    attrMap.remove(category) != null;
    }

    /**
     * Removes the specified attribute from this attribute set if  
     * present. If <CODE>attribute</CODE> is null, then 
     * <CODE>remove()</CODE> does nothing and returns <tt>false</tt>. 
     *
     * @param attribute Attribute value to be removed from this attribute set.
     *
     * @return  <tt>true</tt> if this attribute set changed as a result of the
     *         call, i.e., the given attribute value had been a member of 
     *         this attribute set. 
     * 
     * @throws  UnmodifiableSetException
     *     (unchecked exception) Thrown if this attribute set does not 
     *     support the <CODE>remove()</CODE> operation. 
     */
    public boolean remove(Attribute attribute) {
	return
	    attribute != null &&
	    attrMap.remove(attribute.getCategory()) != null;
    }

    /**
     * Returns <tt>true</tt> if this attribute set contains an 
     * attribute for the specified category.
     *
     * @param  category whose presence in this attribute set is
     *            to be tested.
     *
     * @return  <tt>true</tt> if this attribute set contains an attribute 
     *         value for the specified category.
     */
    public boolean containsKey(Class<?> category) {
	return
	    category != null &&
	    AttributeSetUtilities.
	    verifyAttributeCategory(category, Attribute.class) != null &&
	    attrMap.get(category) != null;
    }

    /**
     * Returns <tt>true</tt> if this attribute set contains the given  
     * attribute.
     *
     * @param  attribute  value whose presence in this attribute set is
     *            to be tested.
     *
     * @return  <tt>true</tt> if this attribute set contains the given 
     *      attribute    value.
     */
    public boolean containsValue(Attribute attribute) {
	return
	   attribute != null &&
	   attribute instanceof Attribute &&
	   attribute.equals(attrMap.get(((Attribute)attribute).getCategory()));
    }

    /**
     * Adds all of the elements in the specified set to this attribute.
     * The outcome is the same as if the
     * {@link #add(Attribute) <CODE>add(Attribute)</CODE>} 
     * operation had been applied to this attribute set successively with 
     * each element from the specified set.
     * The behavior of the <CODE>addAll(AttributeSet)</CODE>
     * operation is unspecified if the specified set is modified while
     * the operation is in progress.
     * <P>
     * If the <CODE>addAll(AttributeSet)</CODE> operation throws an exception,
     * the effect on this attribute set's state is implementation dependent;
     * elements from the specified set before the point of the exception may
     * or may not have been added to this attribute set. 
     *
     * @param  attributes  whose elements are to be added to this attribute 
     *            set.
     *
     * @return  <tt>true</tt> if this attribute set changed as a result of the
     *          call.
     * 
     * @throws  UnmodifiableSetException
     *    (Unchecked exception) Thrown if this attribute set does not 
     *     support the <tt>addAll(AttributeSet)</tt> method. 
     * @throws  NullPointerException
     *     (Unchecked exception) Thrown if some element in the specified 
     *     set is null, or the set is null.
     *
     * @see #add(Attribute)
     */
    public boolean addAll(AttributeSet attributes) {
	
	Attribute []attrs = attributes.toArray();
	boolean result = false;
	for (int i=0; i<attrs.length; i++) {
	    Attribute newValue =
		AttributeSetUtilities.verifyAttributeValue(attrs[i], 
							   myInterface);
	    Object oldValue = attrMap.put(newValue.getCategory(), newValue);
	    result = (! newValue.equals(oldValue)) || result;
	}
	return result;
    }
 
    /**
     * Returns the number of attributes in this attribute set. If this
     * attribute set contains more than <tt>Integer.MAX_VALUE</tt> elements,
     * returns  <tt>Integer.MAX_VALUE</tt>.
     *
     * @return  The number of attributes in this attribute set.
     */
    public int size() {
	return attrMap.size();
    }
    
    /**
     * 
     * @return the Attributes contained in this set as an array, zero length
     * if the AttributeSet is empty.
     */
    public Attribute[] toArray() {
	Attribute []attrs = new Attribute[size()];
	attrMap.values().toArray(attrs);
	return attrs;
    }


    /**
     * Removes all attributes from this attribute set.
     *
     * @throws  UnmodifiableSetException
     *   (unchecked exception) Thrown if this attribute set does not support 
     *     the <CODE>clear()</CODE> operation. 
     */
    public void clear() {
	attrMap.clear();
    }

   /**
     * Returns true if this attribute set contains no attributes.
     *
     * @return true if this attribute set contains no attributes.
     */
    public boolean isEmpty() {
	return attrMap.isEmpty();
    }

    /**
     * Compares the specified object with this attribute set for equality. 
     * Returns <tt>true</tt> if the given object is also an attribute set and 
     * the two attribute sets contain the same attribute category-attribute 
     * value mappings. This ensures that the 
     * <tt>equals()</tt> method works properly across different  
     * implementations of the AttributeSet interface. 
     *
     * @param  object to be compared for equality with this attribute set.
     *
     * @return  <tt>true</tt> if the specified object is equal to this 
     *       attribute   set.
     */
 
    public boolean equals(Object object) {
	if (object == null || !(object instanceof AttributeSet)) {
	    return false;
	}

	AttributeSet aset = (AttributeSet)object;
	if (aset.size() != size()) {
	    return false;
	}

	Attribute[] attrs = toArray();    
	for (int i=0;i<attrs.length; i++) {
	    if (!aset.containsValue(attrs[i])) {
		return false;
	    }
	}
	return true;
    }
    
    /**
     * Returns the hash code value for this attribute set.
     * The hash code of an attribute set is defined to be the sum
     * of the hash codes of each entry in the AttributeSet. 
     * This ensures that <tt>t1.equals(t2)</tt> implies that 
     * <tt>t1.hashCode()==t2.hashCode()</tt> for any two attribute sets 
     * <tt>t1</tt> and <tt>t2</tt>, as required by the general contract of 
     * {@link java.lang.Object#hashCode() <CODE>Object.hashCode()</CODE>}. 
     *
     * @return  The hash code value for this attribute set.
     */
    public int hashCode() {
	int hcode = 0;
	Attribute[] attrs = toArray();    
	for (int i=0;i<attrs.length; i++) {
	    hcode += attrs[i].hashCode();
	}
	return hcode;
    }

}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar