Source for javax.print.attribute.AttributeSetUtilities

   1: /* AttributeSetUtilities.java -- 
   2:    Copyright (C) 2003, 2004  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: package javax.print.attribute;
  39: 
  40: import java.io.Serializable;
  41: 
  42: public final class AttributeSetUtilities
  43: {
  44:   /**
  45:    * This class isn't intended to be instantiated.
  46:    */
  47:   private AttributeSetUtilities() {}
  48: 
  49:   private static class UnmodifiableAttributeSet
  50:     implements AttributeSet, Serializable
  51:   {
  52:     private AttributeSet set;
  53: 
  54:     public UnmodifiableAttributeSet(AttributeSet attributeSet)
  55:     {
  56:       if (attributeSet == null)
  57:         throw new NullPointerException("attributeSet may not be null");
  58: 
  59:       this.set = attributeSet;
  60:     }
  61: 
  62:     public boolean add(Attribute attribute)
  63:     {
  64:       throw new UnmodifiableSetException();
  65:     }
  66: 
  67:     public boolean addAll(AttributeSet attributes)
  68:     {
  69:       throw new UnmodifiableSetException();
  70:     }
  71:     
  72:     public void clear()
  73:     {
  74:       throw new UnmodifiableSetException();
  75:     }
  76: 
  77:     public boolean containsKey(Class category)
  78:     {
  79:       return set.containsKey(category);
  80:     }
  81: 
  82:     public boolean containsValue(Attribute attribute)
  83:     {
  84:       return set.containsValue(attribute);
  85:     }
  86: 
  87:     public boolean equals(Object obj)
  88:     {
  89:       return set.equals(obj);
  90:     }
  91:     
  92:     public Attribute get(Class interfaceName)
  93:     {
  94:       return set.get(interfaceName);
  95:     }
  96: 
  97:     public int hashCode()
  98:     {
  99:       return set.hashCode();
 100:     }
 101:     
 102:     public boolean isEmpty()
 103:     {
 104:       return set.isEmpty();
 105:     }
 106: 
 107:     public boolean remove(Class category)
 108:     {
 109:       throw new UnmodifiableSetException();
 110:     }
 111: 
 112:     public boolean remove(Attribute attribute)
 113:     {
 114:       throw new UnmodifiableSetException();
 115:     }
 116: 
 117:     public int size()
 118:     {
 119:       return set.size();
 120:     }
 121: 
 122:     public Attribute[] toArray()
 123:     {
 124:       return set.toArray();
 125:     }
 126:   }
 127: 
 128:   private static class UnmodifiableDocAttributeSet
 129:     extends UnmodifiableAttributeSet
 130:     implements DocAttributeSet, Serializable
 131:   {
 132:     public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet)
 133:     {
 134:       super(attributeSet);
 135:     }
 136:   }
 137: 
 138:   private static class UnmodifiablePrintJobAttributeSet
 139:     extends UnmodifiableAttributeSet
 140:     implements PrintJobAttributeSet, Serializable
 141:   {
 142:     public UnmodifiablePrintJobAttributeSet(PrintJobAttributeSet attributeSet)
 143:     {
 144:       super(attributeSet);
 145:     }
 146:   }
 147: 
 148:   private static class UnmodifiablePrintRequestAttributeSet
 149:     extends UnmodifiableAttributeSet
 150:     implements PrintRequestAttributeSet, Serializable
 151:   {
 152:     public UnmodifiablePrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
 153:     {
 154:       super(attributeSet);
 155:     }
 156:   }
 157: 
 158:   private static class UnmodifiablePrintServiceAttributeSet
 159:     extends UnmodifiableAttributeSet
 160:     implements PrintServiceAttributeSet, Serializable
 161:   {
 162:     public UnmodifiablePrintServiceAttributeSet(PrintServiceAttributeSet attributeSet)
 163:     {
 164:       super(attributeSet);
 165:     }
 166:   }
 167: 
 168:   private static class SynchronizedAttributeSet
 169:     implements AttributeSet, Serializable
 170:   {
 171:     private AttributeSet set;
 172: 
 173:     public SynchronizedAttributeSet(AttributeSet attributeSet)
 174:     {
 175:       if (attributeSet == null)
 176:         throw new NullPointerException("attributeSet may not be null");
 177: 
 178:       this.set = attributeSet;
 179:     }
 180: 
 181:     public synchronized boolean add(Attribute attribute)
 182:     {
 183:       return set.add(attribute);
 184:     }
 185: 
 186:     public synchronized boolean addAll(AttributeSet attributes)
 187:     {
 188:       return set.addAll(attributes);
 189:     }
 190:     
 191:     public synchronized void clear()
 192:     {
 193:       set.clear();
 194:     }
 195: 
 196:     public synchronized boolean containsKey(Class category)
 197:     {
 198:       return set.containsKey(category);
 199:     }
 200: 
 201:     public synchronized boolean containsValue(Attribute attribute)
 202:     {
 203:       return set.containsValue(attribute);
 204:     }
 205: 
 206:     public synchronized boolean equals(Object obj)
 207:     {
 208:       return set.equals(obj);
 209:     }
 210:     
 211:     public synchronized Attribute get(Class interfaceName)
 212:     {
 213:       return set.get(interfaceName);
 214:     }
 215: 
 216:     public synchronized int hashCode()
 217:     {
 218:       return set.hashCode();
 219:     }
 220:     
 221:     public synchronized boolean isEmpty()
 222:     {
 223:       return set.isEmpty();
 224:     }
 225: 
 226:     public synchronized boolean remove(Class category)
 227:     {
 228:       return set.remove(category);
 229:     }
 230: 
 231:     public synchronized boolean remove(Attribute attribute)
 232:     {
 233:       return set.remove(attribute);
 234:     }
 235: 
 236:     public synchronized int size()
 237:     {
 238:       return set.size();
 239:     }
 240: 
 241:     public synchronized Attribute[] toArray()
 242:     {
 243:       return set.toArray();
 244:     }
 245:   }
 246: 
 247:   private static class SynchronizedDocAttributeSet
 248:     extends SynchronizedAttributeSet
 249:     implements DocAttributeSet, Serializable
 250:   {
 251:     public SynchronizedDocAttributeSet(DocAttributeSet attributeSet)
 252:     {
 253:       super(attributeSet);
 254:     }
 255:   }
 256: 
 257:   private static class SynchronizedPrintJobAttributeSet
 258:     extends SynchronizedAttributeSet
 259:     implements PrintJobAttributeSet, Serializable
 260:   {
 261:     public SynchronizedPrintJobAttributeSet(PrintJobAttributeSet attributeSet)
 262:     {
 263:       super(attributeSet);
 264:     }
 265:   }
 266: 
 267:   private static class SynchronizedPrintRequestAttributeSet
 268:     extends SynchronizedAttributeSet
 269:     implements PrintRequestAttributeSet, Serializable
 270:   {
 271:     public SynchronizedPrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
 272:     {
 273:       super(attributeSet);
 274:     }
 275:   }
 276: 
 277:   private static class SynchronizedPrintServiceAttributeSet
 278:     extends SynchronizedAttributeSet
 279:     implements PrintServiceAttributeSet, Serializable
 280:   {
 281:     public SynchronizedPrintServiceAttributeSet(PrintServiceAttributeSet attributeSet)
 282:     {
 283:       super(attributeSet);
 284:     }
 285:   }
 286: 
 287:   /**
 288:    * Returns a synchronized view of the given attribute set.
 289:    *
 290:    * @return the sychronized attribute set
 291:    */
 292:   public static AttributeSet synchronizedView(AttributeSet attributeSet)
 293:   {
 294:     return new SynchronizedAttributeSet(attributeSet);
 295:   }
 296: 
 297:   /**
 298:    * Returns a synchronized view of the given attribute set.
 299:    *
 300:    * @return the sychronized attribute set
 301:    */
 302:   public static DocAttributeSet synchronizedView(DocAttributeSet attributeSet)
 303:   {
 304:     return new SynchronizedDocAttributeSet(attributeSet);
 305:   }
 306:   
 307:   /**
 308:    * Returns a synchronized view of the given attribute set.
 309:    *
 310:    * @return the sychronized attribute set
 311:    */
 312:   public static PrintJobAttributeSet synchronizedView(PrintJobAttributeSet attributeSet)
 313:   {
 314:     return new SynchronizedPrintJobAttributeSet(attributeSet);
 315:   }
 316:   
 317:   /**
 318:    * Returns a synchronized view of the given attribute set.
 319:    *
 320:    * @return the sychronized attribute set
 321:    */
 322:   public static PrintRequestAttributeSet synchronizedView(PrintRequestAttributeSet attributeSet)
 323:   {
 324:     return new SynchronizedPrintRequestAttributeSet(attributeSet);
 325:   }
 326:   
 327:   /**
 328:    * Returns a synchronized view of the given attribute set.
 329:    *
 330:    * @return the sychronized attribute set
 331:    */
 332:   public static PrintServiceAttributeSet synchronizedView(PrintServiceAttributeSet attributeSet)
 333:   {
 334:     return new SynchronizedPrintServiceAttributeSet(attributeSet);
 335:   }
 336:   
 337:   /**
 338:    * Returns an unmodifiable view of the given attribute set.
 339:    *
 340:    * @return the sychronized attribute set
 341:    */
 342:   public static AttributeSet unmodifiableView(AttributeSet attributeSet)
 343:   {
 344:     return new UnmodifiableAttributeSet(attributeSet);
 345:   }
 346:   
 347:   /**
 348:    * Returns an unmodifiable view of the given attribute set.
 349:    *
 350:    * @return the sychronized attribute set
 351:    */
 352:   public static DocAttributeSet unmodifiableView(DocAttributeSet attributeSet)
 353:   {
 354:     return new UnmodifiableDocAttributeSet(attributeSet);
 355:   }
 356:   
 357:   /**
 358:    * Returns an unmodifiable view of the given attribute set.
 359:    *
 360:    * @return the sychronized attribute set
 361:    */
 362:   public static PrintJobAttributeSet unmodifiableView(PrintJobAttributeSet attributeSet)
 363:   {
 364:     return new UnmodifiablePrintJobAttributeSet(attributeSet);
 365:   }
 366:   
 367:   /**
 368:    * Returns an unmodifiable view of the given attribute set.
 369:    *
 370:    * @return the sychronized attribute set
 371:    */
 372:   public static PrintRequestAttributeSet unmodifiableView(PrintRequestAttributeSet attributeSet)
 373:   {
 374:     return new UnmodifiablePrintRequestAttributeSet(attributeSet);
 375:   }
 376:   
 377:   /**
 378:    * Returns an unmodifiable view of the given attribute set.
 379:    *
 380:    * @return the sychronized attribute set
 381:    */
 382:   public static PrintServiceAttributeSet unmodifiableView(PrintServiceAttributeSet attributeSet)
 383:   {
 384:     return new UnmodifiablePrintServiceAttributeSet(attributeSet);
 385:   }
 386: 
 387:   /**
 388:    * Verifies that the given object is a <code>Class</code> that
 389:    * implements the given interface name.
 390:    *
 391:    * @return object casted to <code>Class</code>
 392:    *
 393:    * @exception ClassCastException if object is not a <code>Class</code>
 394:    * that implements interfaceName
 395:    * @exception NullPointerException if object is null
 396:    */
 397:   public static Class verifyAttributeCategory(Object object,
 398:                                               Class interfaceName)
 399:   {
 400:     if (object == null)
 401:       throw new NullPointerException("object may not be null");
 402: 
 403:     Class clazz = (Class) object;
 404: 
 405:     if (interfaceName.isAssignableFrom(clazz))
 406:       return clazz;
 407: 
 408:     throw new ClassCastException();
 409:   }
 410:   
 411:   /**
 412:    * Verifies that the given object is an attribute of the given interface.
 413:    *
 414:    * @return the object casted to <code>Attribute</code>
 415:    *
 416:    * @exception ClassCastException if object is no instance of interfaceName.
 417:    * @exception NullPointerException if object is null
 418:    */
 419:   public static Attribute verifyAttributeValue(Object object,
 420:                                                Class interfaceName)
 421:   {
 422:     if (object == null)
 423:       throw new NullPointerException("object may not be null");
 424: 
 425:     if (interfaceName.isInstance(object))
 426:       return (Attribute) object;
 427: 
 428:     throw new ClassCastException();
 429:   }
 430: 
 431:   /**
 432:    * Verifies that the category of attribute is equals to category.
 433:    *
 434:    * @param category the category the atteribute should be
 435:    * @param attribute the attribute to verify
 436:    *
 437:    * @exception IllegalArgumentException if the categories are not equal
 438:    * @exception NullPointerException if category is null
 439:    */
 440:   public static void verifyCategoryForValue(Class category,
 441:                                             Attribute attribute)
 442:   {
 443:     if (category == null)
 444:       throw new NullPointerException("object may not be null");
 445: 
 446:     if (category.equals(attribute.getCategory()))
 447:       throw new IllegalArgumentException
 448:         ("category of attribute not equal to category");
 449:   }
 450: }