001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
006     *
007     * Project Info:  http://www.jfree.org/jfreechart/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it
010     * under the terms of the GNU Lesser General Public License as published by
011     * the Free Software Foundation; either version 2.1 of the License, or
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022     * USA.
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025     * in the United States and other countries.]
026     *
027     * -----------------------------
028     * StandardEntityCollection.java
029     * -----------------------------
030     * (C) Copyright 2001-2008, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * Changes
036     * -------
037     * 23-May-2002 : Version 1 (DG);
038     * 26-Jun-2002 : Added iterator() method (DG);
039     * 03-Oct-2002 : Fixed errors reported by Checkstyle (DG);
040     * 19-May-2004 : Implemented Serializable (DG);
041     * 29-Sep-2004 : Renamed addEntity() --> add() and addEntities()
042     *               --> addAll() (DG);
043     * 19-Jan-2005 : Changed storage from Collection --> List (DG);
044     * 20-May-2005 : Fixed bug 1113521 - inefficiency in getEntity() method (DG);
045     * ------------- JFREECHART 1.0.x ---------------------------------------------
046     * 01-Dec-2006 : Implemented PublicCloneable and fixed clone() method (DG);
047     *
048     */
049    
050    package org.jfree.chart.entity;
051    
052    import java.io.Serializable;
053    import java.util.Collection;
054    import java.util.Collections;
055    import java.util.Iterator;
056    import java.util.List;
057    
058    import org.jfree.util.ObjectUtilities;
059    import org.jfree.util.PublicCloneable;
060    
061    /**
062     * A standard implementation of the {@link EntityCollection} interface.
063     */
064    public class StandardEntityCollection implements EntityCollection,
065            Cloneable, PublicCloneable, Serializable {
066    
067        /** For serialization. */
068        private static final long serialVersionUID = 5384773031184897047L;
069    
070        /** Storage for the entities. */
071        private List entities;
072    
073        /**
074         * Constructs a new entity collection (initially empty).
075         */
076        public StandardEntityCollection() {
077            this.entities = new java.util.ArrayList();
078        }
079    
080        /**
081         * Returns the number of entities in the collection.
082         *
083         * @return The entity count.
084         */
085        public int getEntityCount() {
086            return this.entities.size();
087        }
088    
089        /**
090         * Returns a chart entity from the collection.
091         *
092         * @param index  the entity index.
093         *
094         * @return The entity.
095         *
096         * @see #add(ChartEntity)
097         */
098        public ChartEntity getEntity(int index) {
099            return (ChartEntity) this.entities.get(index);
100        }
101    
102        /**
103         * Clears all the entities from the collection.
104         */
105        public void clear() {
106            this.entities.clear();
107        }
108    
109        /**
110         * Adds an entity to the collection.
111         *
112         * @param entity  the entity (<code>null</code> not permitted).
113         */
114        public void add(ChartEntity entity) {
115            if (entity == null) {
116                throw new IllegalArgumentException("Null 'entity' argument.");
117            }
118            this.entities.add(entity);
119        }
120    
121        /**
122         * Adds all the entities from the specified collection.
123         *
124         * @param collection  the collection of entities (<code>null</code> not
125         *     permitted).
126         */
127        public void addAll(EntityCollection collection) {
128            this.entities.addAll(collection.getEntities());
129        }
130    
131        /**
132         * Returns the last entity in the list with an area that encloses the
133         * specified coordinates, or <code>null</code> if there is no such entity.
134         *
135         * @param x  the x coordinate.
136         * @param y  the y coordinate.
137         *
138         * @return The entity (possibly <code>null</code>).
139         */
140        public ChartEntity getEntity(double x, double y) {
141            int entityCount = this.entities.size();
142            for (int i = entityCount - 1; i >= 0; i--) {
143                ChartEntity entity = (ChartEntity) this.entities.get(i);
144                if (entity.getArea().contains(x, y)) {
145                    return entity;
146                }
147            }
148            return null;
149        }
150    
151        /**
152         * Returns the entities in an unmodifiable collection.
153         *
154         * @return The entities.
155         */
156        public Collection getEntities() {
157            return Collections.unmodifiableCollection(this.entities);
158        }
159    
160        /**
161         * Returns an iterator for the entities in the collection.
162         *
163         * @return An iterator.
164         */
165        public Iterator iterator() {
166            return this.entities.iterator();
167        }
168    
169        /**
170         * Tests this object for equality with an arbitrary object.
171         *
172         * @param obj  the object to test against (<code>null</code> permitted).
173         *
174         * @return A boolean.
175         */
176        public boolean equals(Object obj) {
177            if (obj == this) {
178                return true;
179            }
180            if (obj instanceof StandardEntityCollection) {
181                StandardEntityCollection that = (StandardEntityCollection) obj;
182                return ObjectUtilities.equal(this.entities, that.entities);
183            }
184            return false;
185        }
186    
187        /**
188         * Returns a clone of this entity collection.
189         *
190         * @return A clone.
191         *
192         * @throws CloneNotSupportedException if the object cannot be cloned.
193         */
194        public Object clone() throws CloneNotSupportedException {
195            StandardEntityCollection clone
196                    = (StandardEntityCollection) super.clone();
197            clone.entities = new java.util.ArrayList(this.entities.size());
198            for (int i = 0; i < this.entities.size(); i++) {
199                ChartEntity entity = (ChartEntity) this.entities.get(i);
200                clone.entities.add(entity.clone());
201            }
202            return clone;
203        }
204    
205    }