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         }
 252 
 253         public int size() {
 254             int result = 0;
 255             Iterator<PrinterStateReason> iter = iterator();
 256             while (iter.hasNext()) {
 257                 iter.next();
 258                 ++ result;
 259             }
 260             return result;
 261         }
 262 
 263         public Iterator<PrinterStateReason> iterator() {
 264             return new PrinterStateReasonSetIterator(mySeverity,
 265                                                      myEntrySet.iterator());
 266         }
 267     }
 268 
 269     private class PrinterStateReasonSetIterator implements Iterator<PrinterStateReason> {
 270         private Severity mySeverity;
 271         private Iterator<Map.Entry<PrinterStateReason, Severity>> myIterator;
 272         private Map.Entry<PrinterStateReason, Severity> myEntry;
 273 
 274         public PrinterStateReasonSetIterator(Severity severity,
 275                                              Iterator<Map.Entry<PrinterStateReason, Severity>> iterator) {
 276             mySeverity = severity;
 277             myIterator = iterator;
 278             goToNext();
 279         }
 280 
 281         private void goToNext() {
 282             myEntry = null;
 283             while (myEntry == null && myIterator.hasNext()) {
 284                 myEntry = myIterator.next();
 285                 if (myEntry.getValue() != mySeverity) {
 286                     myEntry = null;
 287                 }
 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 }