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