KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > print > attribute > standard > PrinterStateReasons


1 /*
2  * @(#)PrinterStateReasons.java 1.9 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 package javax.print.attribute.standard;
8
9 import java.util.AbstractSet JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.NoSuchElementException JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import javax.print.attribute.Attribute JavaDoc;
17 import javax.print.attribute.PrintServiceAttribute JavaDoc;
18
19 /**
20  * Class PrinterStateReasons is a printing attribute class, a set of
21  * enumeration values, that provides additional information about the
22  * printer's current state, i.e., information that augments the value of the
23  * printer's {@link PrinterState PrinterState} attribute.
24  * <P>
25  * Instances of {@link PrinterStateReason PrinterStateReason} do not appear in
26  * a Print Service's attribute set directly. Rather, a PrinterStateReasons
27  * attribute appears in the Print Service's attribute set. The
28  * PrinterStateReasons attribute contains zero, one, or more than one {@link
29  * PrinterStateReason PrinterStateReason} objects which pertain to the Print
30  * Service's status, and each {@link PrinterStateReason PrinterStateReason}
31  * object is associated with a {@link Severity Severity} level of REPORT
32  * (least severe), WARNING, or ERROR (most severe). The printer adds a {@link
33  * PrinterStateReason PrinterStateReason} object to the Print Service's
34  * PrinterStateReasons attribute when the corresponding condition becomes true
35  * of the printer, and the printer removes the {@link PrinterStateReason
36  * PrinterStateReason} object again when the corresponding condition becomes
37  * false, regardless of whether the Print Service's overall
38  * {@link PrinterState PrinterState} also changed.
39  * <P>
40  * Class PrinterStateReasons inherits its implementation from class {@link
41  * java.util.HashMap java.util.HashMap}. Each entry in the map consists of a
42  * {@link PrinterStateReason PrinterStateReason} object (key) mapping to a
43  * {@link Severity Severity} object (value):
44  * <P>
45  * Unlike most printing attributes which are immutable once constructed, class
46  * PrinterStateReasons is designed to be mutable; you can add {@link
47  * PrinterStateReason PrinterStateReason} objects to an existing
48  * PrinterStateReasons object and remove them again. However, like class
49  * {@link java.util.HashMap java.util.HashMap}, class PrinterStateReasons is
50  * bot multiple thread safe. If a PrinterStateReasons object will be used by
51  * multiple threads, be sure to synchronize its operations (e.g., using a
52  * synchronized map view obtained from class {@link java.util.Collections
53  * java.util.Collections}).
54  * <P>
55  * <B>IPP Compatibility:</B> The string values returned by each individual
56  * {@link PrinterStateReason PrinterStateReason} object's and the associated
57  * {@link Severity Severity} object's <CODE>toString()</CODE> methods,
58  * concatenated
59  * together with a hyphen (<CODE>"-"</CODE>) in between, gives the IPP keyword
60  * value. The category name returned by <CODE>getName()</CODE> gives the IPP
61  * attribute name.
62  * <P>
63  *
64  * @author Alan Kaminsky
65  */

