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 * CustomXYURLGenerator.java
029 * -------------------------
030 * (C) Copyright 2002-present, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributors:     David Gilbert;
034 *
035 */
036
037package org.jfree.chart.urls;
038
039import java.io.Serializable;
040import java.util.ArrayList;
041import java.util.List;
042import org.jfree.chart.util.PublicCloneable;
043
044import org.jfree.data.xy.XYDataset;
045
046/**
047 * A custom URL generator.
048 */
049public class CustomXYURLGenerator implements XYURLGenerator, Cloneable,
050        PublicCloneable, Serializable {
051
052    /** For serialization. */
053    private static final long serialVersionUID = -8565933356596551832L;
054
055    /** Storage for the URLs. */
056    private ArrayList urlSeries = new ArrayList();
057
058    /**
059     * Default constructor.
060     */
061    public CustomXYURLGenerator() {
062        super();
063    }
064
065    /**
066     * Returns the number of URL lists stored by the renderer.
067     *
068     * @return The list count.
069     */
070    public int getListCount() {
071        return this.urlSeries.size();
072    }
073
074    /**
075     * Returns the number of URLs in a given list.
076     *
077     * @param list  the list index (zero based).
078     *
079     * @return The URL count.
080     */
081    public int getURLCount(int list) {
082        int result = 0;
083        List urls = (List) this.urlSeries.get(list);
084        if (urls != null) {
085            result = urls.size();
086        }
087        return result;
088    }
089
090    /**
091     * Returns the URL for an item.
092     *
093     * @param series  the series index.
094     * @param item  the item index.
095     *
096     * @return The URL (possibly {@code null}).
097     */
098    public String getURL(int series, int item) {
099        String result = null;
100        if (series < getListCount()) {
101            List urls = (List) this.urlSeries.get(series);
102            if (urls != null) {
103                if (item < urls.size()) {
104                    result = (String) urls.get(item);
105                }
106            }
107        }
108        return result;
109    }
110
111    /**
112     * Generates a URL.
113     *
114     * @param dataset  the dataset.
115     * @param series  the series (zero-based index).
116     * @param item  the item (zero-based index).
117     *
118     * @return A string containing the URL (possibly {@code null}).
119     */
120    @Override
121    public String generateURL(XYDataset dataset, int series, int item) {
122        return getURL(series, item);
123    }
124
125    /**
126     * Adds a list of URLs.
127     *
128     * @param urls  the list of URLs ({@code null} permitted, the list
129     *     is copied).
130     */
131    public void addURLSeries(List urls) {
132        List listToAdd = null;
133        if (urls != null) {
134            listToAdd = new java.util.ArrayList(urls);
135        }
136        this.urlSeries.add(listToAdd);
137    }
138
139    /**
140     * Tests this generator for equality with an arbitrary object.
141     *
142     * @param obj  the object ({@code null} permitted).
143     *
144     * @return A boolean.
145     */
146    @Override
147    public boolean equals(Object obj) {
148        if (obj == this) {
149            return true;
150        }
151        if (!(obj instanceof CustomXYURLGenerator)) {
152            return false;
153        }
154        CustomXYURLGenerator that = (CustomXYURLGenerator) obj;
155        int listCount = getListCount();
156        if (listCount != that.getListCount()) {
157            return false;
158        }
159
160        for (int series = 0; series < listCount; series++) {
161            int urlCount = getURLCount(series);
162            if (urlCount != that.getURLCount(series)) {
163                return false;
164            }
165
166            for (int item = 0; item < urlCount; item++) {
167                String u1 = getURL(series, item);
168                String u2 = that.getURL(series, item);
169                if (u1 != null) {
170                    if (!u1.equals(u2)) {
171                        return false;
172                    }
173                }
174                else {
175                    if (u2 != null) {
176                        return false;
177                    }
178                }
179            }
180        }
181        return true;
182
183    }
184
185    /**
186     * Returns a new generator that is a copy of, and independent from, this
187     * generator.
188     *
189     * @return A clone.
190     *
191     * @throws CloneNotSupportedException if there is a problem with cloning.
192     */
193    @Override
194    public Object clone() throws CloneNotSupportedException {
195        CustomXYURLGenerator clone = (CustomXYURLGenerator) super.clone();
196        clone.urlSeries = new java.util.ArrayList(this.urlSeries);
197        return clone;
198    }
199
200}