2004-05-15 Mark Wielaard <mark@klomp.org>
[official-gcc.git] / libjava / javax / print / attribute / AttributeSetUtilities.java
blob3c72480c057193f5c1b5127da571a9d8fa456078
1 /* AttributeSetUtilities.java --
2 Copyright (C) 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.print.attribute;
40 import java.io.Serializable;
42 public final class AttributeSetUtilities
44 private static class UnmodifiableAttributeSet
45 implements AttributeSet, Serializable
47 private AttributeSet set;
49 public UnmodifiableAttributeSet(AttributeSet attributeSet)
51 if (attributeSet == null)
52 throw new NullPointerException("attributeSet may not be null");
54 this.set = attributeSet;
57 public boolean add(Attribute attribute)
59 throw new UnmodifiableSetException();
62 public boolean addAll(AttributeSet attributes)
64 throw new UnmodifiableSetException();
67 public void clear()
69 throw new UnmodifiableSetException();
72 public boolean containsKey(Class category)
74 return set.containsKey(category);
77 public boolean containsValue(Attribute attribute)
79 return set.containsValue(attribute);
82 public boolean equals(Object obj)
84 return set.equals(obj);
87 public Attribute get(Class interfaceName)
89 return set.get(interfaceName);
92 public int hashCode()
94 return set.hashCode();
97 public boolean isEmpty()
99 return set.isEmpty();
102 public boolean remove(Class category)
104 throw new UnmodifiableSetException();
107 public boolean remove(Attribute attribute)
109 throw new UnmodifiableSetException();
112 public int size()
114 return set.size();
117 public Attribute[] toArray()
119 return set.toArray();
123 public static class UnmodifiableDocAttributeSet
124 extends UnmodifiableAttributeSet
125 implements DocAttributeSet, Serializable
127 public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet)
129 super(attributeSet);
133 public static class UnmodifiablePrintJobAttributeSet
134 extends UnmodifiableAttributeSet
135 implements PrintJobAttributeSet, Serializable
137 public UnmodifiablePrintJobAttributeSet(PrintJobAttributeSet attributeSet)
139 super(attributeSet);
143 public static class UnmodifiablePrintRequestAttributeSet
144 extends UnmodifiableAttributeSet
145 implements PrintRequestAttributeSet, Serializable
147 public UnmodifiablePrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
149 super(attributeSet);
153 public static class UnmodifiablePrintServiceAttributeSet
154 extends UnmodifiableAttributeSet
155 implements PrintServiceAttributeSet, Serializable
157 public UnmodifiablePrintServiceAttributeSet(PrintServiceAttributeSet attributeSet)
159 super(attributeSet);
163 public static class SynchronizedAttributeSet
164 implements AttributeSet, Serializable
166 private AttributeSet set;
168 public SynchronizedAttributeSet(AttributeSet attributeSet)
170 if (attributeSet == null)
171 throw new NullPointerException("attributeSet may not be null");
173 this.set = attributeSet;
176 public synchronized boolean add(Attribute attribute)
178 return set.add(attribute);
181 public synchronized boolean addAll(AttributeSet attributes)
183 return set.addAll(attributes);
186 public synchronized void clear()
188 set.clear();
191 public synchronized boolean containsKey(Class category)
193 return set.containsKey(category);
196 public synchronized boolean containsValue(Attribute attribute)
198 return set.containsValue(attribute);
201 public synchronized boolean equals(Object obj)
203 return set.equals(obj);
206 public synchronized Attribute get(Class interfaceName)
208 return set.get(interfaceName);
211 public synchronized int hashCode()
213 return set.hashCode();
216 public synchronized boolean isEmpty()
218 return set.isEmpty();
221 public synchronized boolean remove(Class category)
223 return set.remove(category);
226 public synchronized boolean remove(Attribute attribute)
228 return set.remove(attribute);
231 public synchronized int size()
233 return set.size();
236 public synchronized Attribute[] toArray()
238 return set.toArray();
242 public static class SynchronizedDocAttributeSet
243 extends SynchronizedAttributeSet
244 implements DocAttributeSet, Serializable
246 public SynchronizedDocAttributeSet(DocAttributeSet attributeSet)
248 super(attributeSet);
252 public static class SynchronizedPrintJobAttributeSet
253 extends SynchronizedAttributeSet
254 implements PrintJobAttributeSet, Serializable
256 public SynchronizedPrintJobAttributeSet(PrintJobAttributeSet attributeSet)
258 super(attributeSet);
262 public static class SynchronizedPrintRequestAttributeSet
263 extends SynchronizedAttributeSet
264 implements PrintRequestAttributeSet, Serializable
266 public SynchronizedPrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
268 super(attributeSet);
272 public static class SynchronizedPrintServiceAttributeSet
273 extends SynchronizedAttributeSet
274 implements PrintServiceAttributeSet, Serializable
276 public SynchronizedPrintServiceAttributeSet(PrintServiceAttributeSet attributeSet)
278 super(attributeSet);
283 * Returns a synchronized view of the given attribute set.
285 * @return the sychronized attribute set
287 public static AttributeSet synchronizedView(AttributeSet attributeSet)
289 return new SynchronizedAttributeSet(attributeSet);
293 * Returns a synchronized view of the given attribute set.
295 * @return the sychronized attribute set
297 public static DocAttributeSet synchronizedView(DocAttributeSet attributeSet)
299 return new SynchronizedDocAttributeSet(attributeSet);
303 * Returns a synchronized view of the given attribute set.
305 * @return the sychronized attribute set
307 public static PrintJobAttributeSet synchronizedView(PrintJobAttributeSet attributeSet)
309 return new SynchronizedPrintJobAttributeSet(attributeSet);
313 * Returns a synchronized view of the given attribute set.
315 * @return the sychronized attribute set
317 public static PrintRequestAttributeSet synchronizedView(PrintRequestAttributeSet attributeSet)
319 return new SynchronizedPrintRequestAttributeSet(attributeSet);
323 * Returns a synchronized view of the given attribute set.
325 * @return the sychronized attribute set
327 public static PrintServiceAttributeSet synchronizedView(PrintServiceAttributeSet attributeSet)
329 return new SynchronizedPrintServiceAttributeSet(attributeSet);
333 * Returns an unmodifiable view of the given attribute set.
335 * @return the sychronized attribute set
337 public static AttributeSet unmodifiableView(AttributeSet attributeSet)
339 return new UnmodifiableAttributeSet(attributeSet);
343 * Returns an unmodifiable view of the given attribute set.
345 * @return the sychronized attribute set
347 public static DocAttributeSet unmodifiableView(DocAttributeSet attributeSet)
349 return new UnmodifiableDocAttributeSet(attributeSet);
353 * Returns an unmodifiable view of the given attribute set.
355 * @return the sychronized attribute set
357 public static PrintJobAttributeSet unmodifiableView(PrintJobAttributeSet attributeSet)
359 return new UnmodifiablePrintJobAttributeSet(attributeSet);
363 * Returns an unmodifiable view of the given attribute set.
365 * @return the sychronized attribute set
367 public static PrintRequestAttributeSet unmodifiableView(PrintRequestAttributeSet attributeSet)
369 return new UnmodifiablePrintRequestAttributeSet(attributeSet);
373 * Returns an unmodifiable view of the given attribute set.
375 * @return the sychronized attribute set
377 public static PrintServiceAttributeSet unmodifiableView(PrintServiceAttributeSet attributeSet)
379 return new UnmodifiablePrintServiceAttributeSet(attributeSet);
383 * Verifies that the given object is a <code>Class</code> that
384 * implements the given interface name.
386 * @return object casted to <code>Class</code>
388 * @exception ClassCastException if object is not a <code>Class</code>
389 * that implements interfaceName
390 * @exception NullPointerException if object is null
392 public static Class verifyAttributeCategory(Object object,
393 Class interfaceName)
395 if (object == null)
396 throw new NullPointerException("object may not be null");
398 Class clazz = (Class) object;
400 if (interfaceName.isAssignableFrom(clazz))
401 return clazz;
403 throw new ClassCastException();
407 * Verifies that the given object is an attribute of the given interface.
409 * @return the object casted to <code>Attribute</code>
411 * @exception ClassCastException if object is no instance of interfaceName.
412 * @exception NullPointerException if object is null
414 public static Attribute verifyAttributeValue(Object object,
415 Class interfaceName)
417 if (object == null)
418 throw new NullPointerException("object may not be null");
420 if (interfaceName.isInstance(object))
421 return (Attribute) object;
423 throw new ClassCastException();
427 * Verifies that the category of attribute is equals to category.
429 * @param category the category the atteribute should be
430 * @param attribute the attribute to verify
432 * @exception IllegalArgumentException if the categories are not equal
433 * @exception NullPointerException if category is null
435 public static void verifyCategoryForValue(Class category,
436 Attribute attribute)
438 if (category == null)
439 throw new NullPointerException("object may not be null");
441 if (category.equals(attribute.getCategory()))
442 throw new IllegalArgumentException
443 ("category of attribute not equal to category");