CompositeDataSupport.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* CompositeData.java -- A composite data structure implementation.
  2. Copyright (C) 2006, 2007 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.management.openmbean;
  32. import java.io.Serializable;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.Iterator;
  36. import java.util.Map;
  37. import java.util.Set;
  38. import java.util.SortedMap;
  39. import java.util.TreeMap;
  40. /**
  41. * Provides an implementation of the {@link CompositeData}
  42. * interface.
  43. *
  44. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  45. * @since 1.5
  46. */
  47. public class CompositeDataSupport
  48. implements CompositeData, Serializable
  49. {
  50. /**
  51. * Compatible with JDK 1.5
  52. */
  53. private static final long serialVersionUID = 8003518976613702244L;
  54. /**
  55. * Mapping of field names to values.
  56. *
  57. * @serial the map of field names to values.
  58. */
  59. private SortedMap<String, Object> contents;
  60. /**
  61. * The composite type which represents this composite data instance.
  62. *
  63. * @serial the type information for this instance.
  64. */
  65. private CompositeType compositeType;
  66. /**
  67. * Constructs a new {@link CompositeDataSupport} instance with the
  68. * specified type using field names and values from the supplied map.
  69. * The keys of the map become the field names, while the values
  70. * become the values of each respective field. This constructor simply
  71. * calls the other constructor, with the two arrays formed using the
  72. * keys and values of this map, respectively. Thus, the input parameters
  73. * given should conform to the same requirements given there (i.e. no
  74. * null values or empty strings).
  75. *
  76. * @param type the composite type of this composite data structure.
  77. * @param items a mapping of field names to values. This should match
  78. * the mappings given by the type (i.e. for each mapping
  79. * in the type, there should be a corresponding field name
  80. * with a value of the correct type).
  81. * @throws IllegalArgumentException if the type, the map or any of the keys
  82. * or values in the map are <code>null</code>,
  83. * or if any key from the map is an empty
  84. * string.
  85. * @throws OpenDataException if a mismatch occurs between the map and the
  86. * field name/type specification given by the
  87. * {@link CompositeType} instance. This may be
  88. * due to the two having a different size, a
  89. * mismatch between keys or an incorrectly typed
  90. * value.
  91. * @throws ArrayStoreException if one of the keys is not a
  92. * {@link java.lang.String} (thus calling a failure
  93. * in converting the keys to an array of strings).
  94. */
  95. public CompositeDataSupport(CompositeType type, Map<String, ?> items)
  96. throws OpenDataException
  97. {
  98. this(type,
  99. items.keySet().toArray(new String[items.size()]),
  100. items.values().toArray());
  101. }
  102. /**
  103. * Constructs a new {@link CompositeDataSupport} instance with the
  104. * specified type using the supplied arrays of field names and
  105. * values. Neither the type, the two arrays or any elements of the
  106. * arrays may be <code>null</code>. The {@link java.lang.String}s
  107. * within the <code>names</code> array must be non-empty. The
  108. * arrays must match in size and order, as each element of the
  109. * <code>names</code> array is matched against the corresponding
  110. * value in the <code>values</code> array. Internally, the two are
  111. * stored in a map, lexographically ordered using the field names.
  112. * The data given should also conform to the description of the
  113. * instance given by the {@link CompositeType} instance supplied.
  114. *
  115. * @param type the composite type of this composite data structure.
  116. * @param names the field names.
  117. * @param values the corresponding values of the fields.
  118. * @throws IllegalArgumentException if the type, the arrays or any of the keys
  119. * or values in the arrays are <code>null</code>,
  120. * or if any key from <code>names</code> is
  121. * an empty string. This also occurs if the
  122. * arrays differ in length.
  123. * @throws OpenDataException if a mismatch occurs between the arrays and the
  124. * field name/type specification given by the
  125. * {@link CompositeType} instance. This may be
  126. * due to a differing number of field names, a
  127. * mismatch between names or an incorrectly typed
  128. * value.
  129. */
  130. public CompositeDataSupport(CompositeType type, String[] names, Object[] values)
  131. throws OpenDataException
  132. {
  133. if (type == null)
  134. throw new IllegalArgumentException("The given composite type is null.");
  135. compositeType = type;
  136. if (names == null)
  137. throw new IllegalArgumentException("The names array is null.");
  138. if (values == null)
  139. throw new IllegalArgumentException("The values array is null.");
  140. if (names.length != values.length)
  141. throw new IllegalArgumentException("The sizes of the arrays differ.");
  142. Set<String> typeKeys = type.keySet();
  143. if (typeKeys.size() != names.length)
  144. throw new OpenDataException("The number of field names does not match " +
  145. "the type description.");
  146. contents = new TreeMap<String, Object>();
  147. for (int a = 0; a < names.length; ++a)
  148. {
  149. if (names[a] == null)
  150. throw new IllegalArgumentException("Element " + a + " of the names " +
  151. "array is null.");
  152. if (names[a].length() == 0)
  153. throw new IllegalArgumentException("Element " + a + " of the names " +
  154. "array is an empty string.");
  155. if (values[a] == null)
  156. throw new IllegalArgumentException("Element " + a + " of the values " +
  157. "array is null.");
  158. if (!(typeKeys.contains(names[a])))
  159. throw new OpenDataException("The name, " + names[a] + ", is not a " +
  160. "field in the given type description.");
  161. if (!(type.getType(names[a]).isValue(values[a])))
  162. throw new OpenDataException("The value, " + values[a] + ", is not a " +
  163. "valid value for the " + names[a] + " field.");
  164. contents.put(names[a], values[a]);
  165. }
  166. }
  167. /**
  168. * Returns true if this {@link CompositeData} instance contains
  169. * the specified key. This method always returns false for
  170. * an input key equal to <code>null</code> or the empty string.
  171. *
  172. * @param key the key to find in the structure.
  173. * @return true if the key exists.
  174. */
  175. public boolean containsKey(String key)
  176. {
  177. if (key == null || key.length() == 0)
  178. return false;
  179. else
  180. return contents.containsKey(key);
  181. }
  182. /**
  183. * Returns true if this {@link CompositeData} instance has
  184. * a value equal to that supplied.
  185. *
  186. * @param value the value to look for.
  187. * @return true if the value exists.
  188. */
  189. public boolean containsValue(Object value)
  190. {
  191. return contents.containsValue(value);
  192. }
  193. /**
  194. * Compares the specified object with this object for equality.
  195. * The object is judged equivalent if it is non-null, and also
  196. * an instance of {@link CompositeData} with the same name-value
  197. * mappings and types. The two compared instances may be
  198. * equivalent even if they represent different implementations of
  199. * {@link CompositeData}.
  200. *
  201. * @param obj the object to compare for equality.
  202. * @return true if <code>obj</code> is equal to <code>this</code>.
  203. */
  204. public boolean equals(Object obj)
  205. {
  206. if (!(obj instanceof CompositeData))
  207. return false;
  208. CompositeData data = (CompositeData) obj;
  209. if (!(data.getCompositeType().equals(compositeType)))
  210. return false;
  211. for (String key : contents.keySet())
  212. {
  213. if (!(data.containsKey(key)))
  214. return false;
  215. if (!(data.get(key).equals(contents.get(key))))
  216. return false;
  217. }
  218. return true;
  219. }
  220. /**
  221. * Retrieves the value for the specified key.
  222. *
  223. * @param key the key whose value should be returned.
  224. * @return the matching value.
  225. * @throws IllegalArgumentException if the key is <code>null</code>
  226. * or the empty string.
  227. * @throws InvalidKeyException if the key does not exist.
  228. */
  229. public Object get(String key)
  230. {
  231. if (key == null)
  232. throw new IllegalArgumentException("The supplied key is null.");
  233. if (key.length() == 0)
  234. throw new IllegalArgumentException("The supplied key is the empty string.");
  235. if (!(contents.containsKey(key)))
  236. throw new InvalidKeyException("The supplied key does not exist.");
  237. return contents.get(key);
  238. }
  239. /**
  240. * Returns the appropriate value for each key in the given array,
  241. * using the same ordering.
  242. *
  243. * @param keys the keys whose values should be returned.
  244. * @return the matching values.
  245. * @throws IllegalArgumentException if one of the keys is
  246. * <code>null</code> or the
  247. * empty string.
  248. * @throws InvalidKeyException if one of the keys does not exist.
  249. */
  250. public Object[] getAll(String[] keys)
  251. {
  252. Object[] values = new Object[keys.length];
  253. for (int a = 0; a < keys.length; ++a)
  254. values[a] = get(keys[a]);
  255. return values;
  256. }
  257. /**
  258. * Returns the composite type which corresponds to this instance
  259. * of {@link CompositeData}.
  260. *
  261. * @return the composite type for this instance.
  262. */
  263. public CompositeType getCompositeType()
  264. {
  265. return compositeType;
  266. }
  267. /**
  268. * Returns the hash code of this instance. The hash code is
  269. * computed as the sum of the hash codes of all the values plus
  270. * the hash code of the composite type. As equality comparisons
  271. * take place using this same information, this should ensure that
  272. * the property, <code>e1.equals(e2)</code> implies
  273. * <code>e1.hashCode() == e2.hashCode(), holds for any pair
  274. * of instances, <code>e1</code> and <code>e2</code>. However,
  275. * this relies on the other instance implementing the
  276. * <code>hashCode</code> method correctly, if it is not an
  277. * instance of {@link CompositeDataSupport}.
  278. *
  279. * @return the hash code of this {@link CompositeData}.
  280. * @see Object#equals(Object)
  281. */
  282. public int hashCode()
  283. {
  284. int code = compositeType.hashCode();
  285. for (Object o : contents.values())
  286. code += o.hashCode();
  287. return code;
  288. }
  289. /**
  290. * Returns a textual representation of this instance. The
  291. * exact format is left up to the implementation, but it
  292. * should contain the name of the implementing class,
  293. * the name of the type and a mapping of the form
  294. * <code>key=value</code> for each pair of key and value.
  295. *
  296. * @return a {@link java.lang.String} representation of the
  297. * object.
  298. */
  299. public String toString()
  300. {
  301. return getClass().getName() +
  302. "[compositeType=" + compositeType +
  303. ",contents=" + contents +
  304. "]";
  305. }
  306. /**
  307. * Returns a read-only collection of the values associated with
  308. * this instance. The values are sorted using the lexicographic
  309. * ordering of the corresponding keys.
  310. *
  311. * @return the values of this instance.
  312. */
  313. public Collection<?> values()
  314. {
  315. return Collections.unmodifiableCollection(contents.values());
  316. }
  317. }