1. /*
  2. * @(#)AttributeSetUtilities.java 1.10 04/05/05
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.print.attribute;
  8. import java.io.Serializable;
  9. /**
  10. * Class AttributeSetUtilities provides static methods for manipulating
  11. * AttributeSets.
  12. * <ul>
  13. * <li>Methods for creating unmodifiable and synchronized views of attribute
  14. * sets.
  15. * <li>operations useful for building
  16. * implementations of interface {@link AttributeSet AttributeSet}
  17. * </ul>
  18. * <P>
  19. * An <B>unmodifiable view</B> <I>U</I> of an AttributeSet <I>S</I> provides a
  20. * client with "read-only" access to <I>S</I>. Query operations on <I>U</I>
  21. * "read through" to <I>S</I> thus, changes in <I>S</I> are reflected in
  22. * <I>U</I>. However, any attempt to modify <I>U</I>,
  23. * results in an UnmodifiableSetException.
  24. * The unmodifiable view object <I>U</I> will be serializable if the
  25. * attribute set object <I>S</I> is serializable.
  26. * <P>
  27. * A <B>synchronized view</B> <I>V</I> of an attribute set <I>S</I> provides a
  28. * client with synchronized (multiple thread safe) access to <I>S</I>. Each
  29. * operation of <I>V</I> is synchronized using <I>V</I> itself as the lock
  30. * object and then merely invokes the corresponding operation of <I>S</I>. In
  31. * order to guarantee mutually exclusive access, it is critical that all
  32. * access to <I>S</I> is accomplished through <I>V</I>. The synchronized view
  33. * object <I>V</I> will be serializable if the attribute set object <I>S</I>
  34. * is serializable.
  35. * <P>
  36. * As mentioned in the package description of javax.print, a null reference
  37. * parameter to methods is
  38. * incorrect unless explicitly documented on the method as having a meaningful
  39. * interpretation. Usage to the contrary is incorrect coding and may result in
  40. * a run time exception either immediately
  41. * or at some later time. IllegalArgumentException and NullPointerException
  42. * are examples of typical and acceptable run time exceptions for such cases.
  43. *
  44. * @author Alan Kaminsky
  45. */
  46. public final class AttributeSetUtilities {
  47. /* Suppress default constructor, ensuring non-instantiability.
  48. */
  49. private AttributeSetUtilities() {
  50. }
  51. /**
  52. * @serial include
  53. */
  54. private static class UnmodifiableAttributeSet
  55. implements AttributeSet, Serializable {
  56. private AttributeSet attrset;
  57. /* Unmodifiable view of the underlying attribute set.
  58. */
  59. public UnmodifiableAttributeSet(AttributeSet attributeSet) {
  60. attrset = attributeSet;
  61. }
  62. public Attribute get(Class<?> key) {
  63. return attrset.get(key);
  64. }
  65. public boolean add(Attribute attribute) {
  66. throw new UnmodifiableSetException();
  67. }
  68. public synchronized boolean remove(Class<?> category) {
  69. throw new UnmodifiableSetException();
  70. }
  71. public boolean remove(Attribute attribute) {
  72. throw new UnmodifiableSetException();
  73. }
  74. public boolean containsKey(Class<?> category) {
  75. return attrset.containsKey(category);
  76. }
  77. public boolean containsValue(Attribute attribute) {
  78. return attrset.containsValue(attribute);
  79. }
  80. public boolean addAll(AttributeSet attributes) {
  81. throw new UnmodifiableSetException();
  82. }
  83. public int size() {
  84. return attrset.size();
  85. }
  86. public Attribute[] toArray() {
  87. return attrset.toArray();
  88. }
  89. public void clear() {
  90. throw new UnmodifiableSetException();
  91. }
  92. public boolean isEmpty() {
  93. return attrset.isEmpty();
  94. }
  95. public boolean equals(Object o) {
  96. return attrset.equals (o);
  97. }
  98. public int hashCode() {
  99. return attrset.hashCode();
  100. }
  101. }
  102. /**
  103. * @serial include
  104. */
  105. private static class UnmodifiableDocAttributeSet
  106. extends UnmodifiableAttributeSet
  107. implements DocAttributeSet, Serializable {
  108. public UnmodifiableDocAttributeSet(DocAttributeSet attributeSet) {
  109. super (attributeSet);
  110. }
  111. }
  112. /**
  113. * @serial include
  114. */
  115. private static class UnmodifiablePrintRequestAttributeSet
  116. extends UnmodifiableAttributeSet
  117. implements PrintRequestAttributeSet, Serializable
  118. {
  119. public UnmodifiablePrintRequestAttributeSet
  120. (PrintRequestAttributeSet attributeSet) {
  121. super (attributeSet);
  122. }
  123. }
  124. /**
  125. * @serial include
  126. */
  127. private static class UnmodifiablePrintJobAttributeSet
  128. extends UnmodifiableAttributeSet
  129. implements PrintJobAttributeSet, Serializable
  130. {
  131. public UnmodifiablePrintJobAttributeSet
  132. (PrintJobAttributeSet attributeSet) {
  133. super (attributeSet);
  134. }
  135. }
  136. /**
  137. * @serial include
  138. */
  139. private static class UnmodifiablePrintServiceAttributeSet
  140. extends UnmodifiableAttributeSet
  141. implements PrintServiceAttributeSet, Serializable
  142. {
  143. public UnmodifiablePrintServiceAttributeSet
  144. (PrintServiceAttributeSet attributeSet) {
  145. super (attributeSet);
  146. }
  147. }
  148. /**
  149. * Creates an unmodifiable view of the given attribute set.
  150. *
  151. * @param attributeSet Underlying attribute set.
  152. *
  153. * @return Unmodifiable view of <CODE>attributeSet</CODE>.
  154. *
  155. * @exception NullPointerException
  156. * Thrown if <CODE>attributeSet</CODE> is null. Null is never a
  157. */
  158. public static AttributeSet unmodifiableView(AttributeSet attributeSet) {
  159. if (attributeSet == null) {
  160. throw new NullPointerException();
  161. }
  162. return new UnmodifiableAttributeSet(attributeSet);
  163. }
  164. /**
  165. * Creates an unmodifiable view of the given doc attribute set.
  166. *
  167. * @param attributeSet Underlying doc attribute set.
  168. *
  169. * @return Unmodifiable view of <CODE>attributeSet</CODE>.
  170. *
  171. * @exception NullPointerException
  172. * Thrown if <CODE>attributeSet</CODE> is null.
  173. */
  174. public static DocAttributeSet unmodifiableView
  175. (DocAttributeSet attributeSet) {
  176. if (attributeSet == null) {
  177. throw new NullPointerException();
  178. }
  179. return new UnmodifiableDocAttributeSet(attributeSet);
  180. }
  181. /**
  182. * Creates an unmodifiable view of the given print request attribute set.
  183. *
  184. * @param attributeSet Underlying print request attribute set.
  185. *
  186. * @return Unmodifiable view of <CODE>attributeSet</CODE>.
  187. *
  188. * @exception NullPointerException
  189. * Thrown if <CODE>attributeSet</CODE> is null.
  190. */
  191. public static PrintRequestAttributeSet
  192. unmodifiableView(PrintRequestAttributeSet attributeSet) {
  193. if (attributeSet == null) {
  194. throw new NullPointerException();
  195. }
  196. return new UnmodifiablePrintRequestAttributeSet(attributeSet);
  197. }
  198. /**
  199. * Creates an unmodifiable view of the given print job attribute set.
  200. *
  201. * @param attributeSet Underlying print job attribute set.
  202. *
  203. * @return Unmodifiable view of <CODE>attributeSet</CODE>.
  204. *
  205. * @exception NullPointerException
  206. * Thrown if <CODE>attributeSet</CODE> is null.
  207. */
  208. public static PrintJobAttributeSet
  209. unmodifiableView(PrintJobAttributeSet attributeSet) {
  210. if (attributeSet == null) {
  211. throw new NullPointerException();
  212. }
  213. return new UnmodifiablePrintJobAttributeSet(attributeSet);
  214. }
  215. /**
  216. * Creates an unmodifiable view of the given print service attribute set.
  217. *
  218. * @param attributeSet Underlying print service attribute set.
  219. *
  220. * @return Unmodifiable view of <CODE>attributeSet</CODE>.
  221. *
  222. * @exception NullPointerException
  223. * Thrown if <CODE>attributeSet</CODE> is null.
  224. */
  225. public static PrintServiceAttributeSet
  226. unmodifiableView(PrintServiceAttributeSet attributeSet) {
  227. if (attributeSet == null) {
  228. throw new NullPointerException();
  229. }
  230. return new UnmodifiablePrintServiceAttributeSet (attributeSet);
  231. }
  232. /**
  233. * @serial include
  234. */
  235. private static class SynchronizedAttributeSet
  236. implements AttributeSet, Serializable {
  237. private AttributeSet attrset;
  238. public SynchronizedAttributeSet(AttributeSet attributeSet) {
  239. attrset = attributeSet;
  240. }
  241. public synchronized Attribute get(Class<?> category) {
  242. return attrset.get(category);
  243. }
  244. public synchronized boolean add(Attribute attribute) {
  245. return attrset.add(attribute);
  246. }
  247. public synchronized boolean remove(Class<?> category) {
  248. return attrset.remove(category);
  249. }
  250. public synchronized boolean remove(Attribute attribute) {
  251. return attrset.remove(attribute);
  252. }
  253. public synchronized boolean containsKey(Class<?> category) {
  254. return attrset.containsKey(category);
  255. }
  256. public synchronized boolean containsValue(Attribute attribute) {
  257. return attrset.containsValue(attribute);
  258. }
  259. public synchronized boolean addAll(AttributeSet attributes) {
  260. return attrset.addAll(attributes);
  261. }
  262. public synchronized int size() {
  263. return attrset.size();
  264. }
  265. public synchronized Attribute[] toArray() {
  266. return attrset.toArray();
  267. }
  268. public synchronized void clear() {
  269. attrset.clear();
  270. }
  271. public synchronized boolean isEmpty() {
  272. return attrset.isEmpty();
  273. }
  274. public synchronized boolean equals(Object o) {
  275. return attrset.equals (o);
  276. }
  277. public synchronized int hashCode() {
  278. return attrset.hashCode();
  279. }
  280. }
  281. /**
  282. * @serial include
  283. */
  284. private static class SynchronizedDocAttributeSet
  285. extends SynchronizedAttributeSet
  286. implements DocAttributeSet, Serializable {
  287. public SynchronizedDocAttributeSet(DocAttributeSet attributeSet) {
  288. super(attributeSet);
  289. }
  290. }
  291. /**
  292. * @serial include
  293. */
  294. private static class SynchronizedPrintRequestAttributeSet
  295. extends SynchronizedAttributeSet
  296. implements PrintRequestAttributeSet, Serializable {
  297. public SynchronizedPrintRequestAttributeSet
  298. (PrintRequestAttributeSet attributeSet) {
  299. super(attributeSet);
  300. }
  301. }
  302. /**
  303. * @serial include
  304. */
  305. private static class SynchronizedPrintJobAttributeSet
  306. extends SynchronizedAttributeSet
  307. implements PrintJobAttributeSet, Serializable {
  308. public SynchronizedPrintJobAttributeSet
  309. (PrintJobAttributeSet attributeSet) {
  310. super(attributeSet);
  311. }
  312. }
  313. /**
  314. * @serial include
  315. */
  316. private static class SynchronizedPrintServiceAttributeSet
  317. extends SynchronizedAttributeSet
  318. implements PrintServiceAttributeSet, Serializable {
  319. public SynchronizedPrintServiceAttributeSet
  320. (PrintServiceAttributeSet attributeSet) {
  321. super(attributeSet);
  322. }
  323. }
  324. /**
  325. * Creates a synchronized view of the given attribute set.
  326. *
  327. * @param attributeSet Underlying attribute set.
  328. *
  329. * @return Synchronized view of <CODE>attributeSet</CODE>.
  330. *
  331. * @exception NullPointerException
  332. * Thrown if <CODE>attributeSet</CODE> is null.
  333. */
  334. public static AttributeSet synchronizedView
  335. (AttributeSet attributeSet) {
  336. if (attributeSet == null) {
  337. throw new NullPointerException();
  338. }
  339. return new SynchronizedAttributeSet(attributeSet);
  340. }
  341. /**
  342. * Creates a synchronized view of the given doc attribute set.
  343. *
  344. * @param attributeSet Underlying doc attribute set.
  345. *
  346. * @return Synchronized view of <CODE>attributeSet</CODE>.
  347. *
  348. * @exception NullPointerException
  349. * Thrown if <CODE>attributeSet</CODE> is null.
  350. */
  351. public static DocAttributeSet
  352. synchronizedView(DocAttributeSet attributeSet) {
  353. if (attributeSet == null) {
  354. throw new NullPointerException();
  355. }
  356. return new SynchronizedDocAttributeSet(attributeSet);
  357. }
  358. /**
  359. * Creates a synchronized view of the given print request attribute set.
  360. *
  361. * @param attributeSet Underlying print request attribute set.
  362. *
  363. * @return Synchronized view of <CODE>attributeSet</CODE>.
  364. *
  365. * @exception NullPointerException
  366. * Thrown if <CODE>attributeSet</CODE> is null.
  367. */
  368. public static PrintRequestAttributeSet
  369. synchronizedView(PrintRequestAttributeSet attributeSet) {
  370. if (attributeSet == null) {
  371. throw new NullPointerException();
  372. }
  373. return new SynchronizedPrintRequestAttributeSet(attributeSet);
  374. }
  375. /**
  376. * Creates a synchronized view of the given print job attribute set.
  377. *
  378. * @param attributeSet Underlying print job attribute set.
  379. *
  380. * @return Synchronized view of <CODE>attributeSet</CODE>.
  381. *
  382. * @exception NullPointerException
  383. * Thrown if <CODE>attributeSet</CODE> is null.
  384. */
  385. public static PrintJobAttributeSet
  386. synchronizedView(PrintJobAttributeSet attributeSet) {
  387. if (attributeSet == null) {
  388. throw new NullPointerException();
  389. }
  390. return new SynchronizedPrintJobAttributeSet(attributeSet);
  391. }
  392. /**
  393. * Creates a synchronized view of the given print service attribute set.
  394. *
  395. * @param attributeSet Underlying print service attribute set.
  396. *
  397. * @return Synchronized view of <CODE>attributeSet</CODE>.
  398. */
  399. public static PrintServiceAttributeSet
  400. synchronizedView(PrintServiceAttributeSet attributeSet) {
  401. if (attributeSet == null) {
  402. throw new NullPointerException();
  403. }
  404. return new SynchronizedPrintServiceAttributeSet(attributeSet);
  405. }
  406. /**
  407. * Verify that the given object is a {@link java.lang.Class Class} that
  408. * implements the given interface, which is assumed to be interface {@link
  409. * Attribute Attribute} or a subinterface thereof.
  410. *
  411. * @param object Object to test.
  412. * @param interfaceName Interface the object must implement.
  413. *
  414. * @return If <CODE>object</CODE> is a {@link java.lang.Class Class}
  415. * that implements <CODE>interfaceName</CODE>,
  416. * <CODE>object</CODE> is returned downcast to type {@link
  417. * java.lang.Class Class}; otherwise an exception is thrown.
  418. *
  419. * @exception NullPointerException
  420. * (unchecked exception) Thrown if <CODE>object</CODE> is null.
  421. * @exception ClassCastException
  422. * (unchecked exception) Thrown if <CODE>object</CODE> is not a
  423. * {@link java.lang.Class Class} that implements
  424. * <CODE>interfaceName</CODE>.
  425. */
  426. public static Class<?>
  427. verifyAttributeCategory(Object object, Class<?> interfaceName) {
  428. Class result = (Class) object;
  429. if (interfaceName.isAssignableFrom (result)) {
  430. return result;
  431. }
  432. else {
  433. throw new ClassCastException();
  434. }
  435. }
  436. /**
  437. * Verify that the given object is an instance of the given interface, which
  438. * is assumed to be interface {@link Attribute Attribute} or a subinterface
  439. * thereof.
  440. *
  441. * @param object Object to test.
  442. * @param interfaceName Interface of which the object must be an instance.
  443. *
  444. * @return If <CODE>object</CODE> is an instance of
  445. * <CODE>interfaceName</CODE>, <CODE>object</CODE> is returned
  446. * downcast to type {@link Attribute Attribute}; otherwise an
  447. * exception is thrown.
  448. *
  449. * @exception NullPointerException
  450. * (unchecked exception) Thrown if <CODE>object</CODE> is null.
  451. * @exception ClassCastException
  452. * (unchecked exception) Thrown if <CODE>object</CODE> is not an
  453. * instance of <CODE>interfaceName</CODE>.
  454. */
  455. public static Attribute
  456. verifyAttributeValue(Object object, Class<?> interfaceName) {
  457. if (object == null) {
  458. throw new NullPointerException();
  459. }
  460. else if (interfaceName.isInstance (object)) {
  461. return (Attribute) object;
  462. } else {
  463. throw new ClassCastException();
  464. }
  465. }
  466. /**
  467. * Verify that the given attribute category object is equal to the
  468. * category of the given attribute value object. If so, this method
  469. * returns doing nothing. If not, this method throws an exception.
  470. *
  471. * @param category Attribute category to test.
  472. * @param attribute Attribute value to test.
  473. *
  474. * @exception NullPointerException
  475. * (unchecked exception) Thrown if the <CODE>category</CODE> is
  476. * null or if the <CODE>attribute</CODE> is null.
  477. * @exception IllegalArgumentException
  478. * (unchecked exception) Thrown if the <CODE>category</CODE> is not
  479. * equal to the category of the <CODE>attribute</CODE>.
  480. */
  481. public static void
  482. verifyCategoryForValue(Class<?> category, Attribute attribute) {
  483. if (!category.equals (attribute.getCategory())) {
  484. throw new IllegalArgumentException();
  485. }
  486. }
  487. }