001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------------------
028 * StandardEntityCollection.java
029 * -----------------------------
030 * (C) Copyright 2001-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.entity;
038
039import java.io.Serializable;
040import java.util.Collection;
041import java.util.Collections;
042import java.util.Iterator;
043import java.util.List;
044import java.util.Objects;
045import org.jfree.chart.util.Args;
046import org.jfree.chart.util.PublicCloneable;
047
048/**
049 * A standard implementation of the {@link EntityCollection} interface.
050 */
051public class StandardEntityCollection implements EntityCollection,
052        Cloneable, PublicCloneable, Serializable {
053
054    /** For serialization. */
055    private static final long serialVersionUID = 5384773031184897047L;
056
057    /** Storage for the entities. */
058    private List entities;
059
060    /**
061     * Constructs a new entity collection (initially empty).
062     */
063    public StandardEntityCollection() {
064        this.entities = new java.util.ArrayList();
065    }
066
067    /**
068     * Returns the number of entities in the collection.
069     *
070     * @return The entity count.
071     */
072    @Override
073    public int getEntityCount() {
074        return this.entities.size();
075    }
076
077    /**
078     * Returns a chart entity from the collection.
079     *
080     * @param index  the entity index.
081     *
082     * @return The entity.
083     *
084     * @see #add(ChartEntity)
085     */
086    @Override
087    public ChartEntity getEntity(int index) {
088        return (ChartEntity) this.entities.get(index);
089    }
090
091    /**
092     * Clears all the entities from the collection.
093     */
094    @Override
095    public void clear() {
096        this.entities.clear();
097    }
098
099    /**
100     * Adds an entity to the collection.
101     *
102     * @param entity  the entity ({@code null} not permitted).
103     */
104    @Override
105    public void add(ChartEntity entity) {
106        Args.nullNotPermitted(entity, "entity");
107        this.entities.add(entity);
108    }
109
110    /**
111     * Adds all the entities from the specified collection.
112     *
113     * @param collection  the collection of entities ({@code null} not
114     *     permitted).
115     */
116    @Override
117    public void addAll(EntityCollection collection) {
118        this.entities.addAll(collection.getEntities());
119    }
120
121    /**
122     * Returns the last entity in the list with an area that encloses the
123     * specified coordinates, or {@code null} if there is no such entity.
124     *
125     * @param x  the x coordinate.
126     * @param y  the y coordinate.
127     *
128     * @return The entity (possibly {@code null}).
129     */
130    @Override
131    public ChartEntity getEntity(double x, double y) {
132        int entityCount = this.entities.size();
133        for (int i = entityCount - 1; i >= 0; i--) {
134            ChartEntity entity = (ChartEntity) this.entities.get(i);
135            if (entity.getArea().contains(x, y)) {
136                return entity;
137            }
138        }
139        return null;
140    }
141
142    /**
143     * Returns the entities in an unmodifiable collection.
144     *
145     * @return The entities.
146     */
147    @Override
148    public Collection getEntities() {
149        return Collections.unmodifiableCollection(this.entities);
150    }
151
152    /**
153     * Returns an iterator for the entities in the collection.
154     *
155     * @return An iterator.
156     */
157    @Override
158    public Iterator iterator() {
159        return this.entities.iterator();
160    }
161
162    /**
163     * Tests this object for equality with an arbitrary object.
164     *
165     * @param obj  the object to test against ({@code null} permitted).
166     *
167     * @return A boolean.
168     */
169    @Override
170    public boolean equals(Object obj) {
171        if (obj == this) {
172            return true;
173        }
174        if (obj instanceof StandardEntityCollection) {
175            StandardEntityCollection that = (StandardEntityCollection) obj;
176            return Objects.equals(this.entities, that.entities);
177        }
178        return false;
179    }
180
181    /**
182     * Returns a clone of this entity collection.
183     *
184     * @return A clone.
185     *
186     * @throws CloneNotSupportedException if the object cannot be cloned.
187     */
188    @Override
189    public Object clone() throws CloneNotSupportedException {
190        StandardEntityCollection clone
191                = (StandardEntityCollection) super.clone();
192        clone.entities = new java.util.ArrayList(this.entities.size());
193        for (int i = 0; i < this.entities.size(); i++) {
194            ChartEntity entity = (ChartEntity) this.entities.get(i);
195            clone.entities.add(entity.clone());
196        }
197        return clone;
198    }
199
200}