KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > entity > StandardEntityCollection


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
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * -----------------------------
27  * StandardEntityCollection.java
28  * -----------------------------
29  * (C) Copyright 2001-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: StandardEntityCollection.java,v 1.8 2005/05/20 08:30:07 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 23-May-2002 : Version 1 (DG);
39  * 26-Jun-2002 : Added iterator() method (DG);
40  * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
41  * 19-May-2004 : Implemented Serializable (DG);
42  * 29-Sep-2004 : Renamed addEntity() --> add() and addEntities()
43  * --> addAll() (DG);
44  * 19-Jan-2005 : Changed storage from Collection --> List (DG);
45  * 20-May-2005 : Fixed bug 1113521 - inefficiency in getEntity() method (DG);
46  *
47  */

48
49 package org.jfree.chart.entity;
50
51 import java.io.Serializable JavaDoc;
52 import java.util.Collection JavaDoc;
53 import java.util.Collections JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56
57 import org.jfree.util.ObjectUtilities;
58
59 /**
60  * A standard implementation of the {@link EntityCollection} interface.
61  */

62 public class StandardEntityCollection implements EntityCollection,
63                                                  Cloneable JavaDoc, Serializable JavaDoc {
64
65     /** For serialization. */
66     private static final long serialVersionUID = 5384773031184897047L;
67     
68     /** Storage for the entities. */
69     private List JavaDoc entities;
70
71     /**
72      * Constructs a new entity collection (initially empty).
73      */

74     public StandardEntityCollection() {
75         this.entities = new java.util.ArrayList JavaDoc();
76     }
77
78     /**
79      * Returns the number of entities in the collection.
80      *
81      * @return The entity count.
82      */

83     public int getEntityCount() {
84         return this.entities.size();
85     }
86     
87     /**
88      * Returns a chart entity from the collection.
89      *
90      * @param index the entity index.
91      *
92      * @return The entity.
93      */

94     public ChartEntity getEntity(int index) {
95         return (ChartEntity) this.entities.get(index);
96     }
97     
98     /**
99      * Clears the entities.
100      */

101     public void clear() {
102         this.entities.clear();
103     }
104
105     /**
106      * Adds an entity to the collection.
107      *
108      * @param entity the entity (<code>null</code> not permitted).
109      */

110     public void add(ChartEntity entity) {
111         if (entity == null) {
112             throw new IllegalArgumentException JavaDoc("Null 'entity' argument.");
113         }
114         this.entities.add(entity);
115     }
116     
117     /**
118      * Adds all the entities from the specified collection.
119      *
120      * @param collection the collection of entities.
121      */

122     public void addAll(EntityCollection collection) {
123         this.entities.addAll(collection.getEntities());
124     }
125
126     /**
127      * Returns the last entity in the list with an area that encloses the
128      * specified coordinates, or <code>null</code> if there is no such entity.
129      *
130      * @param x the x coordinate.
131      * @param y the y coordinate.
132      *
133      * @return The entity (possibly <code>null</code>).
134      */

135     public ChartEntity getEntity(double x, double y) {
136         int entityCount = this.entities.size();
137         for (int i = entityCount - 1; i >= 0; i--) {
138             ChartEntity entity = (ChartEntity) this.entities.get(i);
139             if (entity.getArea().contains(x, y)) {
140                 return entity;
141             }
142         }
143         return null;
144     }
145
146     /**
147      * Returns the entities in an unmodifiable collection.
148      *
149      * @return The entities.
150      */

151     public Collection JavaDoc getEntities() {
152         return Collections.unmodifiableCollection(this.entities);
153     }
154     
155     /**
156      * Returns an iterator for the entities in the collection.
157      *
158      * @return An iterator.
159      */

160     public Iterator JavaDoc iterator() {
161         return this.entities.iterator();
162     }
163     
164     /**
165      * Tests this object for equality with an arbitrary object.
166      *
167      * @param obj the object to test against (<code>null</code> permitted).
168      *
169      * @return A boolean.
170      */

171     public boolean equals(Object JavaDoc obj) {
172         if (obj == this) {
173             return true;
174         }
175         if (obj instanceof StandardEntityCollection) {
176             StandardEntityCollection that = (StandardEntityCollection) obj;
177             return ObjectUtilities.equal(this.entities, that.entities);
178         }
179         return false;
180     }
181
182     /**
183      * Returns a clone.
184      *
185      * @return A clone.
186      *
187      * @throws CloneNotSupportedException if the object cannot be cloned.
188      */

189     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
190         return super.clone();
191     }
192
193 }
194
Popular Tags