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. * StandardEntityCollection.java
  26. * -----------------------------
  27. * (C) Copyright 2001-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): -;
  31. *
  32. * $Id: StandardEntityCollection.java,v 1.4 2005/01/19 13:53:09 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 23-May-2002 : Version 1 (DG);
  37. * 26-Jun-2002 : Added iterator() method (DG);
  38. * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39. * 19-May-2004 : Implemented Serializable (DG);
  40. * 29-Sep-2004 : Renamed addEntity() --> add() and addEntities() --> addAll() (DG);
  41. * 19-Jan-2005 : Changed storage from Collection --> List (DG);
  42. *
  43. */
  44. package org.jfree.chart.entity;
  45. import java.io.Serializable;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.Iterator;
  49. import java.util.List;
  50. import org.jfree.util.ObjectUtilities;
  51. /**
  52. * A standard implementation of the {@link EntityCollection} interface.
  53. */
  54. public class StandardEntityCollection implements EntityCollection, Cloneable, Serializable {
  55. /** Storage for the entities. */
  56. private List entities;
  57. /**
  58. * Constructs a new entity collection (initially empty).
  59. */
  60. public StandardEntityCollection() {
  61. this.entities = new java.util.ArrayList();
  62. }
  63. /**
  64. * Returns the number of entities in the collection.
  65. *
  66. * @return The entity count.
  67. */
  68. public int getEntityCount() {
  69. return this.entities.size();
  70. }
  71. /**
  72. * Returns a chart entity from the collection.
  73. *
  74. * @param index the entity index.
  75. *
  76. * @return The entity.
  77. */
  78. public ChartEntity getEntity(int index) {
  79. return (ChartEntity) this.entities.get(index);
  80. }
  81. /**
  82. * Clears the entities.
  83. */
  84. public void clear() {
  85. this.entities.clear();
  86. }
  87. /**
  88. * Adds an entity to the collection.
  89. *
  90. * @param entity the entity (<code>null</code> not permitted).
  91. */
  92. public void add(ChartEntity entity) {
  93. if (entity == null) {
  94. throw new IllegalArgumentException("Null 'entity' argument.");
  95. }
  96. this.entities.add(entity);
  97. }
  98. /**
  99. * Adds all the entities from the specified collection.
  100. *
  101. * @param collection the collection of entities.
  102. */
  103. public void addAll(EntityCollection collection) {
  104. this.entities.addAll(collection.getEntities());
  105. }
  106. /**
  107. * Returns an entity for the specified coordinates.
  108. *
  109. * @param x the x coordinate.
  110. * @param y the y coordinate.
  111. *
  112. * @return the entity.
  113. */
  114. public ChartEntity getEntity(double x, double y) {
  115. ChartEntity result = null;
  116. Iterator iterator = this.entities.iterator();
  117. while (iterator.hasNext()) {
  118. ChartEntity entity = (ChartEntity) iterator.next();
  119. if (entity.getArea().contains(x, y)) {
  120. result = entity;
  121. }
  122. }
  123. return result;
  124. }
  125. /**
  126. * Returns the entities in an unmodifiable collection.
  127. *
  128. * @return The entities.
  129. */
  130. public Collection getEntities() {
  131. return Collections.unmodifiableCollection(this.entities);
  132. }
  133. /**
  134. * Returns an iterator for the entities in the collection.
  135. *
  136. * @return An iterator.
  137. */
  138. public Iterator iterator() {
  139. return this.entities.iterator();
  140. }
  141. /**
  142. * Tests this object for equality with an arbitrary object.
  143. *
  144. * @param obj the object to test against (<code>null</code> permitted).
  145. *
  146. * @return A boolean.
  147. */
  148. public boolean equals(Object obj) {
  149. if (obj == this) {
  150. return true;
  151. }
  152. if (obj instanceof StandardEntityCollection) {
  153. StandardEntityCollection c = (StandardEntityCollection) obj;
  154. return ObjectUtilities.equal(this.entities, c.entities);
  155. }
  156. return false;
  157. }
  158. /**
  159. * Returns a clone.
  160. *
  161. * @return A clone.
  162. *
  163. * @throws CloneNotSupportedException if the object cannot be cloned.
  164. */
  165. public Object clone() throws CloneNotSupportedException {
  166. return super.clone();
  167. }
  168. //// DEPRECATED CODE /////////////////////////////////////////////////////////////////////////
  169. /**
  170. * Adds an entity.
  171. *
  172. * @param entity the entity.
  173. */
  174. public void addEntity(ChartEntity entity) {
  175. this.entities.add(entity);
  176. }
  177. /**
  178. * Adds all the entities from the specified collection.
  179. *
  180. * @param collection the collection of entities.
  181. */
  182. public void addEntities(EntityCollection collection) {
  183. this.entities.addAll(collection.getEntities());
  184. }
  185. }