1 /*
2 * Copyright (c) 2000, 2014, 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 package javax.print.attribute.standard;
26
27 import java.util.AbstractSet;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.NoSuchElementException;
31 import java.util.HashMap;
32 import java.util.Set;
33
34 import javax.print.attribute.Attribute;
35 import javax.print.attribute.PrintServiceAttribute;
36
37 /**
38 * Class PrinterStateReasons is a printing attribute class, a set of
39 * enumeration values, that provides additional information about the
40 * printer's current state, i.e., information that augments the value of the
41 * printer's {@link PrinterState PrinterState} attribute.
42 * <P>
43 * Instances of {@link PrinterStateReason PrinterStateReason} do not appear in
44 * a Print Service's attribute set directly. Rather, a PrinterStateReasons
45 * attribute appears in the Print Service's attribute set. The
46 * PrinterStateReasons attribute contains zero, one, or more than one {@link
47 * PrinterStateReason PrinterStateReason} objects which pertain to the Print
48 * Service's status, and each {@link PrinterStateReason PrinterStateReason}
49 * object is associated with a {@link Severity Severity} level of REPORT
50 * (least severe), WARNING, or ERROR (most severe). The printer adds a {@link
51 * PrinterStateReason PrinterStateReason} object to the Print Service's
52 * PrinterStateReasons attribute when the corresponding condition becomes true
53 * of the printer, and the printer removes the {@link PrinterStateReason
54 * PrinterStateReason} object again when the corresponding condition becomes
55 * false, regardless of whether the Print Service's overall
56 * {@link PrinterState PrinterState} also changed.
57 * <P>
58 * Class PrinterStateReasons inherits its implementation from class {@link
59 * java.util.HashMap java.util.HashMap}. Each entry in the map consists of a
60 * {@link PrinterStateReason PrinterStateReason} object (key) mapping to a
61 * {@link Severity Severity} object (value):
62 * <P>
63 * Unlike most printing attributes which are immutable once constructed, class
64 * PrinterStateReasons is designed to be mutable; you can add {@link
65 * PrinterStateReason PrinterStateReason} objects to an existing
66 * PrinterStateReasons object and remove them again. However, like class
67 * {@link java.util.HashMap java.util.HashMap}, class PrinterStateReasons is
68 * not multiple thread safe. If a PrinterStateReasons object will be used by
69 * multiple threads, be sure to synchronize its operations (e.g., using a
70 * synchronized map view obtained from class {@link java.util.Collections
71 * java.util.Collections}).
72 * <P>
73 * <B>IPP Compatibility:</B> The string values returned by each individual
74 * {@link PrinterStateReason PrinterStateReason} object's and the associated
75 * {@link Severity Severity} object's {@code toString()} methods,
76 * concatenated
77 * together with a hyphen ({@code "-"}) in between, gives the IPP keyword
78 * value. The category name returned by {@code getName()} gives the IPP
79 * attribute name.
80 *
81 * @author Alan Kaminsky
82 */
83 public final class PrinterStateReasons
84 extends HashMap<PrinterStateReason,Severity>
85 implements PrintServiceAttribute
86 {
87
88 private static final long serialVersionUID = -3731791085163619457L;
89
90 /**
91 * Construct a new, empty printer state reasons attribute; the underlying
92 * hash map has the default initial capacity and load factor.
93 */
94 public PrinterStateReasons() {
95 super();
96 }
97
98 /**
99 * super a new, empty printer state reasons attribute; the underlying
100 * hash map has the given initial capacity and the default load factor.
101 *
102 * @param initialCapacity Initial capacity.
103 *
104 * @throws IllegalArgumentException if the initial capacity is less
105 * than zero.
106 */
107 public PrinterStateReasons(int initialCapacity) {
108 super (initialCapacity);
109 }
110
111 /**
112 * Construct a new, empty printer state reasons attribute; the underlying
113 * hash map has the given initial capacity and load factor.
114 *
115 * @param initialCapacity Initial capacity.
116 * @param loadFactor Load factor.
117 *
118 * @throws IllegalArgumentException if the initial capacity is less
119 * than zero.
120 */
121 public PrinterStateReasons(int initialCapacity, float loadFactor) {
122 super (initialCapacity, loadFactor);
123 }
124
125 /**
126 * Construct a new printer state reasons attribute that contains the same
127 * {@link PrinterStateReason PrinterStateReason}-to-{@link Severity
128 * Severity} mappings as the given map. The underlying hash map's initial
129 * capacity and load factor are as specified in the superclass constructor
130 * {@link java.util.HashMap#HashMap(java.util.Map)
131 * HashMap(Map)}.
132 *
133 * @param map Map to copy.
134 *
135 * @exception NullPointerException
136 * (unchecked exception) Thrown if {@code map} is null or if any
137 * key or value in {@code map} is null.
138 * @throws ClassCastException
139 * (unchecked exception) Thrown if any key in {@code map} is not
140 * an instance of class {@link PrinterStateReason PrinterStateReason} or
141 * if any value in {@code map} is not an instance of class
142 * {@link Severity Severity}.
143 */
144 public PrinterStateReasons(Map<PrinterStateReason,Severity> map) {
145 this();
146 for (Map.Entry<PrinterStateReason,Severity> e : map.entrySet())
147 put(e.getKey(), e.getValue());
148 }
149
150 /**
151 * Adds the given printer state reason to this printer state reasons
152 * attribute, associating it with the given severity level. If this
153 * printer state reasons attribute previously contained a mapping for the
154 * given printer state reason, the old value is replaced.
155 *
156 * @param reason Printer state reason. This must be an instance of
157 * class {@link PrinterStateReason PrinterStateReason}.
158 * @param severity Severity of the printer state reason. This must be
159 * an instance of class {@link Severity Severity}.
160 *
161 * @return Previous severity associated with the given printer state
162 * reason, or {@code null} if the given printer state reason was
163 * not present.
164 *
165 * @throws NullPointerException
166 * (unchecked exception) Thrown if {@code reason} is null or
167 * {@code severity} is null.
168 * @throws ClassCastException
169 * (unchecked exception) Thrown if {@code reason} is not an
170 * instance of class {@link PrinterStateReason PrinterStateReason} or if
171 * {@code severity} is not an instance of class {@link Severity
172 * Severity}.
173 * @since 1.5
174 */
175 public Severity put(PrinterStateReason reason, Severity severity) {
176 if (reason == null) {
177 throw new NullPointerException("reason is null");
178 }
179 if (severity == null) {
180 throw new NullPointerException("severity is null");
181 }
182 return super.put(reason, severity);
183 }
184
185 /**
186 * Get the printing attribute class which is to be used as the "category"
187 * for this printing attribute value.
188 * <P>
189 * For class PrinterStateReasons, the
190 * category is class PrinterStateReasons itself.
191 *
192 * @return Printing attribute class (category), an instance of class
193 * {@link java.lang.Class java.lang.Class}.
194 */
195 public final Class<? extends Attribute> getCategory() {
196 return PrinterStateReasons.class;
197 }
198
199 /**
200 * Get the name of the category of which this attribute value is an
201 * instance.
202 * <P>
203 * For class PrinterStateReasons, the
204 * category name is {@code "printer-state-reasons"}.
205 *
206 * @return Attribute category name.
207 */
208 public final String getName() {
209 return "printer-state-reasons";
210 }
211
212 /**
213 * Obtain an unmodifiable set view of the individual printer state reason
214 * attributes at the given severity level in this PrinterStateReasons
215 * attribute. Each element in the set view is a {@link PrinterStateReason
216 * PrinterStateReason} object. The only elements in the set view are the
217 * {@link PrinterStateReason PrinterStateReason} objects that map to the
218 * given severity value. The set view is backed by this
219 * PrinterStateReasons attribute, so changes to this PrinterStateReasons
220 * attribute are reflected in the set view.
221 * The set view does not support element insertion or
222 * removal. The set view's iterator does not support element removal.
223 *
224 * @param severity Severity level.
225 *
226 * @return Set view of the individual {@link PrinterStateReason
227 * PrinterStateReason} attributes at the given {@link Severity
228 * Severity} level.
229 *
230 * @exception NullPointerException
231 * (unchecked exception) Thrown if {@code severity} is null.
232 */
233 public Set<PrinterStateReason> printerStateReasonSet(Severity severity) {
234 if (severity == null) {
235 throw new NullPointerException("severity is null");
236 }
237 return new PrinterStateReasonSet (severity, entrySet());
238 }
239
240 private class PrinterStateReasonSet
241 extends AbstractSet<PrinterStateReason>
242 {
243 private Severity mySeverity;
244
245 private Set<Map.Entry<PrinterStateReason, Severity>> myEntrySet;
246
247 public PrinterStateReasonSet(Severity severity,
248 Set<Map.Entry<PrinterStateReason, Severity>> entrySet) {
249 mySeverity = severity;
250 myEntrySet = entrySet;
251 }
288 }
289 }
290
291 public boolean hasNext() {
292 return myEntry != null;
293 }
294
295 public PrinterStateReason next() {
296 if (myEntry == null) {
297 throw new NoSuchElementException();
298 }
299 PrinterStateReason result = myEntry.getKey();
300 goToNext();
301 return result;
302 }
303
304 public void remove() {
305 throw new UnsupportedOperationException();
306 }
307 }
308
309 }
|
1 /*
2 * Copyright (c) 2000, 2017, 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.standard;
27
28 import java.util.AbstractSet;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 import java.util.NoSuchElementException;
33 import java.util.Set;
34
35 import javax.print.attribute.Attribute;
36 import javax.print.attribute.PrintServiceAttribute;
37
38 /**
39 * Class {@code PrinterStateReasons} is a printing attribute class, a set of
40 * enumeration values, that provides additional information about the printer's
41 * current state, i.e., information that augments the value of the printer's
42 * {@link PrinterState PrinterState} attribute.
43 * <p>
44 * Instances of {@link PrinterStateReason PrinterStateReason} do not appear in a
45 * Print Service's attribute set directly. Rather, a {@code PrinterStateReasons}
46 * attribute appears in the Print Service's attribute set. The
47 * {@code PrinterStateReasons} attribute contains zero, one, or more than one
48 * {@link PrinterStateReason PrinterStateReason} objects which pertain to the
49 * Print Service's status, and each
50 * {@link PrinterStateReason PrinterStateReason} object is associated with a
51 * {@link Severity Severity} level of {@code REPORT} (least severe),
52 * {@code WARNING}, or {@code ERROR} (most severe). The printer adds a
53 * {@link PrinterStateReason PrinterStateReason} object to the Print Service's
54 * {@code PrinterStateReasons} attribute when the corresponding condition
55 * becomes true of the printer, and the printer removes the
56 * {@link PrinterStateReason PrinterStateReason} object again when the
57 * corresponding condition becomes false, regardless of whether the Print
58 * Service's overall {@link PrinterState PrinterState} also changed.
59 * <p>
60 * Class PrinterStateReasons inherits its implementation from class
61 * {@link HashMap java.util.HashMap}. Each entry in the map consists of a
62 * {@link PrinterStateReason PrinterStateReason} object (key) mapping to a
63 * {@link Severity Severity} object (value):
64 * <p>
65 * Unlike most printing attributes which are immutable once constructed, class
66 * {@code PrinterStateReasons} is designed to be mutable; you can add
67 * {@link PrinterStateReason PrinterStateReason} objects to an existing
68 * {@code PrinterStateReasons} object and remove them again. However, like class
69 * {@link HashMap java.util.HashMap}, class {@code PrinterStateReasons} is not
70 * multiple thread safe. If a {@code PrinterStateReasons} object will be used by
71 * multiple threads, be sure to synchronize its operations (e.g., using a
72 * synchronized map view obtained from class {@link java.util.Collections
73 * java.util.Collections}).
74 * <p>
75 * <b>IPP Compatibility:</b> The string values returned by each individual
76 * {@link PrinterStateReason PrinterStateReason} object's and the associated
77 * {@link Severity Severity} object's {@code toString()} methods, concatenated
78 * together with a hyphen ({@code "-"}) in between, gives the IPP keyword value.
79 * The category name returned by {@code getName()} gives the IPP attribute name.
80 *
81 * @author Alan Kaminsky
82 */
83 public final class PrinterStateReasons
84 extends HashMap<PrinterStateReason,Severity>
85 implements PrintServiceAttribute
86 {
87
88 /**
89 * Use serialVersionUID from JDK 1.4 for interoperability.
90 */
91 private static final long serialVersionUID = -3731791085163619457L;
92
93 /**
94 * Construct a new, empty printer state reasons attribute; the underlying
95 * hash map has the default initial capacity and load factor.
96 */
97 public PrinterStateReasons() {
98 super();
99 }
100
101 /**
102 * Construct a new, empty printer state reasons attribute; the underlying
103 * hash map has the given initial capacity and the default load factor.
104 *
105 * @param initialCapacity initial capacity
106 * @throws IllegalArgumentException if the initial capacity is negative
107 */
108 public PrinterStateReasons(int initialCapacity) {
109 super (initialCapacity);
110 }
111
112 /**
113 * Construct a new, empty printer state reasons attribute; the underlying
114 * hash map has the given initial capacity and load factor.
115 *
116 * @param initialCapacity initial capacity
117 * @param loadFactor load factor
118 * @throws IllegalArgumentException if the initial capacity is negative
119 */
120 public PrinterStateReasons(int initialCapacity, float loadFactor) {
121 super (initialCapacity, loadFactor);
122 }
123
124 /**
125 * Construct a new printer state reasons attribute that contains the same
126 * {@link PrinterStateReason PrinterStateReason}-to-{@link Severity
127 * Severity} mappings as the given map. The underlying hash map's initial
128 * capacity and load factor are as specified in the superclass constructor
129 * {@link HashMap#HashMap(Map) HashMap(Map)}.
130 *
131 * @param map map to copy
132 * @throws NullPointerException if {@code map} is {@code null} or if any key
133 * or value in {@code map} is {@code null}
134 * @throws ClassCastException if any key in {@code map} is not an instance
135 * of class {@link PrinterStateReason PrinterStateReason} or if any
136 * value in {@code map} is not an instance of class
137 * {@link Severity Severity}
138 */
139 public PrinterStateReasons(Map<PrinterStateReason,Severity> map) {
140 this();
141 for (Map.Entry<PrinterStateReason,Severity> e : map.entrySet())
142 put(e.getKey(), e.getValue());
143 }
144
145 /**
146 * Adds the given printer state reason to this printer state reasons
147 * attribute, associating it with the given severity level. If this printer
148 * state reasons attribute previously contained a mapping for the given
149 * printer state reason, the old value is replaced.
150 *
151 * @param reason printer state reason. This must be an instance of class
152 * {@link PrinterStateReason PrinterStateReason}
153 * @param severity severity of the printer state reason. This must be an
154 * instance of class {@link Severity Severity}
155 * @return previous severity associated with the given printer state reason,
156 * or {@code null} if the given printer state reason was not
157 * present
158 * @throws NullPointerException if {@code reason} is {@code null} or
159 * {@code severity} is {@code null}
160 * @throws ClassCastException if {@code reason} is not an instance of class
161 * {@link PrinterStateReason PrinterStateReason} or if
162 * {@code severity} is not an instance of class
163 * {@link Severity Severity}
164 * @since 1.5
165 */
166 public Severity put(PrinterStateReason reason, Severity severity) {
167 if (reason == null) {
168 throw new NullPointerException("reason is null");
169 }
170 if (severity == null) {
171 throw new NullPointerException("severity is null");
172 }
173 return super.put(reason, severity);
174 }
175
176 /**
177 * Get the printing attribute class which is to be used as the "category"
178 * for this printing attribute value.
179 * <p>
180 * For class {@code PrinterStateReasons}, the category is class
181 * {@code PrinterStateReasons} itself.
182 *
183 * @return printing attribute class (category), an instance of class
184 * {@link Class java.lang.Class}
185 */
186 public final Class<? extends Attribute> getCategory() {
187 return PrinterStateReasons.class;
188 }
189
190 /**
191 * Get the name of the category of which this attribute value is an
192 * instance.
193 * <p>
194 * For class {@code PrinterStateReasons}, the category name is
195 * {@code "printer-state-reasons"}.
196 *
197 * @return attribute category name
198 */
199 public final String getName() {
200 return "printer-state-reasons";
201 }
202
203 /**
204 * Obtain an unmodifiable set view of the individual printer state reason
205 * attributes at the given severity level in this
206 * {@code PrinterStateReasons} attribute. Each element in the set view is a
207 * {@link PrinterStateReason PrinterStateReason} object. The only elements
208 * in the set view are the {@link PrinterStateReason PrinterStateReason}
209 * objects that map to the given severity value. The set view is backed by
210 * this {@code PrinterStateReasons} attribute, so changes to this
211 * {@code PrinterStateReasons} attribute are reflected in the set view. The
212 * set view does not support element insertion or removal. The set view's
213 * iterator does not support element removal.
214 *
215 * @param severity severity level
216 * @return set view of the individual
217 * {@link PrinterStateReason PrinterStateReason} attributes at the
218 * given {@link Severity Severity} level
219 * @throws NullPointerException if {@code severity} is {@code null}
220 */
221 public Set<PrinterStateReason> printerStateReasonSet(Severity severity) {
222 if (severity == null) {
223 throw new NullPointerException("severity is null");
224 }
225 return new PrinterStateReasonSet (severity, entrySet());
226 }
227
228 private class PrinterStateReasonSet
229 extends AbstractSet<PrinterStateReason>
230 {
231 private Severity mySeverity;
232
233 private Set<Map.Entry<PrinterStateReason, Severity>> myEntrySet;
234
235 public PrinterStateReasonSet(Severity severity,
236 Set<Map.Entry<PrinterStateReason, Severity>> entrySet) {
237 mySeverity = severity;
238 myEntrySet = entrySet;
239 }
276 }
277 }
278
279 public boolean hasNext() {
280 return myEntry != null;
281 }
282
283 public PrinterStateReason next() {
284 if (myEntry == null) {
285 throw new NoSuchElementException();
286 }
287 PrinterStateReason result = myEntry.getKey();
288 goToNext();
289 return result;
290 }
291
292 public void remove() {
293 throw new UnsupportedOperationException();
294 }
295 }
296 }
|