Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / print / attribute / AttributeSetUtilities.java
blob32bee51b6cb611d99d7f31518c994973f9f9e3fe
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 /**
45 * This class isn't intended to be instantiated.
47 private AttributeSetUtilities() {}
49 private static class UnmodifiableAttributeSet
50 implements AttributeSet, Serializable
52 private AttributeSet set;
54 public UnmodifiableAttributeSet(AttributeSet attributeSet)
56 if (attributeSet == null)
57 throw new NullPointerException("attributeSet may not be null");
59 this.set = attributeSet;
62 public boolean add(Attribute attribute)
64 throw new UnmodifiableSetException();
67 public boolean addAll(AttributeSet attributes)
69 throw new UnmodifiableSetException();
72 public void clear()
74 throw new UnmodifiableSetException();
77 public boolean containsKey(Class category)
79 return set.containsKey(category);
82 public boolean containsValue(Attribute attribute)
84 return set.containsValue(attribute);
87 public boolean equals(Object obj)
89 return set.equals(obj);
92 public Attribute get(Class interfaceName)
94 return set.get(interfaceName);
97 public int hashCode()
99 return set.hashCode();
102 public boolean isEmpty()
104 return set.isEmpty();
107 public boolean remove(Class category)
109 throw new UnmodifiableSetException();
112 public boolean remove(Attribute attribute)
114 throw new UnmodifiableSetException();
117 public int size()
119 return set.size();
122 public Attribute[] toArray()
124 return set.toArray();
128 private static class UnmodifiableDocAttributeSet
129 extends UnmodifiableAttributeSet
130 implements DocAttributeSet, Serializable
132 public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet)
134 super(attributeSet);
138 private static class UnmodifiablePrintJobAttributeSet
139 extends UnmodifiableAttributeSet
140 implements PrintJobAttributeSet, Serializable
142 public UnmodifiablePrintJobAttributeSet(PrintJobAttributeSet attributeSet)
144 super(attributeSet);
148 private static class UnmodifiablePrintRequestAttributeSet
149 extends UnmodifiableAttributeSet
150 implements PrintRequestAttributeSet, Serializable
152 public UnmodifiablePrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
154 super(attributeSet);
158 private static class UnmodifiablePrintServiceAttributeSet
159 extends UnmodifiableAttributeSet
160 implements PrintServiceAttributeSet, Serializable
162 public UnmodifiablePrintServiceAttributeSet(PrintServiceAttributeSet attributeSet)
164 super(attributeSet);
168 private static class SynchronizedAttributeSet
169 implements AttributeSet, Serializable
171 private AttributeSet set;
173 public SynchronizedAttributeSet(AttributeSet attributeSet)
175 if (attributeSet == null)
176 throw new NullPointerException("attributeSet may not be null");
178 this.set = attributeSet;
181 public synchronized boolean add(Attribute attribute)
183 return set.add(attribute);
186 public synchronized boolean addAll(AttributeSet attributes)
188 return set.addAll(attributes);
191 public synchronized void clear()
193 set.clear();
196 public synchronized boolean containsKey(Class category)
198 return set.containsKey(category);
201 public synchronized boolean containsValue(Attribute attribute)
203 return set.containsValue(attribute);
206 public synchronized boolean equals(Object obj)
208 return set.equals(obj);
211 public synchronized Attribute get(Class interfaceName)
213 return set.get(interfaceName);
216 public synchronized int hashCode()
218 return set.hashCode();
221 public synchronized boolean isEmpty()
223 return set.isEmpty();
226 public synchronized boolean remove(Class category)
228 return set.remove(category);
231 public synchronized boolean remove(Attribute attribute)
233 return set.remove(attribute);
236 public synchronized int size()
238 return set.size();
241 public synchronized Attribute[] toArray()
243 return set.toArray();
247 private static class SynchronizedDocAttributeSet
248 extends SynchronizedAttributeSet
249 implements DocAttributeSet, Serializable
251 public SynchronizedDocAttributeSet(DocAttributeSet attributeSet)
253 super(attributeSet);
257 private static class SynchronizedPrintJobAttributeSet
258 extends SynchronizedAttributeSet
259 implements PrintJobAttributeSet, Serializable
261 public SynchronizedPrintJobAttributeSet(PrintJobAttributeSet attributeSet)
263 super(attributeSet);
267 private static class SynchronizedPrintRequestAttributeSet
268 extends SynchronizedAttributeSet
269 implements PrintRequestAttributeSet, Serializable
271 public SynchronizedPrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
273 super(attributeSet);
277 private static class SynchronizedPrintServiceAttributeSet
278 extends SynchronizedAttributeSet
279 implements PrintServiceAttributeSet, Serializable
281 public SynchronizedPrintServiceAttributeSet(PrintServiceAttributeSet attributeSet)
283 super(attributeSet);
288 * Returns a synchronized view of the given attribute set.
290 * @return the sychronized attribute set
292 public static AttributeSet synchronizedView(AttributeSet attributeSet)
294 return new SynchronizedAttributeSet(attributeSet);
298 * Returns a synchronized view of the given attribute set.
300 * @return the sychronized attribute set
302 public static DocAttributeSet synchronizedView(DocAttributeSet attributeSet)
304 return new SynchronizedDocAttributeSet(attributeSet);
308 * Returns a synchronized view of the given attribute set.
310 * @return the sychronized attribute set
312 public static PrintJobAttributeSet synchronizedView(PrintJobAttributeSet attributeSet)
314 return new SynchronizedPrintJobAttributeSet(attributeSet);
318 * Returns a synchronized view of the given attribute set.
320 * @return the sychronized attribute set
322 public static PrintRequestAttributeSet synchronizedView(PrintRequestAttributeSet attributeSet)
324 return new SynchronizedPrintRequestAttributeSet(attributeSet);
328 * Returns a synchronized view of the given attribute set.
330 * @return the sychronized attribute set
332 public static PrintServiceAttributeSet synchronizedView(PrintServiceAttributeSet attributeSet)
334 return new SynchronizedPrintServiceAttributeSet(attributeSet);
338 * Returns an unmodifiable view of the given attribute set.
340 * @return the sychronized attribute set
342 public static AttributeSet unmodifiableView(AttributeSet attributeSet)
344 return new UnmodifiableAttributeSet(attributeSet);
348 * Returns an unmodifiable view of the given attribute set.
350 * @return the sychronized attribute set
352 public static DocAttributeSet unmodifiableView(DocAttributeSet attributeSet)
354 return new UnmodifiableDocAttributeSet(attributeSet);
358 * Returns an unmodifiable view of the given attribute set.
360 * @return the sychronized attribute set
362 public static PrintJobAttributeSet unmodifiableView(PrintJobAttributeSet attributeSet)
364 return new UnmodifiablePrintJobAttributeSet(attributeSet);
368 * Returns an unmodifiable view of the given attribute set.
370 * @return the sychronized attribute set
372 public static PrintRequestAttributeSet unmodifiableView(PrintRequestAttributeSet attributeSet)
374 return new UnmodifiablePrintRequestAttributeSet(attributeSet);
378 * Returns an unmodifiable view of the given attribute set.
380 * @return the sychronized attribute set
382 public static PrintServiceAttributeSet unmodifiableView(PrintServiceAttributeSet attributeSet)
384 return new UnmodifiablePrintServiceAttributeSet(attributeSet);
388 * Verifies that the given object is a <code>Class</code> that
389 * implements the given interface name.
391 * @return object casted to <code>Class</code>
393 * @exception ClassCastException if object is not a <code>Class</code>
394 * that implements interfaceName
395 * @exception NullPointerException if object is null
397 public static Class verifyAttributeCategory(Object object,
398 Class interfaceName)
400 if (object == null)
401 throw new NullPointerException("object may not be null");
403 Class clazz = (Class) object;
405 if (interfaceName.isAssignableFrom(clazz))
406 return clazz;
408 throw new ClassCastException();
412 * Verifies that the given object is an attribute of the given interface.
414 * @return the object casted to <code>Attribute</code>
416 * @exception ClassCastException if object is no instance of interfaceName.
417 * @exception NullPointerException if object is null
419 public static Attribute verifyAttributeValue(Object object,
420 Class interfaceName)
422 if (object == null)
423 throw new NullPointerException("object may not be null");
425 if (interfaceName.isInstance(object))
426 return (Attribute) object;
428 throw new ClassCastException();
432 * Verifies that the category of attribute is equals to category.
434 * @param category the category the atteribute should be
435 * @param attribute the attribute to verify
437 * @exception IllegalArgumentException if the categories are not equal
438 * @exception NullPointerException if category is null
440 public static void verifyCategoryForValue(Class category,
441 Attribute attribute)
443 if (category == null)
444 throw new NullPointerException("object may not be null");
446 if (category.equals(attribute.getCategory()))
447 throw new IllegalArgumentException
448 ("category of attribute not equal to category");