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