001/* ======================================
002 * JFreeChart : a free Java chart library
003 * ======================================
004 *
005 * (C) Copyright 2000-2022, 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 * CustomPieURLGenerator.java
029 * --------------------------
030 * (C) Copyright 2004-2021, by David Basten and Contributors.
031 *
032 * Original Author:  David Basten;
033 * Contributors:     -;
034 *
035 */
036
037package org.jfree.chart.urls;
038
039import java.io.Serializable;
040import java.util.ArrayList;
041import java.util.HashMap;
042import java.util.Iterator;
043import java.util.List;
044import java.util.Map;
045import java.util.Set;
046
047import org.jfree.chart.plot.pie.MultiplePiePlot;
048import org.jfree.chart.plot.pie.PiePlot;
049import org.jfree.chart.api.PublicCloneable;
050import org.jfree.data.general.PieDataset;
051
052/**
053 * A custom URL generator for pie charts.  This implementation supports both
054 * the standard {@link PiePlot} as well as {@link MultiplePiePlot}.
055 */
056public class CustomPieURLGenerator implements PieURLGenerator,
057        Cloneable, PublicCloneable, Serializable {
058
059    /** For serialization. */
060    private static final long serialVersionUID = 7100607670144900503L;
061
062    /** Storage for the URLs - a list to support multi pie plots. */
063    private final List<Map<Comparable<?>, String>> urlMaps;
064
065    /**
066     * Creates a new {@code CustomPieURLGenerator} instance, initially
067     * empty.  Call {@link #addURLs(Map)} to specify the URL fragments to be
068     * used.
069     */
070    public CustomPieURLGenerator() {
071        this.urlMaps = new ArrayList<>();
072    }
073
074    /**
075     * Generates a URL fragment.
076     *
077     * @param dataset  the dataset (ignored).
078     * @param key  the item key.
079     * @param plotIndex  the plot index.
080     *
081     * @return A string containing the generated URL.
082     *
083     * @see #getURL(Comparable, int)
084     */
085    @Override
086    public String generateURL(PieDataset dataset, Comparable<?> key,
087                              int plotIndex) {
088        return getURL(key, plotIndex);
089    }
090
091    /**
092     * Returns the number of URL maps stored by the generator.  For a standard
093     * pie chart, only one map is required, but for a {@link MultiplePiePlot}
094     * the generator will use multiple maps.
095     *
096     * @return The list count.
097     *
098     * @see #addURLs(Map)
099     */
100    public int getListCount() {
101        return this.urlMaps.size();
102    }
103
104    /**
105     * Returns the number of URLs in a given map (specified by its position
106     * in the map list).
107     *
108     * @param plotIndex  the plot index (zero based).
109     *
110     * @return The URL count.
111     *
112     * @see #getListCount()
113     */
114    public int getURLCount(int plotIndex) {
115        int result = 0;
116        Map<Comparable<?>, String> urlMap = this.urlMaps.get(plotIndex);
117        if (urlMap != null) {
118            result = urlMap.size();
119        }
120        return result;
121    }
122
123    /**
124     * Returns the URL for a section in the specified map.
125     *
126     * @param key  the key.
127     * @param plotIndex  the plot index.
128     *
129     * @return The URL.
130     */
131    public String getURL(Comparable<?> key, int plotIndex) {
132        String result = null;
133        if (plotIndex < getListCount()) {
134            Map<Comparable<?>, String> urlMap = this.urlMaps.get(plotIndex);
135            if (urlMap != null) {
136                result = (String) urlMap.get(key);
137            }
138        }
139        return result;
140    }
141
142    /**
143     * Adds a map containing {@code (key, URL)} mappings where each
144     * {@code key} is an instance of {@code Comparable}
145     * (corresponding to the key for an item in a pie dataset) and each
146     * {@code URL} is a {@code String} representing a URL fragment.
147     * <br><br>
148     * The map is appended to an internal list...you can add multiple maps
149     * if you are working with, say, a {@link MultiplePiePlot}.
150     *
151     * @param urlMap  the URLs ({@code null} permitted).
152     */
153    public void addURLs(Map urlMap) {
154        this.urlMaps.add(urlMap);
155    }
156
157    /**
158     * Tests if this object is equal to another.
159     *
160     * @param o  the other object.
161     *
162     * @return A boolean.
163     */
164    @Override
165    public boolean equals(Object o) {
166
167        if (o == this) {
168            return true;
169        }
170
171        if (o instanceof CustomPieURLGenerator) {
172            CustomPieURLGenerator generator = (CustomPieURLGenerator) o;
173            if (getListCount() != generator.getListCount()) {
174                return false;
175            }
176            Set keySet;
177            for (int pieItem = 0; pieItem < getListCount(); pieItem++) {
178                if (getURLCount(pieItem) != generator.getURLCount(pieItem)) {
179                    return false;
180                }
181                keySet = this.urlMaps.get(pieItem).keySet();
182                String key;
183                for (Iterator i = keySet.iterator(); i.hasNext();) {
184                key = (String) i.next();
185                    if (!getURL(key, pieItem).equals(
186                            generator.getURL(key, pieItem))) {
187                        return false;
188                    }
189                }
190            }
191            return true;
192        }
193        return false;
194    }
195
196    /**
197     * Returns a clone of the generator.
198     *
199     * @return A clone.
200     *
201     * @throws CloneNotSupportedException if cloning is not supported.
202     */
203    @Override
204    public Object clone() throws CloneNotSupportedException {
205        CustomPieURLGenerator urlGen = new CustomPieURLGenerator();
206        Map map;
207        Map newMap;
208        String key;
209
210        for (Iterator i = this.urlMaps.iterator(); i.hasNext();) {
211            map = (Map) i.next();
212
213            newMap = new HashMap();
214            for (Iterator j = map.keySet().iterator(); j.hasNext();) {
215                key = (String) j.next();
216                newMap.put(key, map.get(key));
217            }
218
219            urlGen.addURLs(newMap);
220        }
221
222        return urlGen;
223    }
224
225}