0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.print.attribute.standard;
0N/A
0N/Aimport java.util.AbstractSet;
0N/Aimport java.util.Iterator;
0N/Aimport java.util.Map;
0N/Aimport java.util.NoSuchElementException;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Set;
0N/A
0N/Aimport javax.print.attribute.Attribute;
0N/Aimport javax.print.attribute.PrintServiceAttribute;
0N/A
0N/A/**
0N/A * Class PrinterStateReasons is a printing attribute class, a set of
0N/A * enumeration values, that provides additional information about the
0N/A * printer's current state, i.e., information that augments the value of the
0N/A * printer's {@link PrinterState PrinterState} attribute.
0N/A * <P>
0N/A * Instances of {@link PrinterStateReason PrinterStateReason} do not appear in
0N/A * a Print Service's attribute set directly. Rather, a PrinterStateReasons
0N/A * attribute appears in the Print Service's attribute set. The
0N/A * PrinterStateReasons attribute contains zero, one, or more than one {@link
0N/A * PrinterStateReason PrinterStateReason} objects which pertain to the Print
0N/A * Service's status, and each {@link PrinterStateReason PrinterStateReason}
0N/A * object is associated with a {@link Severity Severity} level of REPORT
0N/A * (least severe), WARNING, or ERROR (most severe). The printer adds a {@link
0N/A * PrinterStateReason PrinterStateReason} object to the Print Service's
0N/A * PrinterStateReasons attribute when the corresponding condition becomes true
0N/A * of the printer, and the printer removes the {@link PrinterStateReason
0N/A * PrinterStateReason} object again when the corresponding condition becomes
0N/A * false, regardless of whether the Print Service's overall
0N/A * {@link PrinterState PrinterState} also changed.
0N/A * <P>
0N/A * Class PrinterStateReasons inherits its implementation from class {@link
0N/A * java.util.HashMap java.util.HashMap}. Each entry in the map consists of a
0N/A * {@link PrinterStateReason PrinterStateReason} object (key) mapping to a
0N/A * {@link Severity Severity} object (value):
0N/A * <P>
0N/A * Unlike most printing attributes which are immutable once constructed, class
0N/A * PrinterStateReasons is designed to be mutable; you can add {@link
0N/A * PrinterStateReason PrinterStateReason} objects to an existing
0N/A * PrinterStateReasons object and remove them again. However, like class
0N/A * {@link java.util.HashMap java.util.HashMap}, class PrinterStateReasons is
1879N/A * not multiple thread safe. If a PrinterStateReasons object will be used by
0N/A * multiple threads, be sure to synchronize its operations (e.g., using a
0N/A * synchronized map view obtained from class {@link java.util.Collections
0N/A * java.util.Collections}).
0N/A * <P>
0N/A * <B>IPP Compatibility:</B> The string values returned by each individual
0N/A * {@link PrinterStateReason PrinterStateReason} object's and the associated
0N/A * {@link Severity Severity} object's <CODE>toString()</CODE> methods,
0N/A * concatenated
0N/A * together with a hyphen (<CODE>"-"</CODE>) in between, gives the IPP keyword
0N/A * value. The category name returned by <CODE>getName()</CODE> gives the IPP
0N/A * attribute name.
0N/A * <P>
0N/A *
0N/A * @author Alan Kaminsky
0N/A */
0N/Apublic final class PrinterStateReasons
0N/A extends HashMap<PrinterStateReason,Severity>
0N/A implements PrintServiceAttribute
0N/A{
0N/A
0N/A private static final long serialVersionUID = -3731791085163619457L;
0N/A
0N/A /**
0N/A * Construct a new, empty printer state reasons attribute; the underlying
0N/A * hash map has the default initial capacity and load factor.
0N/A */
0N/A public PrinterStateReasons() {
0N/A super();
0N/A }
0N/A
0N/A /**
0N/A * super a new, empty printer state reasons attribute; the underlying
0N/A * hash map has the given initial capacity and the default load factor.
0N/A *
0N/A * @param initialCapacity Initial capacity.
0N/A *
0N/A * @throws IllegalArgumentException if the initial capacity is less
0N/A * than zero.
0N/A */
0N/A public PrinterStateReasons(int initialCapacity) {
0N/A super (initialCapacity);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new, empty printer state reasons attribute; the underlying
0N/A * hash map has the given initial capacity and load factor.
0N/A *
0N/A * @param initialCapacity Initial capacity.
0N/A * @param loadFactor Load factor.
0N/A *
0N/A * @throws IllegalArgumentException if the initial capacity is less
0N/A * than zero.
0N/A */
0N/A public PrinterStateReasons(int initialCapacity, float loadFactor) {
0N/A super (initialCapacity, loadFactor);
0N/A }
0N/A
0N/A /**
0N/A * Construct a new printer state reasons attribute that contains the same
0N/A * {@link PrinterStateReason PrinterStateReason}-to-{@link Severity
0N/A * Severity} mappings as the given map. The underlying hash map's initial
0N/A * capacity and load factor are as specified in the superclass constructor
0N/A * {@link java.util.HashMap#HashMap(java.util.Map)
0N/A * <CODE>HashMap(Map)</CODE>}.
0N/A *
0N/A * @param map Map to copy.
0N/A *
0N/A * @exception NullPointerException
0N/A * (unchecked exception) Thrown if <CODE>map</CODE> is null or if any
0N/A * key or value in <CODE>map</CODE> is null.
0N/A * @throws ClassCastException
0N/A * (unchecked exception) Thrown if any key in <CODE>map</CODE> is not
0N/A * an instance of class {@link PrinterStateReason PrinterStateReason} or
0N/A * if any value in <CODE>map</CODE> is not an instance of class
0N/A * {@link Severity Severity}.
0N/A */
0N/A public PrinterStateReasons(Map<PrinterStateReason,Severity> map) {
0N/A this();
0N/A for (Map.Entry<PrinterStateReason,Severity> e : map.entrySet())
0N/A put(e.getKey(), e.getValue());
0N/A }
0N/A
0N/A /**
0N/A * Adds the given printer state reason to this printer state reasons
0N/A * attribute, associating it with the given severity level. If this
0N/A * printer state reasons attribute previously contained a mapping for the
0N/A * given printer state reason, the old value is replaced.
0N/A *
0N/A * @param reason Printer state reason. This must be an instance of
0N/A * class {@link PrinterStateReason PrinterStateReason}.
0N/A * @param severity Severity of the printer state reason. This must be
0N/A * an instance of class {@link Severity Severity}.
0N/A *
0N/A * @return Previous severity associated with the given printer state
0N/A * reason, or <tt>null</tt> if the given printer state reason was
0N/A * not present.
0N/A *
0N/A * @throws NullPointerException
0N/A * (unchecked exception) Thrown if <CODE>reason</CODE> is null or
0N/A * <CODE>severity</CODE> is null.
0N/A * @throws ClassCastException
0N/A * (unchecked exception) Thrown if <CODE>reason</CODE> is not an
0N/A * instance of class {@link PrinterStateReason PrinterStateReason} or if
0N/A * <CODE>severity</CODE> is not an instance of class {@link Severity
0N/A * Severity}.
0N/A * @since 1.5
0N/A */
0N/A public Severity put(PrinterStateReason reason, Severity severity) {
0N/A if (reason == null) {
0N/A throw new NullPointerException("reason is null");
0N/A }
0N/A if (severity == null) {
0N/A throw new NullPointerException("severity is null");
0N/A }
0N/A return super.put((PrinterStateReason) reason,
0N/A (Severity) severity);
0N/A }
0N/A
0N/A /**
0N/A * Get the printing attribute class which is to be used as the "category"
0N/A * for this printing attribute value.
0N/A * <P>
0N/A * For class PrinterStateReasons, the
0N/A * category is class PrinterStateReasons itself.
0N/A *
0N/A * @return Printing attribute class (category), an instance of class
0N/A * {@link java.lang.Class java.lang.Class}.
0N/A */
0N/A public final Class<? extends Attribute> getCategory() {
0N/A return PrinterStateReasons.class;
0N/A }
0N/A
0N/A /**
0N/A * Get the name of the category of which this attribute value is an
0N/A * instance.
0N/A * <P>
0N/A * For class PrinterStateReasons, the
0N/A * category name is <CODE>"printer-state-reasons"</CODE>.
0N/A *
0N/A * @return Attribute category name.
0N/A */
0N/A public final String getName() {
0N/A return "printer-state-reasons";
0N/A }
0N/A
0N/A /**
0N/A * Obtain an unmodifiable set view of the individual printer state reason
0N/A * attributes at the given severity level in this PrinterStateReasons
0N/A * attribute. Each element in the set view is a {@link PrinterStateReason
0N/A * PrinterStateReason} object. The only elements in the set view are the
0N/A * {@link PrinterStateReason PrinterStateReason} objects that map to the
0N/A * given severity value. The set view is backed by this
0N/A * PrinterStateReasons attribute, so changes to this PrinterStateReasons
0N/A * attribute are reflected in the set view.
0N/A * The set view does not support element insertion or
0N/A * removal. The set view's iterator does not support element removal.
0N/A *
0N/A * @param severity Severity level.
0N/A *
0N/A * @return Set view of the individual {@link PrinterStateReason
0N/A * PrinterStateReason} attributes at the given {@link Severity
0N/A * Severity} level.
0N/A *
0N/A * @exception NullPointerException
0N/A * (unchecked exception) Thrown if <CODE>severity</CODE> is null.
0N/A */
0N/A public Set<PrinterStateReason> printerStateReasonSet(Severity severity) {
0N/A if (severity == null) {
0N/A throw new NullPointerException("severity is null");
0N/A }
0N/A return new PrinterStateReasonSet (severity, entrySet());
0N/A }
0N/A
0N/A private class PrinterStateReasonSet
0N/A extends AbstractSet<PrinterStateReason>
0N/A {
0N/A private Severity mySeverity;
0N/A private Set myEntrySet;
0N/A
0N/A public PrinterStateReasonSet(Severity severity, Set entrySet) {
0N/A mySeverity = severity;
0N/A myEntrySet = entrySet;
0N/A }
0N/A
0N/A public int size() {
0N/A int result = 0;
0N/A Iterator iter = iterator();
0N/A while (iter.hasNext()) {
0N/A iter.next();
0N/A ++ result;
0N/A }
0N/A return result;
0N/A }
0N/A
0N/A public Iterator iterator() {
0N/A return new PrinterStateReasonSetIterator(mySeverity,
0N/A myEntrySet.iterator());
0N/A }
0N/A }
0N/A
0N/A private class PrinterStateReasonSetIterator implements Iterator {
0N/A private Severity mySeverity;
0N/A private Iterator myIterator;
0N/A private Map.Entry myEntry;
0N/A
0N/A public PrinterStateReasonSetIterator(Severity severity,
0N/A Iterator iterator) {
0N/A mySeverity = severity;
0N/A myIterator = iterator;
0N/A goToNext();
0N/A }
0N/A
0N/A private void goToNext() {
0N/A myEntry = null;
0N/A while (myEntry == null && myIterator.hasNext()) {
0N/A myEntry = (Map.Entry) myIterator.next();
0N/A if ((Severity) myEntry.getValue() != mySeverity) {
0N/A myEntry = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public boolean hasNext() {
0N/A return myEntry != null;
0N/A }
0N/A
0N/A public Object next() {
0N/A if (myEntry == null) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A Object result = myEntry.getKey();
0N/A goToNext();
0N/A return result;
0N/A }
0N/A
0N/A public void remove() {
0N/A throw new UnsupportedOperationException();
0N/A }
0N/A }
0N/A
0N/A}