66 public final class PrinterStateReasons
67     extends HashMap JavaDoc<PrinterStateReason JavaDoc,Severity JavaDoc>
68     implements PrintServiceAttribute JavaDoc
69 {
70
71     private static final long serialVersionUID = -3731791085163619457L;
72
73     /**
74      * Construct a new, empty printer state reasons attribute; the underlying
75      * hash map has the default initial capacity and load factor.
76      */

77     public PrinterStateReasons() {
78     super();
79     }
80
81     /**
82      * super a new, empty printer state reasons attribute; the underlying
83      * hash map has the given initial capacity and the default load factor.
84      *
85      * @param initialCapacity Initial capacity.
86      *
87      * @throws IllegalArgumentException if the initial capacity is less
88      * than zero.
89      */

90     public PrinterStateReasons(int initialCapacity) {
91     super (initialCapacity);
92     }
93
94     /**
95      * Construct a new, empty printer state reasons attribute; the underlying
96      * hash map has the given initial capacity and load factor.
97      *
98      * @param initialCapacity Initial capacity.
99      * @param loadFactor Load factor.
100      *
101      * @throws IllegalArgumentException if the initial capacity is less
102      * than zero.
103      */

104     public PrinterStateReasons(int initialCapacity, float loadFactor) {
105     super (initialCapacity, loadFactor);
106     }
107
108     /**
109      * Construct a new printer state reasons attribute that contains the same
110      * {@link PrinterStateReason PrinterStateReason}-to-{@link Severity
111      * Severity} mappings as the given map. The underlying hash map's initial
112      * capacity and load factor are as specified in the superclass constructor
113      * {@link java.util.HashMap#HashMap(java.util.Map)
114      * <CODE>HashMap(Map)</CODE>}.
115      *
116      * @param map Map to copy.
117      *
118      * @exception NullPointerException
119      * (unchecked exception) Thrown if <CODE>map</CODE> is null or if any
120      * key or value in <CODE>map</CODE> is null.
121      * @throws ClassCastException
122      * (unchecked exception) Thrown if any key in <CODE>map</CODE> is not
123      * an instance of class {@link PrinterStateReason PrinterStateReason} or
124      * if any value in <CODE>map</CODE> is not an instance of class
125      * {@link Severity Severity}.
126      */

127     public PrinterStateReasons(Map JavaDoc<PrinterStateReason JavaDoc,Severity JavaDoc> map) {
128     this();
129     for (Map.Entry JavaDoc<PrinterStateReason JavaDoc,Severity JavaDoc> e : map.entrySet())
130         put(e.getKey(), e.getValue());
131     }
132
133     /**
134      * Adds the given printer state reason to this printer state reasons
135      * attribute, associating it with the given severity level. If this
136      * printer state reasons attribute previously contained a mapping for the
137      * given printer state reason, the old value is replaced.
138      *
139      * @param reason Printer state reason. This must be an instance of
140      * class {@link PrinterStateReason PrinterStateReason}.
141      * @param severity Severity of the printer state reason. This must be
142      * an instance of class {@link Severity Severity}.
143      *
144      * @return Previous severity associated with the given printer state
145      * reason, or <tt>null</tt> if the given printer state reason was
146      * not present.
147      *
148      * @throws NullPointerException
149      * (unchecked exception) Thrown if <CODE>reason</CODE> is null or
150      * <CODE>severity</CODE> is null.
151      * @throws ClassCastException
152      * (unchecked exception) Thrown if <CODE>reason</CODE> is not an
153      * instance of class {@link PrinterStateReason PrinterStateReason} or if
154      * <CODE>severity</CODE> is not an instance of class {@link Severity
155      * Severity}.
156      */

157     public Severity JavaDoc put(PrinterStateReason JavaDoc reason, Severity JavaDoc severity) {
158     if (reason == null) {
159         throw new NullPointerException JavaDoc("reason is null");
160     }
161     if (severity == null) {
162         throw new NullPointerException JavaDoc("severity is null");
163     }
164     return super.put((PrinterStateReason JavaDoc) reason,
165              (Severity JavaDoc) severity);
166     }
167
168     /**
169      * Get the printing attribute class which is to be used as the "category"
170      * for this printing attribute value.
171      * <P>
172      * For class PrinterStateReasons, the
173      * category is class PrinterStateReasons itself.
174      *
175      * @return Printing attribute class (category), an instance of class
176      * {@link java.lang.Class java.lang.Class}.
177      */

178     public final Class JavaDoc<? extends Attribute JavaDoc> getCategory() {
179     return PrinterStateReasons JavaDoc.class;
180     }
181
182     /**
183      * Get the name of the category of which this attribute value is an
184      * instance.
185      * <P>
186      * For class PrinterStateReasons, the
187      * category name is <CODE>"printer-state-reasons"</CODE>.
188      *
189      * @return Attribute category name.
190      */

191     public final String JavaDoc getName() {
192     return "printer-state-reasons";
193     }
194
195     /**
196      * Obtain an unmodifiable set view of the individual printer state reason
197      * attributes at the given severity level in this PrinterStateReasons
198      * attribute. Each element in the set view is a {@link PrinterStateReason
199      * PrinterStateReason} object. The only elements in the set view are the
200      * {@link PrinterStateReason PrinterStateReason} objects that map to the
201      * given severity value. The set view is backed by this
202      * PrinterStateReasons attribute, so changes to this PrinterStateReasons
203      * attribute are reflected in the set view.
204      * The set view does not support element insertion or
205      * removal. The set view's iterator does not support element removal.
206      *
207      * @param severity Severity level.
208      *
209      * @return Set view of the individual {@link PrinterStateReason
210      * PrinterStateReason} attributes at the given {@link Severity
211      * Severity} level.
212      *
213      * @exception NullPointerException
214      * (unchecked exception) Thrown if <CODE>severity</CODE> is null.
215      */

216     public Set JavaDoc<PrinterStateReason JavaDoc> printerStateReasonSet(Severity JavaDoc severity) {
217     if (severity == null) {
218         throw new NullPointerException JavaDoc("severity is null");
219     }
220     return new PrinterStateReasonSet (severity, entrySet());
221     }
222
223     private class PrinterStateReasonSet
224     extends AbstractSet JavaDoc<PrinterStateReason JavaDoc>
225     {
226     private Severity JavaDoc mySeverity;
227     private Set JavaDoc myEntrySet;
228
229     public PrinterStateReasonSet(Severity JavaDoc severity, Set JavaDoc entrySet) {
230         mySeverity = severity;
231         myEntrySet = entrySet;
232     }
233
234     public int size() {
235         int result = 0;
236         Iterator JavaDoc iter = iterator();
237         while (iter.hasNext()) {
238         iter.next();
239         ++ result;
240         }
241         return result;
242     }
243
244     public Iterator JavaDoc iterator() {
245         return new PrinterStateReasonSetIterator(mySeverity,
246                              myEntrySet.iterator());
247     }
248     }
249
250     private class PrinterStateReasonSetIterator implements Iterator JavaDoc {
251     private Severity JavaDoc mySeverity;
252     private Iterator JavaDoc myIterator;
253     private Map.Entry JavaDoc myEntry;
254
255     public PrinterStateReasonSetIterator(Severity JavaDoc severity,
256                          Iterator JavaDoc iterator) {
257         mySeverity = severity;
258         myIterator = iterator;
259         goToNext();
260     }
261
262     private void goToNext() {
263         myEntry = null;
264         while (myEntry == null && myIterator.hasNext()) {
265         myEntry = (Map.Entry JavaDoc) myIterator.next();
266         if ((Severity JavaDoc) myEntry.getValue() != mySeverity) {
267             myEntry = null;
268         }
269         }
270     }
271
272     public boolean hasNext() {
273         return myEntry != null;
274     }
275
276     public Object JavaDoc next() {
277         if (myEntry == null) {
278         throw new NoSuchElementException JavaDoc();
279         }
280         Object JavaDoc result = myEntry.getKey();
281         goToNext();
282         return result;
283     }
284
285     public void remove() {
286         throw new UnsupportedOperationException JavaDoc();
287     }
288     }
289     
290 }
291
Popular Tags