001 /* PrinterStateReasons.java --
002 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.print.attribute.standard;
040
041 import java.util.Collections;
042 import java.util.HashMap;
043 import java.util.HashSet;
044 import java.util.Iterator;
045 import java.util.Map;
046 import java.util.Set;
047
048 import javax.print.attribute.Attribute;
049 import javax.print.attribute.PrintServiceAttribute;
050
051 /**
052 * The <code>PrinterStateReasons</code> attribute provides the set of
053 * additional informations available about the current state of the printer
054 * device.
055 * <p>
056 * The attribute is basically a map with <code>PrinterStateReason</code>
057 * objects as keys associated with their severity level as
058 * <code>Severity</code> instances. The IPP keyword value can be
059 * constructed as follows: <br>
060 * <code>reason.toString() + '-' + severity.toString()</code>
061 * </p>
062 * <p>
063 * <b>IPP Compatibility:</b> PrinterStateReasons is an IPP 1.1 attribute.
064 * </p>
065 * @see javax.print.attribute.standard.PrinterState
066 * @see javax.print.attribute.standard.PrinterStateReason
067 * @see javax.print.attribute.standard.Severity
068 *
069 * @author Michael Koch (konqueror@gmx.de)
070 * @author Wolfgang Baer (WBaer@gmx.de)
071 */
072 public final class PrinterStateReasons
073 extends HashMap<PrinterStateReason, Severity>
074 implements PrintServiceAttribute
075 {
076 private static final long serialVersionUID = -3731791085163619457L;
077
078 /**
079 * Constructs an empty <code>PrinterStateReasons</code> attribute.
080 */
081 public PrinterStateReasons()
082 {
083 super();
084 }
085
086 /**
087 * Constructs an empty <code>PrinterStateReasons</code> attribute
088 * with the given initial capacity and load factor.
089 *
090 * @param initialCapacity the intial capacity.
091 * @param loadFactor the load factor of the underlying HashMap.
092 *
093 * @throws IllegalArgumentException if initialCapacity < 0
094 * @throws IllegalArgumentException if initialCapacity or loadFactor < 0
095 */
096 public PrinterStateReasons(int initialCapacity, float loadFactor)
097 {
098 super(initialCapacity, loadFactor);
099 }
100
101 /**
102 * Constructs an empty <code>PrinterStateReasons</code> attribute
103 * with the given initial capacity and the default load factor.
104 *
105 * @param initialCapacity the intial capacity.
106 *
107 * @throws IllegalArgumentException if initialCapacity < 0
108 */
109 public PrinterStateReasons(int initialCapacity)
110 {
111 super(initialCapacity);
112 }
113
114 /**
115 * Constructs a <code>PrinterStateReasons</code> attribute
116 * with the given content of the map.
117 *
118 * @param map the map for the initial values with the same
119 * <code>PrinterStateReason</code> to <code>Severity</code> mappings.
120 *
121 * @throws NullPointerException if map or any key/value is <code>null</code>.
122 * @throws ClassCastException if values of map are not of type
123 * <code>PrinterStateReason</code> and keys are not of type
124 * <code>Severity</code>.
125 */
126 public PrinterStateReasons(Map<PrinterStateReason,Severity> map)
127 {
128 super(map.size(), 0.75f);
129 for (Map.Entry<PrinterStateReason,Severity> entry : map.entrySet())
130 {
131 put(entry.getKey(), entry.getValue());
132 }
133 }
134
135 /**
136 * Constructs an unmodifiable view of the contained printer state reasons
137 * associated with the given severity level.
138 *
139 * @param severity the severity level for the constructed set.
140 * @return The set of printer state reasons.
141 */
142 public Set<PrinterStateReason> printerStateReasonSet(Severity severity)
143 {
144 if (severity == null)
145 throw new NullPointerException("severity is null");
146
147 HashSet set = new HashSet();
148 Iterator it = entrySet().iterator();
149 while (it.hasNext())
150 {
151 Map.Entry entry = (Map.Entry) it.next();
152 if (entry.getValue().equals(severity))
153 set.add(entry.getKey());
154 }
155
156 return Collections.unmodifiableSet(set);
157 }
158
159 /**
160 * Puts the given reason object associated with the given severity object
161 * into the set.
162 *
163 * @param reason the reason of type <code>PrinterStateReason</code>.
164 * @param severity the severity of the reason of type <code>Severity</code>.
165 *
166 * @return The previously associated severity of the reason or
167 * <code>null</code> if the reason object was not in the map before.
168 *
169 * @throws NullPointerException if any of the values is <code>null</code>.
170 * @throws ClassCastException if reason is not a
171 * <code>PrinterStateReason</code> and severity is not a
172 * <code>Severity</code> instance.
173 */
174 public Severity put(PrinterStateReason reason,Severity severity)
175 {
176 if (reason == null)
177 throw new NullPointerException("reason is null");
178 if (severity == null)
179 throw new NullPointerException("severity is null");
180
181 return super.put(reason, severity);
182 }
183
184 /**
185 * Returns category of this class.
186 *
187 * @return The class <code>PrintStateReasons</code> itself.
188 */
189 public Class< ? extends Attribute> getCategory()
190 {
191 return PrinterStateReasons.class;
192 }
193
194 /**
195 * Returns the name of this attribute.
196 *
197 * @return The name "printer-state-reasons".
198 */
199 public String getName()
200 {
201 return "printer-state-reasons";
202 }
203 }