AttributeSetUtilities.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /* AttributeSetUtilities.java --
  2. Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.print.attribute;
  32. import java.io.Serializable;
  33. /**
  34. * <code>AttributeSetUtilities</code> provides static methods for working
  35. * with <code>AttributeSet</code>s.
  36. * <p>
  37. * For every type of an attribute set available in the Java Print Service API
  38. * are methods provided to get an unmodifiable view of an attribute set.
  39. * This unmodifiable view provides a read-only version of the attribute
  40. * set which throws {@link javax.print.attribute.UnmodifiableSetException}s
  41. * if state changing methods are invoked.
  42. * </p>
  43. * <p>
  44. * Methods for getting a synchronized view of an attribute set are also
  45. * available. This view provides synchronized (thread safe) access to the
  46. * underlying wrapped attribute set.
  47. * </P>
  48. * <p>
  49. * Three static methods for the implementation of own AttributeSets
  50. * are provided, which verify that:
  51. * <ul>
  52. * <li>the given object is an attribute of the given interface.</li>
  53. * <li>the category of given attribute is equals to a given category.</li>
  54. * <li>the given object is a <code>Class</code> that implements the given
  55. * interface name.</li>
  56. * </ul>
  57. *
  58. */
  59. public final class AttributeSetUtilities
  60. {
  61. /**
  62. * This class isn't intended to be instantiated.
  63. */
  64. private AttributeSetUtilities()
  65. {
  66. // only static methods
  67. }
  68. private static class UnmodifiableAttributeSet
  69. implements AttributeSet, Serializable
  70. {
  71. private AttributeSet attrset;
  72. public UnmodifiableAttributeSet(AttributeSet attributeSet)
  73. {
  74. if (attributeSet == null)
  75. throw new NullPointerException("attributeSet may not be null");
  76. this.attrset = attributeSet;
  77. }
  78. public boolean add(Attribute attribute)
  79. {
  80. throw new UnmodifiableSetException();
  81. }
  82. public boolean addAll(AttributeSet attributes)
  83. {
  84. throw new UnmodifiableSetException();
  85. }
  86. public void clear()
  87. {
  88. throw new UnmodifiableSetException();
  89. }
  90. public boolean containsKey(Class category)
  91. {
  92. return attrset.containsKey(category);
  93. }
  94. public boolean containsValue(Attribute attribute)
  95. {
  96. return attrset.containsValue(attribute);
  97. }
  98. public boolean equals(Object obj)
  99. {
  100. return attrset.equals(obj);
  101. }
  102. public Attribute get(Class interfaceName)
  103. {
  104. return attrset.get(interfaceName);
  105. }
  106. public int hashCode()
  107. {
  108. return attrset.hashCode();
  109. }
  110. public boolean isEmpty()
  111. {
  112. return attrset.isEmpty();
  113. }
  114. public boolean remove(Class category)
  115. {
  116. throw new UnmodifiableSetException();
  117. }
  118. public boolean remove(Attribute attribute)
  119. {
  120. throw new UnmodifiableSetException();
  121. }
  122. public int size()
  123. {
  124. return attrset.size();
  125. }
  126. public Attribute[] toArray()
  127. {
  128. return attrset.toArray();
  129. }
  130. }
  131. private static class UnmodifiableDocAttributeSet
  132. extends UnmodifiableAttributeSet
  133. implements DocAttributeSet, Serializable
  134. {
  135. public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet)
  136. {
  137. super(attributeSet);
  138. }
  139. }
  140. private static class UnmodifiablePrintJobAttributeSet
  141. extends UnmodifiableAttributeSet
  142. implements PrintJobAttributeSet, Serializable
  143. {
  144. public UnmodifiablePrintJobAttributeSet(PrintJobAttributeSet attributeSet)
  145. {
  146. super(attributeSet);
  147. }
  148. }
  149. private static class UnmodifiablePrintRequestAttributeSet
  150. extends UnmodifiableAttributeSet
  151. implements PrintRequestAttributeSet, Serializable
  152. {
  153. public UnmodifiablePrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
  154. {
  155. super(attributeSet);
  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. private static class SynchronizedAttributeSet
  168. implements AttributeSet, Serializable
  169. {
  170. private AttributeSet attrset;
  171. public SynchronizedAttributeSet(AttributeSet attributeSet)
  172. {
  173. if (attributeSet == null)
  174. throw new NullPointerException("attributeSet may not be null");
  175. attrset = attributeSet;
  176. }
  177. public synchronized boolean add(Attribute attribute)
  178. {
  179. return attrset.add(attribute);
  180. }
  181. public synchronized boolean addAll(AttributeSet attributes)
  182. {
  183. return attrset.addAll(attributes);
  184. }
  185. public synchronized void clear()
  186. {
  187. attrset.clear();
  188. }
  189. public synchronized boolean containsKey(Class category)
  190. {
  191. return attrset.containsKey(category);
  192. }
  193. public synchronized boolean containsValue(Attribute attribute)
  194. {
  195. return attrset.containsValue(attribute);
  196. }
  197. public synchronized boolean equals(Object obj)
  198. {
  199. return attrset.equals(obj);
  200. }
  201. public synchronized Attribute get(Class interfaceName)
  202. {
  203. return attrset.get(interfaceName);
  204. }
  205. public synchronized int hashCode()
  206. {
  207. return attrset.hashCode();
  208. }
  209. public synchronized boolean isEmpty()
  210. {
  211. return attrset.isEmpty();
  212. }
  213. public synchronized boolean remove(Class category)
  214. {
  215. return attrset.remove(category);
  216. }
  217. public synchronized boolean remove(Attribute attribute)
  218. {
  219. return attrset.remove(attribute);
  220. }
  221. public synchronized int size()
  222. {
  223. return attrset.size();
  224. }
  225. public synchronized Attribute[] toArray()
  226. {
  227. return attrset.toArray();
  228. }
  229. }
  230. private static class SynchronizedDocAttributeSet
  231. extends SynchronizedAttributeSet
  232. implements DocAttributeSet, Serializable
  233. {
  234. public SynchronizedDocAttributeSet(DocAttributeSet attributeSet)
  235. {
  236. super(attributeSet);
  237. }
  238. }
  239. private static class SynchronizedPrintJobAttributeSet
  240. extends SynchronizedAttributeSet
  241. implements PrintJobAttributeSet, Serializable
  242. {
  243. public SynchronizedPrintJobAttributeSet(PrintJobAttributeSet attributeSet)
  244. {
  245. super(attributeSet);
  246. }
  247. }
  248. private static class SynchronizedPrintRequestAttributeSet
  249. extends SynchronizedAttributeSet
  250. implements PrintRequestAttributeSet, Serializable
  251. {
  252. public SynchronizedPrintRequestAttributeSet(PrintRequestAttributeSet attributeSet)
  253. {
  254. super(attributeSet);
  255. }
  256. }
  257. private static class SynchronizedPrintServiceAttributeSet
  258. extends SynchronizedAttributeSet
  259. implements PrintServiceAttributeSet, Serializable
  260. {
  261. public SynchronizedPrintServiceAttributeSet(PrintServiceAttributeSet attributeSet)
  262. {
  263. super(attributeSet);
  264. }
  265. }
  266. /**
  267. * Returns a synchronized view of the given attribute set.
  268. *
  269. * @param attributeSet the set to synchronize.
  270. * @return The sychronized attribute set.
  271. */
  272. public static AttributeSet synchronizedView(AttributeSet attributeSet)
  273. {
  274. return new SynchronizedAttributeSet(attributeSet);
  275. }
  276. /**
  277. * Returns a synchronized view of the given attribute set.
  278. *
  279. * @param attributeSet the set to synchronize.
  280. * @return The sychronized attribute set.
  281. */
  282. public static DocAttributeSet synchronizedView(DocAttributeSet attributeSet)
  283. {
  284. return new SynchronizedDocAttributeSet(attributeSet);
  285. }
  286. /**
  287. * Returns a synchronized view of the given attribute set.
  288. *
  289. * @param attributeSet the set to synchronize.
  290. * @return The sychronized attribute set.
  291. */
  292. public static PrintJobAttributeSet synchronizedView(PrintJobAttributeSet attributeSet)
  293. {
  294. return new SynchronizedPrintJobAttributeSet(attributeSet);
  295. }
  296. /**
  297. * Returns a synchronized view of the given attribute set.
  298. *
  299. * @param attributeSet the set to synchronize.
  300. * @return The sychronized attribute set.
  301. */
  302. public static PrintRequestAttributeSet synchronizedView(PrintRequestAttributeSet attributeSet)
  303. {
  304. return new SynchronizedPrintRequestAttributeSet(attributeSet);
  305. }
  306. /**
  307. * Returns a synchronized view of the given attribute set.
  308. *
  309. * @param attributeSet the set to synchronize.
  310. * @return The sychronized attribute set.
  311. */
  312. public static PrintServiceAttributeSet synchronizedView(PrintServiceAttributeSet attributeSet)
  313. {
  314. return new SynchronizedPrintServiceAttributeSet(attributeSet);
  315. }
  316. /**
  317. * Returns an unmodifiable view of the given attribute set.
  318. *
  319. * @param attributeSet the set to make unmodifiable.
  320. * @return The unmodifiable attribute set.
  321. */
  322. public static AttributeSet unmodifiableView(AttributeSet attributeSet)
  323. {
  324. return new UnmodifiableAttributeSet(attributeSet);
  325. }
  326. /**
  327. * Returns an unmodifiable view of the given attribute set.
  328. *
  329. * @param attributeSet the set to make unmodifiable.
  330. * @return The unmodifiable attribute set.
  331. */
  332. public static DocAttributeSet unmodifiableView(DocAttributeSet attributeSet)
  333. {
  334. return new UnmodifiableDocAttributeSet(attributeSet);
  335. }
  336. /**
  337. * Returns an unmodifiable view of the given attribute set.
  338. *
  339. * @param attributeSet the set to make unmodifiable.
  340. * @return The unmodifiable attribute set.
  341. */
  342. public static PrintJobAttributeSet unmodifiableView(PrintJobAttributeSet attributeSet)
  343. {
  344. return new UnmodifiablePrintJobAttributeSet(attributeSet);
  345. }
  346. /**
  347. * Returns an unmodifiable view of the given attribute set.
  348. *
  349. * @param attributeSet the set to make unmodifiable.
  350. * @return The unmodifiable attribute set.
  351. */
  352. public static PrintRequestAttributeSet unmodifiableView(PrintRequestAttributeSet attributeSet)
  353. {
  354. return new UnmodifiablePrintRequestAttributeSet(attributeSet);
  355. }
  356. /**
  357. * Returns an unmodifiable view of the given attribute set.
  358. *
  359. * @param attributeSet the set to make unmodifiable.
  360. * @return The unmodifiable attribute set.
  361. */
  362. public static PrintServiceAttributeSet unmodifiableView(PrintServiceAttributeSet attributeSet)
  363. {
  364. return new UnmodifiablePrintServiceAttributeSet(attributeSet);
  365. }
  366. /**
  367. * Verifies that the given object is a <code>Class</code> that
  368. * implements the given interface name and returns it casted.
  369. *
  370. * @param object the object to test.
  371. * @param interfaceName the <code>Class</code> to verify against.
  372. * @return object casted to <code>Class</code>
  373. *
  374. * @exception ClassCastException if object is not a <code>Class</code>
  375. * that implements interfaceName
  376. * @exception NullPointerException if object is null
  377. */
  378. public static Class<?> verifyAttributeCategory(Object object,
  379. Class<?> interfaceName)
  380. {
  381. if (object == null)
  382. throw new NullPointerException("object may not be null");
  383. Class clazz = (Class) object;
  384. if (interfaceName.isAssignableFrom(clazz))
  385. return clazz;
  386. throw new ClassCastException();
  387. }
  388. /**
  389. * Verifies that the given object is an attribute of the given interface.
  390. * and returns it casted to the interface type.
  391. *
  392. * @param object the object to test.
  393. * @param interfaceName the <code>Class</code> to verify against.
  394. * @return the object casted to <code>Attribute</code>
  395. *
  396. * @exception ClassCastException if object is no instance of interfaceName.
  397. * @exception NullPointerException if object is null
  398. */
  399. public static Attribute verifyAttributeValue(Object object,
  400. Class<?> interfaceName)
  401. {
  402. if (object == null)
  403. throw new NullPointerException("object may not be null");
  404. if (interfaceName.isInstance(object))
  405. return (Attribute) object;
  406. throw new ClassCastException();
  407. }
  408. /**
  409. * Verifies that the category of attribute is equals to the given category
  410. * class.
  411. *
  412. * @param category the category to test.
  413. * @param attribute the attribute to verify.
  414. *
  415. * @exception IllegalArgumentException if the categories are not equal
  416. * @exception NullPointerException if category is null
  417. */
  418. public static void verifyCategoryForValue(Class<?> category,
  419. Attribute attribute)
  420. {
  421. if (category == null || attribute == null)
  422. throw new NullPointerException("category or attribute may not be null");
  423. if (!category.equals(attribute.getCategory()))
  424. throw new IllegalArgumentException
  425. ("category of attribute not equal to category");
  426. }
  427. }