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