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