001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2014, 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 * StandardPieURLGenerator.java
029 * ----------------------------
030 * (C) Copyright 2002-2014, 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 * 07-Mar-2003 : Modified to use KeyedValuesDataset and added pieIndex
040 *               parameter (DG);
041 * 21-Mar-2003 : Implemented Serializable (DG);
042 * 24-Apr-2003 : Switched around PieDataset and KeyedValuesDataset (DG);
043 * 31-Mar-2004 : Added an optional 'pieIndex' parameter (DG);
044 * 13-Jan-2005 : Fixed for compliance with XHTML 1.0 (DG);
045 * ------------- JFREECHART 1.0.x ---------------------------------------------
046 * 24-Nov-2006 : Fixed equals() method and added argument checks (DG);
047 * 17-Apr-2007 : Encode section key in generateURL() (DG);
048 * 03-Jul-2013 : Use ParamChecks (DG);
049 *
050 */
051
052package org.jfree.chart.urls;
053
054import java.io.Serializable;
055import java.io.UnsupportedEncodingException;
056import java.net.URLEncoder;
057
058import org.jfree.chart.util.ParamChecks;
059import org.jfree.data.general.PieDataset;
060import org.jfree.util.ObjectUtilities;
061
062/**
063 * A URL generator for pie charts.  Instances of this class are immutable.
064 */
065public class StandardPieURLGenerator implements PieURLGenerator, Serializable {
066
067    /** For serialization. */
068    private static final long serialVersionUID = 1626966402065883419L;
069
070    /** The prefix. */
071    private String prefix = "index.html";
072
073    /** The category parameter name. */
074    private String categoryParamName = "category";
075
076    /** The pie index parameter name. */
077    private String indexParamName = "pieIndex";
078
079    /**
080     * Default constructor.
081     */
082    public StandardPieURLGenerator() {
083        this("index.html");
084    }
085
086    /**
087     * Creates a new generator.
088     *
089     * @param prefix  the prefix ({@code null} not permitted).
090     */
091    public StandardPieURLGenerator(String prefix) {
092        this(prefix, "category");
093    }
094
095    /**
096     * Creates a new generator.
097     *
098     * @param prefix  the prefix ({@code null} not permitted).
099     * @param categoryParamName  the category parameter name ({@code null} not 
100     *         permitted).
101     */
102    public StandardPieURLGenerator(String prefix, String categoryParamName) {
103        this(prefix, categoryParamName, "pieIndex");
104    }
105
106    /**
107     * Creates a new generator.
108     *
109     * @param prefix  the prefix ({@code null} not permitted).
110     * @param categoryParamName  the category parameter name ({@code null} not 
111     *         permitted).
112     * @param indexParamName  the index parameter name ({@code null} permitted).
113     */
114    public StandardPieURLGenerator(String prefix, String categoryParamName,
115            String indexParamName) {
116        ParamChecks.nullNotPermitted(prefix, "prefix");
117        ParamChecks.nullNotPermitted(categoryParamName, "categoryParamName");
118        this.prefix = prefix;
119        this.categoryParamName = categoryParamName;
120        this.indexParamName = indexParamName;
121    }
122
123    /**
124     * Generates a URL.
125     *
126     * @param dataset  the dataset (ignored).
127     * @param key  the item key ({@code null} not permitted).
128     * @param pieIndex  the pie index.
129     *
130     * @return A string containing the generated URL.
131     */
132    @Override
133    public String generateURL(PieDataset dataset, Comparable key,
134            int pieIndex) {
135        String url = this.prefix;
136        try {
137            if (url.contains("?")) {
138                url += "&" + this.categoryParamName + "="
139                        + URLEncoder.encode(key.toString(), "UTF-8");
140            } else {
141                url += "?" + this.categoryParamName + "="
142                        + URLEncoder.encode(key.toString(), "UTF-8");
143            }
144            if (this.indexParamName != null) {
145                url += "&" + this.indexParamName + "=" + pieIndex;
146            }
147        } catch (UnsupportedEncodingException e) {  // this won't happen :)
148            throw new RuntimeException(e);
149        }
150        return url;
151    }
152
153    /**
154     * Tests if this object is equal to another.
155     *
156     * @param obj  the object ({@code null} permitted).
157     *
158     * @return A boolean.
159     */
160    @Override
161    public boolean equals(Object obj) {
162        if (obj == this) {
163            return true;
164        }
165        if (!(obj instanceof StandardPieURLGenerator)) {
166            return false;
167        }
168        StandardPieURLGenerator that = (StandardPieURLGenerator) obj;
169        if (!this.prefix.equals(that.prefix)) {
170            return false;
171        }
172        if (!this.categoryParamName.equals(that.categoryParamName)) {
173            return false;
174        }
175        if (!ObjectUtilities.equal(this.indexParamName, that.indexParamName)) {
176            return false;
177        }
178        return true;
179    }
180}