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 * StandardPieURLGenerator.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.io.UnsupportedEncodingException;
041import java.net.URLEncoder;
042import java.util.Objects;
043
044import org.jfree.chart.util.Args;
045import org.jfree.data.general.PieDataset;
046
047/**
048 * A URL generator for pie charts.  Instances of this class are immutable.
049 */
050public class StandardPieURLGenerator implements PieURLGenerator, Serializable {
051
052    /** For serialization. */
053    private static final long serialVersionUID = 1626966402065883419L;
054
055    /** The prefix. */
056    private String prefix = "index.html";
057
058    /** The category parameter name. */
059    private String categoryParamName = "category";
060
061    /** The pie index parameter name. */
062    private String indexParamName = "pieIndex";
063
064    /**
065     * Default constructor.
066     */
067    public StandardPieURLGenerator() {
068        this("index.html");
069    }
070
071    /**
072     * Creates a new generator.
073     *
074     * @param prefix  the prefix ({@code null} not permitted).
075     */
076    public StandardPieURLGenerator(String prefix) {
077        this(prefix, "category");
078    }
079
080    /**
081     * Creates a new generator.
082     *
083     * @param prefix  the prefix ({@code null} not permitted).
084     * @param categoryParamName  the category parameter name ({@code null} not 
085     *         permitted).
086     */
087    public StandardPieURLGenerator(String prefix, String categoryParamName) {
088        this(prefix, categoryParamName, "pieIndex");
089    }
090
091    /**
092     * Creates a new generator.
093     *
094     * @param prefix  the prefix ({@code null} not permitted).
095     * @param categoryParamName  the category parameter name ({@code null} not 
096     *         permitted).
097     * @param indexParamName  the index parameter name ({@code null} permitted).
098     */
099    public StandardPieURLGenerator(String prefix, String categoryParamName,
100            String indexParamName) {
101        Args.nullNotPermitted(prefix, "prefix");
102        Args.nullNotPermitted(categoryParamName, "categoryParamName");
103        this.prefix = prefix;
104        this.categoryParamName = categoryParamName;
105        this.indexParamName = indexParamName;
106    }
107
108    /**
109     * Generates a URL.
110     *
111     * @param dataset  the dataset (ignored).
112     * @param key  the item key ({@code null} not permitted).
113     * @param pieIndex  the pie index.
114     *
115     * @return A string containing the generated URL.
116     */
117    @Override
118    public String generateURL(PieDataset dataset, Comparable key,
119            int pieIndex) {
120        String url = this.prefix;
121        try {
122            if (url.contains("?")) {
123                url += "&" + this.categoryParamName + "="
124                        + URLEncoder.encode(key.toString(), "UTF-8");
125            } else {
126                url += "?" + this.categoryParamName + "="
127                        + URLEncoder.encode(key.toString(), "UTF-8");
128            }
129            if (this.indexParamName != null) {
130                url += "&" + this.indexParamName + "=" + pieIndex;
131            }
132        } catch (UnsupportedEncodingException e) {  // this won't happen :)
133            throw new RuntimeException(e);
134        }
135        return url;
136    }
137
138    /**
139     * Tests if this object is equal to another.
140     *
141     * @param obj  the object ({@code null} permitted).
142     *
143     * @return A boolean.
144     */
145    @Override
146    public boolean equals(Object obj) {
147        if (obj == this) {
148            return true;
149        }
150        if (!(obj instanceof StandardPieURLGenerator)) {
151            return false;
152        }
153        StandardPieURLGenerator that = (StandardPieURLGenerator) obj;
154        if (!this.prefix.equals(that.prefix)) {
155            return false;
156        }
157        if (!this.categoryParamName.equals(that.categoryParamName)) {
158            return false;
159        }
160        if (!Objects.equals(this.indexParamName, that.indexParamName)) {
161            return false;
162        }
163        return true;
164    }
165}