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