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