HashAttributeSet.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* HashAttributeSet.java --
  2. Copyright (C) 2003, 2004, 2005, 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.IOException;
  33. import java.io.ObjectInputStream;
  34. import java.io.ObjectOutputStream;
  35. import java.io.Serializable;
  36. import java.util.HashMap;
  37. import java.util.Iterator;
  38. /**
  39. * <code>HashAttributeSet</code> provides an implementation of
  40. * {@link javax.print.attribute.AttributeSet}.
  41. */
  42. public class HashAttributeSet implements AttributeSet, Serializable
  43. {
  44. private static final long serialVersionUID = 5311560590283707917L;
  45. private Class myInterface;
  46. private transient HashMap attributeMap = new HashMap();
  47. /**
  48. * Creates an empty <code>HashAttributeSet</code> object.
  49. */
  50. public HashAttributeSet()
  51. {
  52. this(Attribute.class);
  53. }
  54. /**
  55. * Creates a <code>HashAttributeSet</code> object with the given
  56. * attribute in it.
  57. *
  58. * @param attribute the attribute to put into the set
  59. *
  60. * @exception NullPointerException if attribute is null
  61. */
  62. public HashAttributeSet(Attribute attribute)
  63. {
  64. this(attribute, Attribute.class);
  65. }
  66. /**
  67. * Creates a <code>HashAttributeSet</code> object with the given
  68. * attributes in it.
  69. *
  70. * @param attributes the array of attributes to put into the set. If
  71. * <code>null</code> an empty set is created.
  72. *
  73. * @exception NullPointerException if one of the attributes of the given
  74. * array is null.
  75. */
  76. public HashAttributeSet(Attribute[] attributes)
  77. {
  78. this(attributes, Attribute.class);
  79. }
  80. /**
  81. * Creates a <code>HashAttributeSet</code> object with attributes
  82. * of the given attributes set in it.
  83. *
  84. * @param attributes the attributes set to put into the set. If
  85. * <code>null</code> an empty set is created.
  86. */
  87. public HashAttributeSet(AttributeSet attributes)
  88. {
  89. this(attributes, Attribute.class);
  90. }
  91. /**
  92. * Creates an empty <code>HashAttributeSet</code> object.
  93. *
  94. * @param interfaceName the interface that all members must implement
  95. *
  96. * @exception NullPointerException if interfaceName is null
  97. */
  98. protected HashAttributeSet(Class<?> interfaceName)
  99. {
  100. if (interfaceName == null)
  101. throw new NullPointerException("interfaceName may not be null");
  102. myInterface = interfaceName;
  103. }
  104. /**
  105. * Creates a <code>HashAttributeSet</code> object with the given
  106. * attribute in it.
  107. *
  108. * @param attribute the attribute to put into the set.
  109. * @param interfaceName the interface that all members must implement.
  110. *
  111. * @exception ClassCastException if attribute is not an interface of
  112. * interfaceName
  113. * @exception NullPointerException if attribute or interfaceName is null
  114. */
  115. protected HashAttributeSet(Attribute attribute, Class<?> interfaceName)
  116. {
  117. this(interfaceName);
  118. if (attribute == null)
  119. throw new NullPointerException();
  120. addInternal(attribute, interfaceName);
  121. }
  122. /**
  123. * Creates a <code>HashAttributeSet</code> object with the given
  124. * attributes in it.
  125. *
  126. * @param attributes the array of attributes to put into the set. If
  127. * <code>null</code> an empty set is created.
  128. * @param interfaceName the interface that all members must implement.
  129. *
  130. * @exception ClassCastException if any element of attributes is not an
  131. * interface of interfaceName
  132. * @exception NullPointerException if attributes or interfaceName is null
  133. */
  134. protected HashAttributeSet(Attribute[] attributes, Class<?> interfaceName)
  135. {
  136. this(interfaceName);
  137. if (attributes != null)
  138. {
  139. for (int index = 0; index < attributes.length; index++)
  140. addInternal(attributes[index], interfaceName);
  141. }
  142. }
  143. /**
  144. * Creates a <code>HashAttributeSet</code> object with attributes
  145. * of the given attributes set in it.
  146. *
  147. * @param attributes the attributes set to put into the set. If
  148. * <code>null</code> an empty set is created.
  149. * @param interfaceName the interface that all members must implement.
  150. *
  151. * @exception ClassCastException if any element of attributes is not an
  152. * interface of interfaceName
  153. */
  154. protected HashAttributeSet(AttributeSet attributes, Class<?> interfaceName)
  155. {
  156. this(interfaceName);
  157. if (attributes != null)
  158. addAllInternal(attributes, interfaceName);
  159. }
  160. /**
  161. * Adds the specified attribute value to this attribute set
  162. * if it is not already present.
  163. *
  164. * This operation removes any existing attribute of the same category
  165. * before adding the given attribute to the set.
  166. *
  167. * @param attribute the attribute to add.
  168. * @return <code>true</code> if the set is changed, false otherwise.
  169. * @throws NullPointerException if the attribute is <code>null</code>.
  170. * @throws UnmodifiableSetException if the set does not support modification.
  171. */
  172. public boolean add(Attribute attribute)
  173. {
  174. return addInternal(attribute, myInterface);
  175. }
  176. private boolean addInternal(Attribute attribute, Class interfaceName)
  177. {
  178. if (attribute == null)
  179. throw new NullPointerException("attribute may not be null");
  180. AttributeSetUtilities.verifyAttributeCategory(interfaceName,
  181. myInterface);
  182. Object old = attributeMap.put
  183. (attribute.getCategory(), AttributeSetUtilities.verifyAttributeValue
  184. (attribute, interfaceName));
  185. return !attribute.equals(old);
  186. }
  187. /**
  188. * Adds all of the elements in the specified set to this attribute set.
  189. *
  190. * @param attributes the set of attributes to add.
  191. * @return <code>true</code> if the set is changed, false otherwise.
  192. * @throws UnmodifiableSetException if the set does not support modification.
  193. *
  194. * @see #add(Attribute)
  195. */
  196. public boolean addAll(AttributeSet attributes)
  197. {
  198. return addAllInternal(attributes, myInterface);
  199. }
  200. private boolean addAllInternal(AttributeSet attributes, Class interfaceName)
  201. {
  202. boolean modified = false;
  203. Attribute[] array = attributes.toArray();
  204. for (int index = 0; index < array.length; index++)
  205. if (addInternal(array[index], interfaceName))
  206. modified = true;
  207. return modified;
  208. }
  209. /**
  210. * Removes all attributes from this attribute set.
  211. *
  212. * @throws UnmodifiableSetException if the set does not support modification.
  213. */
  214. public void clear()
  215. {
  216. attributeMap.clear();
  217. }
  218. /**
  219. * Checks if this attributes set contains an attribute with the given
  220. * category.
  221. *
  222. * @param category the category to test for.
  223. * @return <code>true</code> if an attribute of the category is contained
  224. * in the set, <code>false</code> otherwise.
  225. */
  226. public boolean containsKey(Class<?> category)
  227. {
  228. return attributeMap.containsKey(category);
  229. }
  230. /**
  231. * Checks if this attribute set contains the given attribute.
  232. *
  233. * @param attribute the attribute to test for.
  234. * @return <code>true</code> if the attribute is contained in the set,
  235. * <code>false</code> otherwise.
  236. */
  237. public boolean containsValue(Attribute attribute)
  238. {
  239. return attributeMap.containsValue(attribute);
  240. }
  241. /**
  242. * Tests this set for equality with the given object. <code>true</code> is
  243. * returned, if the given object is also of type <code>AttributeSet</code>
  244. * and the contained attributes are the same as in this set.
  245. *
  246. * @param obj the Object to test.
  247. * @return <code>true</code> if equal, false otherwise.
  248. */
  249. public boolean equals(Object obj)
  250. {
  251. if (! (obj instanceof HashAttributeSet))
  252. return false;
  253. return attributeMap.equals(((HashAttributeSet) obj).attributeMap);
  254. }
  255. /**
  256. * Returns the attribute object contained in this set for the given attribute
  257. * category.
  258. *
  259. * @param category the category of the attribute. A <code>Class</code>
  260. * instance of a class implementing the <code>Attribute</code> interface.
  261. * @return The attribute for this category or <code>null</code> if no
  262. * attribute is contained for the given category.
  263. * @throws NullPointerException if category is null.
  264. * @throws ClassCastException if category is not implementing
  265. * <code>Attribute</code>.
  266. */
  267. public Attribute get(Class<?> category)
  268. {
  269. if (category == null)
  270. throw new NullPointerException("category may not be null");
  271. return (Attribute) attributeMap.get(category);
  272. }
  273. /**
  274. * Returns the hashcode value. The hashcode value is the sum of all hashcodes
  275. * of the attributes contained in this set.
  276. *
  277. * @return The hashcode for this attribute set.
  278. */
  279. public int hashCode()
  280. {
  281. int hashcode = 0;
  282. Iterator it = attributeMap.values().iterator();
  283. while (it.hasNext())
  284. hashcode = hashcode + it.next().hashCode();
  285. return hashcode;
  286. }
  287. /**
  288. * Checks if the attribute set is empty.
  289. *
  290. * @return <code>true</code> if the attribute set is empty, false otherwise.
  291. */
  292. public boolean isEmpty()
  293. {
  294. return attributeMap.isEmpty();
  295. }
  296. /**
  297. * Removes the given attribute from the set. If the given attribute is <code>null</code>
  298. * nothing is done and <code>false</code> is returned.
  299. *
  300. * @param attribute the attribute to remove.
  301. * @return <code>true</code> if removed, false in all other cases.
  302. * @throws UnmodifiableSetException if the set does not support modification.
  303. */
  304. public boolean remove(Attribute attribute)
  305. {
  306. if (attribute == null)
  307. return false;
  308. return attributeMap.remove(attribute.getCategory()) != null;
  309. }
  310. /**
  311. * Removes the attribute entry of the given category from the set. If the given
  312. * category is <code>null</code> nothing is done and <code>false</code> is returned.
  313. *
  314. * @param category the category of the entry to be removed.
  315. * @return <code>true</code> if an attribute is removed, false in all other cases.
  316. * @throws UnmodifiableSetException if the set does not support modification.
  317. */
  318. public boolean remove(Class<?> category)
  319. {
  320. if (category == null)
  321. return false;
  322. return attributeMap.remove(category) != null;
  323. }
  324. /**
  325. * Returns the number of elements in this attribute set.
  326. *
  327. * @return The number of elements.
  328. */
  329. public int size()
  330. {
  331. return attributeMap.size();
  332. }
  333. /**
  334. * Returns the content of the attribute set as an array
  335. *
  336. * @return An array of attributes.
  337. */
  338. public Attribute[] toArray()
  339. {
  340. int index = 0;
  341. Iterator it = attributeMap.values().iterator();
  342. Attribute[] array = new Attribute[size()];
  343. while (it.hasNext())
  344. {
  345. array[index] = (Attribute) it.next();
  346. index++;
  347. }
  348. return array;
  349. }
  350. // Implemented as specified in serialized form
  351. private void readObject(ObjectInputStream s)
  352. throws ClassNotFoundException, IOException
  353. {
  354. myInterface = (Class) s.readObject();
  355. int size = s.readInt();
  356. attributeMap = new HashMap(size);
  357. for (int i=0; i < size; i++)
  358. add((Attribute) s.readObject());
  359. }
  360. private void writeObject(ObjectOutputStream s) throws IOException
  361. {
  362. s.writeObject(myInterface);
  363. s.writeInt(size());
  364. Iterator it = attributeMap.values().iterator();
  365. while (it.hasNext())
  366. s.writeObject(it.next());
  367. }
  368. }