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 * LegendItemCollection.java
029 * -------------------------
030 * (C) Copyright 2002-2013, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 07-Feb-2002 : Version 1 (DG);
038 * 24-Sep-2002 : Added get(int) and getItemCount() methods (DG);
039 * 02-Oct-2002 : Fixed errors reported by Checkstyle (DG);
040 * 18-Apr-2005 : Added equals() method and implemented Cloneable and
041 *               Serializable (DG);
042 * 23-Apr-2008 : Fixed clone() method (DG);
043 *
044 */
045
046package org.jfree.chart;
047
048import java.io.Serializable;
049import java.util.Iterator;
050import java.util.List;
051
052import org.jfree.util.ObjectUtilities;
053
054/**
055 * A collection of legend items.
056 */
057public class LegendItemCollection implements Cloneable, Serializable {
058
059    /** For serialization. */
060    private static final long serialVersionUID = 1365215565589815953L;
061
062    /** Storage for the legend items. */
063    private List items;
064
065    /**
066     * Constructs a new legend item collection, initially empty.
067     */
068    public LegendItemCollection() {
069        this.items = new java.util.ArrayList();
070    }
071
072    /**
073     * Adds a legend item to the collection.
074     *
075     * @param item  the item to add.
076     */
077    public void add(LegendItem item) {
078        this.items.add(item);
079    }
080
081    /**
082     * Adds the legend items from another collection to this collection.
083     *
084     * @param collection  the other collection (<code>null</code> not
085     *     permitted).
086     */
087    public void addAll(LegendItemCollection collection) {
088        this.items.addAll(collection.items);
089    }
090
091    /**
092     * Returns a legend item from the collection.
093     *
094     * @param index  the legend item index (zero-based).
095     *
096     * @return The legend item.
097     */
098    public LegendItem get(int index) {
099        return (LegendItem) this.items.get(index);
100    }
101
102    /**
103     * Returns the number of legend items in the collection.
104     *
105     * @return The item count.
106     */
107    public int getItemCount() {
108        return this.items.size();
109    }
110
111    /**
112     * Returns an iterator that provides access to all the legend items.
113     *
114     * @return An iterator.
115     */
116    public Iterator iterator() {
117        return this.items.iterator();
118    }
119
120    /**
121     * Tests this collection for equality with an arbitrary object.
122     *
123     * @param obj  the object (<code>null</code> permitted).
124     *
125     * @return A boolean.
126     */
127    @Override
128    public boolean equals(Object obj) {
129        if (obj == this) {
130            return true;
131        }
132        if (!(obj instanceof LegendItemCollection)) {
133            return false;
134        }
135        LegendItemCollection that = (LegendItemCollection) obj;
136        if (!this.items.equals(that.items)) {
137            return false;
138        }
139        return true;
140    }
141
142    /**
143     * Returns a clone of the collection.
144     *
145     * @return A clone.
146     *
147     * @throws CloneNotSupportedException if an item in the collection is not
148     *         cloneable.
149     */
150    @Override
151    public Object clone() throws CloneNotSupportedException {
152        LegendItemCollection clone = (LegendItemCollection) super.clone();
153        clone.items = (List) ObjectUtilities.deepClone(this.items);
154        return clone;
155    }
156
157}