Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/print/attribute/HashAttributeSet.java
41159 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.print.attribute;
27
28
import java.io.IOException;
29
import java.io.ObjectInputStream;
30
import java.io.ObjectOutputStream;
31
import java.io.Serial;
32
import java.io.Serializable;
33
import java.util.HashMap;
34
35
/**
36
* Class {@code HashAttributeSet} provides an {@code AttributeSet}
37
* implementation with characteristics of a hash map.
38
*
39
* @author Alan Kaminsky
40
*/
41
public class HashAttributeSet implements AttributeSet, Serializable {
42
43
/**
44
* Use serialVersionUID from JDK 1.4 for interoperability.
45
*/
46
@Serial
47
private static final long serialVersionUID = 5311560590283707917L;
48
49
/**
50
* The interface of which all members of this attribute set must be an
51
* instance. It is assumed to be interface {@link Attribute Attribute} or a
52
* subinterface thereof.
53
*
54
* @serial
55
*/
56
private Class<?> myInterface;
57
58
/**
59
* A {@code HashMap} used by the implementation. The serialised form doesn't
60
* include this instance variable.
61
*/
62
private transient HashMap<Class<?>, Attribute> attrMap = new HashMap<>();
63
64
/**
65
* Write the instance to a stream (ie serialize the object).
66
*
67
* @param s the output stream
68
* @throws IOException if an I/O exception has occurred
69
* @serialData The serialized form of an attribute set explicitly writes the
70
* number of attributes in the set, and each of the attributes.
71
* This does not guarantee equality of serialized forms since
72
* the order in which the attributes are written is not defined.
73
*/
74
@Serial
75
private void writeObject(ObjectOutputStream s) throws IOException {
76
77
s.defaultWriteObject();
78
Attribute [] attrs = toArray();
79
s.writeInt(attrs.length);
80
for (int i = 0; i < attrs.length; i++) {
81
s.writeObject(attrs[i]);
82
}
83
}
84
85
/**
86
* Reconstitute an instance from a stream that is, deserialize it).
87
*
88
* @param s the input stream
89
* @throws ClassNotFoundException if the class is not found
90
* @throws IOException if an I/O exception has occurred
91
*/
92
@Serial
93
private void readObject(ObjectInputStream s)
94
throws ClassNotFoundException, IOException {
95
96
s.defaultReadObject();
97
attrMap = new HashMap<>();
98
int count = s.readInt();
99
Attribute attr;
100
for (int i = 0; i < count; i++) {
101
attr = (Attribute)s.readObject();
102
add(attr);
103
}
104
}
105
106
/**
107
* Construct a new, empty attribute set.
108
*/
109
public HashAttributeSet() {
110
this(Attribute.class);
111
}
112
113
/**
114
* Construct a new attribute set, initially populated with the given
115
* attribute.
116
*
117
* @param attribute attribute value to add to the set
118
* @throws NullPointerException if {@code attribute} is {@code null}
119
*/
120
public HashAttributeSet(Attribute attribute) {
121
this (attribute, Attribute.class);
122
}
123
124
/**
125
* Construct a new attribute set, initially populated with the values from
126
* the given array. The new attribute set is populated by adding the
127
* elements of {@code attributes} array to the set in sequence, starting at
128
* index 0. Thus, later array elements may replace earlier array elements if
129
* the array contains duplicate attribute values or attribute categories.
130
*
131
* @param attributes array of attribute values to add to the set. If
132
* {@code null}, an empty attribute set is constructed.
133
* @throws NullPointerException if any element of {@code attributes} is
134
* {@code null}
135
*/
136
public HashAttributeSet(Attribute[] attributes) {
137
this (attributes, Attribute.class);
138
}
139
140
/**
141
* Construct a new attribute set, initially populated with the values from
142
* the given set.
143
*
144
* @param attributes set of attributes from which to initialise this set.
145
* If {@code null}, an empty attribute set is constructed.
146
*/
147
public HashAttributeSet(AttributeSet attributes) {
148
this (attributes, Attribute.class);
149
}
150
151
/**
152
* Construct a new, empty attribute set, where the members of the attribute
153
* set are restricted to the given interface.
154
*
155
* @param interfaceName the interface of which all members of this
156
* attribute set must be an instance. It is assumed to be interface
157
* {@link Attribute Attribute} or a subinterface thereof.
158
* @throws NullPointerException if {@code interfaceName} is {@code null}
159
*/
160
protected HashAttributeSet(Class<?> interfaceName) {
161
if (interfaceName == null) {
162
throw new NullPointerException("null interface");
163
}
164
myInterface = interfaceName;
165
}
166
167
/**
168
* Construct a new attribute set, initially populated with the given
169
* attribute, where the members of the attribute set are restricted to the
170
* given interface.
171
*
172
* @param attribute attribute value to add to the set
173
* @param interfaceName the interface of which all members of this
174
* attribute set must be an instance. It is assumed to be interface
175
* {@link Attribute Attribute} or a subinterface thereof.
176
* @throws NullPointerException if {@code attribute} or
177
* {@code interfaceName} are {@code null}
178
* @throws ClassCastException if {@code attribute} is not an instance of
179
* {@code interfaceName}
180
*/
181
protected HashAttributeSet(Attribute attribute, Class<?> interfaceName) {
182
if (interfaceName == null) {
183
throw new NullPointerException("null interface");
184
}
185
myInterface = interfaceName;
186
add (attribute);
187
}
188
189
/**
190
* Construct a new attribute set, where the members of the attribute set are
191
* restricted to the given interface. The new attribute set is populated by
192
* adding the elements of {@code attributes} array to the set in sequence,
193
* starting at index 0. Thus, later array elements may replace earlier array
194
* elements if the array contains duplicate attribute values or attribute
195
* categories.
196
*
197
* @param attributes array of attribute values to add to the set. If
198
* {@code null}, an empty attribute set is constructed.
199
* @param interfaceName the interface of which all members of this
200
* attribute set must be an instance. It is assumed to be interface
201
* {@link Attribute Attribute} or a subinterface thereof.
202
* @throws NullPointerException if {@code interfaceName} is {@code null}, or
203
* if any element of {@code attributes} is {@code null}
204
* @throws ClassCastException if any element of {@code attributes} is not an
205
* instance of {@code interfaceName}
206
*/
207
protected HashAttributeSet(Attribute[] attributes, Class<?> interfaceName) {
208
if (interfaceName == null) {
209
throw new NullPointerException("null interface");
210
}
211
myInterface = interfaceName;
212
int n = attributes == null ? 0 : attributes.length;
213
for (int i = 0; i < n; ++ i) {
214
add (attributes[i]);
215
}
216
}
217
218
/**
219
* Construct a new attribute set, initially populated with the values from
220
* the given set where the members of the attribute set are restricted to
221
* the given interface.
222
*
223
* @param attributes set of attribute values to initialise the set. If
224
* {@code null}, an empty attribute set is constructed.
225
* @param interfaceName The interface of which all members of this
226
* attribute set must be an instance. It is assumed to be interface
227
* {@link Attribute Attribute} or a subinterface thereof.
228
* @throws ClassCastException if any element of {@code attributes} is not an
229
* instance of {@code interfaceName}
230
*/
231
protected HashAttributeSet(AttributeSet attributes, Class<?> interfaceName) {
232
myInterface = interfaceName;
233
if (attributes != null) {
234
Attribute[] attribArray = attributes.toArray();
235
int n = attribArray == null ? 0 : attribArray.length;
236
for (int i = 0; i < n; ++ i) {
237
add (attribArray[i]);
238
}
239
}
240
}
241
242
/**
243
* Returns the attribute value which this attribute set contains in the
244
* given attribute category. Returns {@code null} if this attribute set does
245
* not contain any attribute value in the given attribute category.
246
*
247
* @param category attribute category whose associated attribute value is
248
* to be returned. It must be a {@link Class Class} that implements
249
* interface {@link Attribute Attribute}.
250
* @return the attribute value in the given attribute category contained in
251
* this attribute set, or {@code null} if this attribute set does
252
* not contain any attribute value in the given attribute category
253
* @throws NullPointerException if the {@code category} is {@code null}
254
* @throws ClassCastException if the {@code category} is not a
255
* {@link Class Class} that implements interface
256
* {@link Attribute Attribute}
257
*/
258
public Attribute get(Class<?> category) {
259
return attrMap.get(AttributeSetUtilities.
260
verifyAttributeCategory(category,
261
Attribute.class));
262
}
263
264
/**
265
* Adds the specified attribute to this attribute set if it is not already
266
* present, first removing any existing in the same attribute category as
267
* the specified attribute value.
268
*
269
* @param attribute attribute value to be added to this attribute set
270
* @return {@code true} if this attribute set changed as a result of the
271
* call, i.e., the given attribute value was not already a member of
272
* this attribute set
273
* @throws NullPointerException if the {@code attribute} is {@code null}
274
* @throws UnmodifiableSetException if this attribute set does not support
275
* the {@code add()} operation
276
*/
277
public boolean add(Attribute attribute) {
278
Object oldAttribute =
279
attrMap.put(attribute.getCategory(),
280
AttributeSetUtilities.
281
verifyAttributeValue(attribute, myInterface));
282
return (!attribute.equals(oldAttribute));
283
}
284
285
/**
286
* Removes any attribute for this category from this attribute set if
287
* present. If {@code category} is {@code null}, then {@code remove()} does
288
* nothing and returns {@code false}.
289
*
290
* @param category attribute category to be removed from this attribute set
291
* @return {@code true} if this attribute set changed as a result of the
292
* call, i.e., the given attribute category had been a member of
293
* this attribute set
294
* @throws UnmodifiableSetException if this attribute set does not support
295
* the {@code remove()} operation
296
*/
297
public boolean remove(Class<?> category) {
298
return
299
category != null &&
300
AttributeSetUtilities.
301
verifyAttributeCategory(category, Attribute.class) != null &&
302
attrMap.remove(category) != null;
303
}
304
305
/**
306
* Removes the specified attribute from this attribute set if present. If
307
* {@code attribute} is {@code null}, then {@code remove()} does nothing and
308
* returns {@code false}.
309
*
310
* @param attribute attribute value to be removed from this attribute set
311
* @return {@code true} if this attribute set changed as a result of the
312
* call, i.e., the given attribute value had been a member of this
313
* attribute set
314
* @throws UnmodifiableSetException if this attribute set does not support
315
* the {@code remove()} operation
316
*/
317
public boolean remove(Attribute attribute) {
318
return
319
attribute != null &&
320
attrMap.remove(attribute.getCategory()) != null;
321
}
322
323
/**
324
* Returns {@code true} if this attribute set contains an attribute for the
325
* specified category.
326
*
327
* @param category whose presence in this attribute set is to be tested
328
* @return {@code true} if this attribute set contains an attribute value
329
* for the specified category
330
*/
331
public boolean containsKey(Class<?> category) {
332
return
333
category != null &&
334
AttributeSetUtilities.
335
verifyAttributeCategory(category, Attribute.class) != null &&
336
attrMap.get(category) != null;
337
}
338
339
/**
340
* Returns {@code true} if this attribute set contains the given attribute.
341
*
342
* @param attribute value whose presence in this attribute set is to be
343
* tested
344
* @return {@code true} if this attribute set contains the given attribute
345
* value
346
*/
347
public boolean containsValue(Attribute attribute) {
348
return
349
attribute != null &&
350
attribute instanceof Attribute &&
351
attribute.equals(attrMap.get(attribute.getCategory()));
352
}
353
354
/**
355
* Adds all of the elements in the specified set to this attribute. The
356
* outcome is the same as if the {@link #add(Attribute) add(Attribute)}
357
* operation had been applied to this attribute set successively with each
358
* element from the specified set. The behavior of the
359
* {@code addAll(AttributeSet)} operation is unspecified if the specified
360
* set is modified while the operation is in progress.
361
* <p>
362
* If the {@code addAll(AttributeSet)} operation throws an exception, the
363
* effect on this attribute set's state is implementation dependent;
364
* elements from the specified set before the point of the exception may or
365
* may not have been added to this attribute set.
366
*
367
* @param attributes whose elements are to be added to this attribute set
368
* @return {@code true} if this attribute set changed as a result of the
369
* call
370
* @throws UnmodifiableSetException if this attribute set does not support
371
* the {@code addAll(AttributeSet)} method
372
* @throws NullPointerException if some element in the specified set is
373
* {@code null}, or the set is {@code null}
374
* @see #add(Attribute)
375
*/
376
public boolean addAll(AttributeSet attributes) {
377
378
Attribute []attrs = attributes.toArray();
379
boolean result = false;
380
for (int i=0; i<attrs.length; i++) {
381
Attribute newValue =
382
AttributeSetUtilities.verifyAttributeValue(attrs[i],
383
myInterface);
384
Object oldValue = attrMap.put(newValue.getCategory(), newValue);
385
result = (! newValue.equals(oldValue)) || result;
386
}
387
return result;
388
}
389
390
/**
391
* Returns the number of attributes in this attribute set. If this attribute
392
* set contains more than {@code Integer.MAX_VALUE} elements, returns
393
* {@code Integer.MAX_VALUE}.
394
*
395
* @return the number of attributes in this attribute set
396
*/
397
public int size() {
398
return attrMap.size();
399
}
400
401
/**
402
* Returns an array of the attributes contained in this set.
403
*
404
* @return the attributes contained in this set as an array, zero length if
405
* the {@code AttributeSet} is empty
406
*/
407
public Attribute[] toArray() {
408
Attribute []attrs = new Attribute[size()];
409
attrMap.values().toArray(attrs);
410
return attrs;
411
}
412
413
/**
414
* Removes all attributes from this attribute set.
415
*
416
* @throws UnmodifiableSetException if this attribute set does not support
417
* the {@code clear()} operation
418
*/
419
public void clear() {
420
attrMap.clear();
421
}
422
423
/**
424
* Returns {@code true} if this attribute set contains no attributes.
425
*
426
* @return {@code true} if this attribute set contains no attributes
427
*/
428
public boolean isEmpty() {
429
return attrMap.isEmpty();
430
}
431
432
/**
433
* Compares the specified object with this attribute set for equality.
434
* Returns {@code true} if the given object is also an attribute set and the
435
* two attribute sets contain the same attribute category-attribute value
436
* mappings. This ensures that the {@code equals()} method works properly
437
* across different implementations of the {@code AttributeSet} interface.
438
*
439
* @param object to be compared for equality with this attribute set
440
* @return {@code true} if the specified object is equal to this attribute
441
* set
442
*/
443
public boolean equals(Object object) {
444
if (object == null || !(object instanceof AttributeSet)) {
445
return false;
446
}
447
448
AttributeSet aset = (AttributeSet)object;
449
if (aset.size() != size()) {
450
return false;
451
}
452
453
Attribute[] attrs = toArray();
454
for (int i=0;i<attrs.length; i++) {
455
if (!aset.containsValue(attrs[i])) {
456
return false;
457
}
458
}
459
return true;
460
}
461
462
/**
463
* Returns the hash code value for this attribute set. The hash code of an
464
* attribute set is defined to be the sum of the hash codes of each entry in
465
* the {@code AttributeSet}. This ensures that {@code t1.equals(t2)} implies
466
* that {@code t1.hashCode()==t2.hashCode()} for any two attribute sets
467
* {@code t1} and {@code t2}, as required by the general contract of
468
* {@link Object#hashCode() Object.hashCode()}.
469
*
470
* @return the hash code value for this attribute set
471
*/
472
public int hashCode() {
473
int hcode = 0;
474
Attribute[] attrs = toArray();
475
for (int i=0;i<attrs.length; i++) {
476
hcode += attrs[i].hashCode();
477
}
478
return hcode;
479
}
480
}
481
482