1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -----------------
  25. * KeyedObjects.java
  26. * -----------------
  27. * (C) Copyright 2003-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: KeyedObjects.java,v 1.3 2005/01/12 14:53:39 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 31-Oct-2002 : Version 1 (DG);
  37. * 11-Jan-2005 : Minor tidy up (DG);
  38. *
  39. */
  40. package org.jfree.data;
  41. import java.io.Serializable;
  42. import java.util.Iterator;
  43. import java.util.List;
  44. import org.jfree.util.PublicCloneable;
  45. /**
  46. * A collection of (key, object) pairs.
  47. */
  48. public class KeyedObjects implements Cloneable, PublicCloneable, Serializable {
  49. /** Storage for the data. */
  50. private List data;
  51. /**
  52. * Creates a new collection (initially empty).
  53. */
  54. public KeyedObjects() {
  55. this.data = new java.util.ArrayList();
  56. }
  57. /**
  58. * Returns the number of items (values) in the collection.
  59. *
  60. * @return The item count.
  61. */
  62. public int getItemCount() {
  63. return this.data.size();
  64. }
  65. /**
  66. * Returns an object.
  67. *
  68. * @param item the item index (zero-based).
  69. *
  70. * @return The object (<code>null</code> if the index is out of range).
  71. */
  72. public Object getObject(int item) {
  73. Object result = null;
  74. if (item >= 0 && item < this.data.size()) {
  75. KeyedObject kobj = (KeyedObject) this.data.get(item);
  76. if (kobj != null) {
  77. result = kobj.getObject();
  78. }
  79. }
  80. return result;
  81. }
  82. /**
  83. * Returns a key.
  84. *
  85. * @param index the item index (zero-based).
  86. *
  87. * @return The row key.
  88. *
  89. * @throws IndexOutOfBoundsException if <code>index</code> is out of bounds.
  90. */
  91. public Comparable getKey(int index) {
  92. Comparable result = null;
  93. if (index >= 0 && index < this.data.size()) {
  94. KeyedObject item = (KeyedObject) this.data.get(index);
  95. if (item != null) {
  96. result = item.getKey();
  97. }
  98. }
  99. return result;
  100. }
  101. /**
  102. * Returns the index for a given key.
  103. *
  104. * @param key the key.
  105. *
  106. * @return The index, or <code>-1</code> if the key is unrecognised.
  107. */
  108. public int getIndex(Comparable key) {
  109. int result = -1;
  110. int i = 0;
  111. Iterator iterator = this.data.iterator();
  112. while (iterator.hasNext()) {
  113. KeyedObject ko = (KeyedObject) iterator.next();
  114. if (ko.getKey().equals(key)) {
  115. result = i;
  116. }
  117. i++;
  118. }
  119. return result;
  120. }
  121. /**
  122. * Returns the keys.
  123. *
  124. * @return The keys (never <code>null</code>).
  125. */
  126. public List getKeys() {
  127. List result = new java.util.ArrayList();
  128. Iterator iterator = this.data.iterator();
  129. while (iterator.hasNext()) {
  130. KeyedObject ko = (KeyedObject) iterator.next();
  131. result.add(ko.getKey());
  132. }
  133. return result;
  134. }
  135. /**
  136. * Returns the object for a given key. If the key is not recognised, the method should
  137. * return <code>null</code>.
  138. *
  139. * @param key the key.
  140. *
  141. * @return The object (possibly <code>null</code>).
  142. */
  143. public Object getObject(Comparable key) {
  144. return getObject(getIndex(key));
  145. }
  146. /**
  147. * Adds a new object to the collection, or overwrites an existing object.
  148. * This is the same as the {@link #setObject(Comparable, Object)} method.
  149. *
  150. * @param key the key.
  151. * @param object the object.
  152. */
  153. public void addObject(Comparable key, Object object) {
  154. setObject(key, object);
  155. }
  156. /**
  157. * Replaces an existing object, or adds a new object to the collection.
  158. * This is the same as the {@link #addObject(Comparable, Object)}
  159. * method.
  160. *
  161. * @param key the key.
  162. * @param object the object.
  163. */
  164. public void setObject(Comparable key, Object object) {
  165. int keyIndex = getIndex(key);
  166. if (keyIndex >= 0) {
  167. KeyedObject ko = (KeyedObject) this.data.get(keyIndex);
  168. ko.setObject(object);
  169. }
  170. else {
  171. KeyedObject ko = new KeyedObject(key, object);
  172. this.data.add(ko);
  173. }
  174. }
  175. /**
  176. * Removes a value from the collection.
  177. *
  178. * @param index the index of the item to remove.
  179. */
  180. public void removeValue(int index) {
  181. this.data.remove(index);
  182. }
  183. /**
  184. * Removes a value from the collection.
  185. *
  186. * @param key the key of the item to remove.
  187. */
  188. public void removeValue(Comparable key) {
  189. removeValue(getIndex(key));
  190. }
  191. /**
  192. * Returns a clone of this object.
  193. *
  194. * @return A clone.
  195. *
  196. * @throws CloneNotSupportedException if there is a problem cloning.
  197. */
  198. public Object clone() throws CloneNotSupportedException {
  199. KeyedObjects clone = (KeyedObjects) super.clone();
  200. clone.data = new java.util.ArrayList();
  201. Iterator iterator = this.data.iterator();
  202. while (iterator.hasNext()) {
  203. KeyedObject ko = (KeyedObject) iterator.next();
  204. clone.data.add(ko.clone());
  205. }
  206. return clone;
  207. }
  208. /**
  209. * Tests if this object is equal to another.
  210. *
  211. * @param o the other object.
  212. *
  213. * @return A boolean.
  214. */
  215. public boolean equals(Object o) {
  216. if (o == null) {
  217. return false;
  218. }
  219. if (o == this) {
  220. return true;
  221. }
  222. if (!(o instanceof KeyedObjects)) {
  223. return false;
  224. }
  225. KeyedObjects kos = (KeyedObjects) o;
  226. int count = getItemCount();
  227. if (count != kos.getItemCount()) {
  228. return false;
  229. }
  230. for (int i = 0; i < count; i++) {
  231. Comparable k1 = getKey(i);
  232. Comparable k2 = kos.getKey(i);
  233. if (!k1.equals(k2)) {
  234. return false;
  235. }
  236. Object o1 = getObject(i);
  237. Object o2 = kos.getObject(i);
  238. if (o1 == null) {
  239. if (o2 != null) {
  240. return false;
  241. }
  242. }
  243. else {
  244. if (!o1.equals(o2)) {
  245. return false;
  246. }
  247. }
  248. }
  249. return true;
  250. }
  251. }