TabularDataSupport.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /* TabularDataSupport.java -- Tables of composite data structures.
  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.ArrayList;
  34. import java.util.Collection;
  35. import java.util.HashMap;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Set;
  40. /**
  41. * Provides an implementation of the {@link TabularData}
  42. * interface using a {@link java.util.HashMap}.
  43. *
  44. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  45. * @since 1.5
  46. */
  47. public class TabularDataSupport
  48. implements TabularData, Serializable, Cloneable, Map<Object,Object>
  49. {
  50. /**
  51. * Compatible with JDK 1.5
  52. */
  53. private static final long serialVersionUID = 5720150593236309827L;
  54. /**
  55. * Mapping of rows to column values.
  56. *
  57. * @serial the map of rows to column values.
  58. */
  59. private HashMap<Object,Object> dataMap;
  60. /**
  61. * The tabular type which represents this tabular data instance.
  62. *
  63. * @serial the type information for this instance.
  64. */
  65. private TabularType tabularType;
  66. /**
  67. * Constructs a new empty {@link TabularDataSupport} with the
  68. * specified type. The type may not be null. This constructor
  69. * simply calls the other, with the default initial capacity of
  70. * <code>101</code> and default load factor of <code>0.75</code>.
  71. *
  72. * @param type the tabular type of this tabular data instance.
  73. * @throws IllegalArgumentException if <code>type</code> is
  74. * <code>null</code>.
  75. */
  76. public TabularDataSupport(TabularType type)
  77. {
  78. this(type, 101, 0.75f);
  79. }
  80. /**
  81. * Constructs a new empty {@link TabularDataSupport} with the
  82. * specified type and the supplied initial capacity and load factor
  83. * being used for the underlying {@link java.util.HashMap}. The
  84. * type may not be null and the initial capacity and load factor
  85. * must be positive.
  86. *
  87. * @param type the tabular type of this tabular data instance.
  88. * @param cap the initial capacity of the underlying map.
  89. * @param lf the load factor of the underlying map.
  90. * @throws IllegalArgumentException if <code>type</code> is
  91. * <code>null</code>, or
  92. * <code>cap</code> or
  93. * <code>lf</code> are
  94. * negative.
  95. */
  96. public TabularDataSupport(TabularType type, int cap, float lf)
  97. {
  98. if (type == null)
  99. throw new IllegalArgumentException("The type may not be null.");
  100. tabularType = type;
  101. dataMap = new HashMap<Object,Object>(cap, lf);
  102. }
  103. /**
  104. * Calculates the index the specified {@link CompositeData} value
  105. * would have, if it was to be added to this {@link TabularData}
  106. * instance. This method includes a check that the type of the
  107. * given value is the same as the row type of this instance, but not
  108. * a check for existing instances of the given value. The value
  109. * must also not be <code>null</code>. Possible indices are
  110. * selected by the {@link TabularType#getIndexNames()} method of
  111. * this instance's tabular type. The returned indices are the
  112. * values of the fields in the supplied {@link CompositeData}
  113. * instance that match the names given in the {@link TabularType}.
  114. *
  115. * @param val the {@link CompositeData} value whose index should
  116. * be calculated.
  117. * @return the index the value would take on, if it were to be added.
  118. * @throws NullPointerException if the value is <code>null</code>.
  119. * @throws InvalidOpenTypeException if the value does not match the
  120. * row type of this instance.
  121. */
  122. public Object[] calculateIndex(CompositeData val)
  123. {
  124. if (!(val.getCompositeType().equals(tabularType.getRowType())))
  125. throw new InvalidOpenTypeException("The type of the given value " +
  126. "does not match the row type " +
  127. "of this instance.");
  128. List<String> indexNames = tabularType.getIndexNames();
  129. List<String> matchingIndicies = new ArrayList<String>(indexNames.size());
  130. for (String name : indexNames)
  131. matchingIndicies.add(val.get(name).toString());
  132. return matchingIndicies.toArray();
  133. }
  134. /**
  135. * Removes all {@link CompositeData} values from the table.
  136. */
  137. public void clear()
  138. {
  139. dataMap.clear();
  140. }
  141. /**
  142. * Returns a shallow clone of the information, as obtained by the
  143. * {@link Object} implementation of {@link Object#clone()}. The map
  144. * is also cloned, but it still references the same objects.
  145. *
  146. * @return a shallow clone of this {@link TabularDataSupport}.
  147. */
  148. @SuppressWarnings("unchecked")
  149. public Object clone()
  150. {
  151. TabularDataSupport clone = null;
  152. try
  153. {
  154. clone = (TabularDataSupport) super.clone();
  155. clone.setMap((HashMap<Object,Object>) dataMap.clone());
  156. }
  157. catch (CloneNotSupportedException e)
  158. {
  159. /* This won't happen as we implement Cloneable */
  160. }
  161. return clone;
  162. }
  163. /**
  164. * Returns true iff this instance of the {@link TabularData} class
  165. * contains a {@link CompositeData} value at the specified index.
  166. * The method returns <code>false</code> if the given key can
  167. * not be cast to an {@link java.lang.Object} array; otherwise
  168. * it returns the result of {@link #containsKey(java.lang.Object[])}.
  169. *
  170. *
  171. * @param key the key to test for.
  172. * @return true if the key maps to a {@link CompositeData} value.
  173. */
  174. public boolean containsKey(Object key)
  175. {
  176. if (key instanceof Object[])
  177. return containsKey((Object[]) key);
  178. else
  179. return false;
  180. }
  181. /**
  182. * Returns true iff this instance of the {@link TabularData} class
  183. * contains a {@link CompositeData} value at the specified index.
  184. * In any other circumstance, including if the given key
  185. * is <code>null</code> or of the incorrect type, according to
  186. * the {@link TabularType} of this instance, this method returns
  187. * false.
  188. *
  189. * @param key the key to test for.
  190. * @return true if the key maps to a {@link CompositeData} value.
  191. */
  192. public boolean containsKey(Object[] key)
  193. {
  194. if (key == null)
  195. return false;
  196. if (!(isKeyValid(key)))
  197. return false;
  198. return dataMap.containsKey(key);
  199. }
  200. /**
  201. * Returns true iff this instance of the {@link TabularData} class
  202. * contains the specified {@link CompositeData} value. If the given
  203. * value is not an instance of {@link CompositeData}, this method
  204. * simply returns false.
  205. *
  206. * @param val the value to test for.
  207. * @return true if the value exists.
  208. */
  209. public boolean containsValue(Object val)
  210. {
  211. if (val instanceof CompositeData)
  212. return containsValue((CompositeData) val);
  213. else
  214. return false;
  215. }
  216. /**
  217. * Returns true iff this instance of the {@link TabularData} class
  218. * contains the specified {@link CompositeData} value.
  219. * In any other circumstance, including if the given value
  220. * is <code>null</code> or of the incorrect type, according to
  221. * the {@link TabularType} of this instance, this method returns
  222. * false.
  223. *
  224. * @param val the value to test for.
  225. * @return true if the value exists.
  226. */
  227. public boolean containsValue(CompositeData val)
  228. {
  229. if (val == null)
  230. return false;
  231. if (!(val.getCompositeType().equals(tabularType.getRowType())))
  232. return false;
  233. return dataMap.containsValue(val);
  234. }
  235. /**
  236. * <p>
  237. * Returns a set view of the mappings in this Map. Each element in the
  238. * set is a Map.Entry. The set is backed by the map, so that changes in
  239. * one show up in the other. Modifications made while an iterator is
  240. * in progress cause undefined behavior. If the set supports removal,
  241. * these methods remove the underlying mapping from the map:
  242. * <code>Iterator.remove</code>, <code>Set.remove</code>,
  243. * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
  244. * Element addition, via <code>add</code> or <code>addAll</code>, is
  245. * not supported via this set.
  246. * </p>
  247. * <p>
  248. * <strong>Note</strong>: using the
  249. * {@link java.util.Map.Entry#setValue(Object) will cause corruption of
  250. * the index to row mappings.
  251. * </p>
  252. *
  253. * @return the set view of all mapping entries
  254. * @see java.util.Map.Entry
  255. */
  256. public Set<Map.Entry<Object,Object>> entrySet()
  257. {
  258. return dataMap.entrySet();
  259. }
  260. /**
  261. * Compares the specified object with this object for equality.
  262. * The object is judged equivalent if it is non-null, and also
  263. * an instance of {@link TabularData} with the same row type,
  264. * and {@link CompositeData} values. The two compared instances may
  265. * be equivalent even if they represent different implementations
  266. * of {@link TabularData}.
  267. *
  268. * @param obj the object to compare for equality.
  269. * @return true if <code>obj</code> is equal to <code>this</code>.
  270. */
  271. public boolean equals(Object obj)
  272. {
  273. if (!(obj instanceof TabularData))
  274. return false;
  275. TabularData data = (TabularData) obj;
  276. return tabularType.equals(data.getTabularType()) &&
  277. dataMap.values().equals(data.values());
  278. }
  279. /**
  280. * Retrieves the value for the specified key by simply
  281. * calling <code>get((Object[]) key)</code>.
  282. *
  283. * @param key the key whose value should be returned.
  284. * @return the matching {@link CompositeData} value, or
  285. * <code>null</code> if one does not exist.
  286. * @throws NullPointerException if the key is <code>null</code>.
  287. * @throws ClassCastException if the key is not an instance
  288. * of <code>Object[]</code>.
  289. * @throws InvalidKeyException if the key does not match
  290. * the {@link TabularType} of this
  291. * instance.
  292. */
  293. public Object get(Object key)
  294. {
  295. return get((Object[]) key);
  296. }
  297. /**
  298. * Retrieves the {@link CompositeData} value for the specified
  299. * key, or <code>null</code> if no such mapping exists.
  300. *
  301. * @param key the key whose value should be returned.
  302. * @return the matching {@link CompositeData} value, or
  303. * <code>null</code> if one does not exist.
  304. * @throws NullPointerException if the key is <code>null</code>.
  305. * @throws InvalidKeyException if the key does not match
  306. * the {@link TabularType} of this
  307. * instance.
  308. */
  309. public CompositeData get(Object[] key)
  310. {
  311. if (!(isKeyValid(key)))
  312. throw new InvalidKeyException("The key does not match the " +
  313. "tabular type of this instance.");
  314. return (CompositeData) dataMap.get(key);
  315. }
  316. /**
  317. * Returns the tabular type which corresponds to this instance
  318. * of {@link TabularData}.
  319. *
  320. * @return the tabular type for this instance.
  321. */
  322. public TabularType getTabularType()
  323. {
  324. return tabularType;
  325. }
  326. /**
  327. * Returns the hash code of the composite data type. This is
  328. * computed as the sum of the hash codes of each value, together
  329. * with the hash code of the tabular type. These are the same
  330. * elements of the type that are compared as part of the {@link
  331. * #equals(java.lang.Object)} method, thus ensuring that the
  332. * hashcode is compatible with the equality test.
  333. *
  334. * @return the hash code of this instance.
  335. */
  336. public int hashCode()
  337. {
  338. return tabularType.hashCode() + dataMap.values().hashCode();
  339. }
  340. /**
  341. * Returns true if this {@link TabularData} instance
  342. * contains no {@link CompositeData} values.
  343. *
  344. * @return true if the instance is devoid of rows.
  345. */
  346. public boolean isEmpty()
  347. {
  348. return dataMap.isEmpty();
  349. }
  350. /**
  351. * Returns true if the given key is valid for the
  352. * @link{TabularType} of this instance.
  353. *
  354. * @return true if the key is valid.
  355. * @throws NullPointerException if <code>key</code>
  356. * is null.
  357. */
  358. private boolean isKeyValid(Object[] key)
  359. {
  360. Iterator<String> it = tabularType.getIndexNames().iterator();
  361. CompositeType rowType = tabularType.getRowType();
  362. for (int a = 0; it.hasNext(); ++a)
  363. {
  364. OpenType<?> type = rowType.getType(it.next());
  365. if (!(type.isValue(key[a])))
  366. return false;
  367. }
  368. return true;
  369. }
  370. /**
  371. * Returns a set view of the keys in this Map. The set is backed by the
  372. * map, so that changes in one show up in the other. Modifications made
  373. * while an iterator is in progress cause undefined behavior. If the set
  374. * supports removal, these methods remove the underlying mapping from
  375. * the map: <code>Iterator.remove</code>, <code>Set.remove</code>,
  376. * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.
  377. * Element addition, via <code>add</code> or <code>addAll</code>, is
  378. * not supported via this set.
  379. *
  380. * @return the set view of all keys
  381. */
  382. public Set<Object> keySet()
  383. {
  384. return dataMap.keySet();
  385. }
  386. /**
  387. * Adds the specified {@link CompositeData} value to the
  388. * table. The value must be non-null, of the same type
  389. * as the row type of this instance, and must not have
  390. * the same index as an existing value. The index is
  391. * calculated using the index names of the
  392. * {@link TabularType} for this instance.
  393. *
  394. * @param val the {@link CompositeData} value to add.
  395. * @throws NullPointerException if <code>val</code> is
  396. * <code>null</code>.
  397. * @throws InvalidOpenTypeException if the type of the
  398. * given value does not
  399. * match the row type.
  400. * @throws KeyAlreadyExistsException if the value has the
  401. * same calculated index
  402. * as an existing value.
  403. */
  404. public void put(CompositeData val)
  405. {
  406. Object[] key = calculateIndex(val);
  407. if (dataMap.containsKey(key))
  408. throw new KeyAlreadyExistsException("A value with this index " +
  409. "already exists.");
  410. dataMap.put(key, val);
  411. }
  412. /**
  413. * Adds the specified {@link CompositeData} value to the
  414. * table, ignoring the supplied key, by simply calling
  415. * <code>put((CompositeData) val)</code>.
  416. *
  417. * @param key ignored.
  418. * @param val the {@link CompositeData} value to add.
  419. * @return the {@link CompositeData} value.
  420. * @throws NullPointerException if <code>val</code> is
  421. * <code>null</code>.
  422. * @throws InvalidOpenTypeException if the type of the
  423. * given value does not
  424. * match the row type.
  425. * @throws KeyAlreadyExistsException if the value has the
  426. * same calculated index
  427. * as an existing value.
  428. */
  429. public Object put(Object key, Object val)
  430. {
  431. put((CompositeData) val);
  432. return val;
  433. }
  434. /**
  435. * Adds each of the specified {@link CompositeData} values
  436. * to the table. Each element of the array must meet the
  437. * conditions given for the {@link #put(CompositeData)}
  438. * method. In addition, the index of each value in the
  439. * array must be distinct from the index of the other
  440. * values in the array, as well as from the existing values
  441. * in the table. The operation should be atomic; if one
  442. * value can not be added, then none of the values should
  443. * be. If the array is <code>null</code> or empty, the
  444. * method simply returns.
  445. *
  446. * @param vals the {@link CompositeData} values to add.
  447. * @throws NullPointerException if a value from the array is
  448. * <code>null</code>.
  449. * @throws InvalidOpenTypeException if the type of a
  450. * given value does not
  451. * match the row type.
  452. * @throws KeyAlreadyExistsException if a value has the
  453. * same calculated index
  454. * as an existing value or
  455. * of one of the other
  456. * specified values.
  457. */
  458. public void putAll(CompositeData[] vals)
  459. {
  460. if (vals == null || vals.length == 0)
  461. return;
  462. Map<Object,Object> mapToAdd = new HashMap<Object,Object>(vals.length);
  463. for (int a = 0; a < vals.length; ++a)
  464. {
  465. Object[] key = calculateIndex(vals[a]);
  466. if (dataMap.containsKey(key))
  467. throw new KeyAlreadyExistsException("Element " + a + ": A " +
  468. "value with this index " +
  469. "already exists.");
  470. mapToAdd.put(key, vals[a]);
  471. }
  472. dataMap.putAll(mapToAdd);
  473. }
  474. /**
  475. * Converts each value from the specified map to a member of an
  476. * array of {@link CompositeData} values and adds them using {@link
  477. * #put(CompositeData[])}, if possible. As in {@link
  478. * #put(Object,Object)}, the keys are simply ignored. This method
  479. * is useful for adding the {@link CompositeData} values from a
  480. * different {@link TabularData} instance, which uses the same
  481. * {@link TabularType} but a different selection of index names, to
  482. * this one. If the map is <code>null</code> or empty, the method
  483. * simply returns.
  484. *
  485. * @param m the map to add. Only the values are used and must
  486. * all be instances of {@link CompositeData}.
  487. * @throws NullPointerException if a value from the map is
  488. * <code>null</code>.
  489. * @throws ClassCastException if a value from the map is not
  490. * an instance of {@link CompositeData}.
  491. * @throws InvalidOpenTypeException if the type of the
  492. * given value does not
  493. * match the row type.
  494. * @throws KeyAlreadyExistsException if the value has the
  495. * same calculated index
  496. * as an existing value or
  497. * of one of the other
  498. * specified values.
  499. */
  500. public void putAll(Map<?,?> m)
  501. {
  502. if (m == null || m.size() == 0)
  503. return;
  504. Collection<?> vals = m.values();
  505. CompositeData[] data = new CompositeData[vals.size()];
  506. Iterator<?> it = vals.iterator();
  507. for (int a = 0; it.hasNext(); ++a)
  508. {
  509. data[a] = (CompositeData) it.next();
  510. }
  511. putAll(data);
  512. }
  513. /**
  514. * Removes the value for the specified key by simply
  515. * calling <code>remove((Object[]) key)</code>.
  516. *
  517. * @param key the key whose value should be removed.
  518. * @return the removed value, or <code>null</code> if
  519. * there is no value for the given key.
  520. * @throws NullPointerException if the key is <code>null</code>.
  521. * @throws ClassCastException if the key is not an instance
  522. * of <code>Object[]</code>.
  523. * @throws InvalidOpenTypeException if the key does not match
  524. * the {@link TabularType} of this
  525. * instance.
  526. */
  527. public Object remove(Object key)
  528. {
  529. return remove((Object[]) key);
  530. }
  531. /**
  532. * Removes the {@link CompositeData} value located at the
  533. * specified index. <code>null</code> is returned if the
  534. * value does not exist. Otherwise, the removed value is
  535. * returned.
  536. *
  537. * @param key the key of the value to remove.
  538. * @return the removed value, or <code>null</code> if
  539. * there is no value for the given key.
  540. * @throws NullPointerException if the key is <code>null</code>.
  541. * @throws InvalidOpenTypeException if the key does not match
  542. * the {@link TabularType} of this
  543. * instance.
  544. */
  545. public CompositeData remove(Object[] key)
  546. {
  547. if (!(isKeyValid(key)))
  548. throw new InvalidKeyException("The key does not match the " +
  549. "tabular type of this instance.");
  550. return (CompositeData) dataMap.remove(key);
  551. }
  552. /**
  553. * Private method to set the internal {@link java.util.Map}
  554. * instance (used in cloning).
  555. *
  556. * @param map the new map used.
  557. */
  558. private void setMap(HashMap<Object,Object> map)
  559. {
  560. dataMap = map;
  561. }
  562. /**
  563. * Returns the number of {@link CompositeData} values or rows
  564. * in the table.
  565. *
  566. * @return the number of rows in the table.
  567. */
  568. public int size()
  569. {
  570. return dataMap.size();
  571. }
  572. /**
  573. * Returns a textual representation of this instance. This
  574. * is constructed using the class name
  575. * (<code>javax.management.openmbean.TabularDataSupport</code>)
  576. * and the result of calling <code>toString()</code> on the
  577. * tabular type and underlying hash map instance.
  578. *
  579. * @return a {@link java.lang.String} representation of the
  580. * object.
  581. */
  582. public String toString()
  583. {
  584. return getClass().getName()
  585. + "[tabularType=" + tabularType
  586. + ",dataMap=" + dataMap
  587. + "]";
  588. }
  589. /**
  590. * Returns a collection (or bag) view of the values in this Map. The
  591. * collection is backed by the map, so that changes in one show up in
  592. * the other. Modifications made while an iterator is in progress cause
  593. * undefined behavior. If the collection supports removal, these methods
  594. * remove the underlying mapping from the map: <code>Iterator.remove</code>,
  595. * <code>Collection.remove</code>, <code>removeAll</code>,
  596. * <code>retainAll</code>, and <code>clear</code>. Element addition, via
  597. * <code>add</code> or <code>addAll</code>, is not supported via this
  598. * collection.
  599. *
  600. * @return the collection view of all values
  601. */
  602. public Collection<Object> values()
  603. {
  604. return dataMap.values();
  605. }
  606. }