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