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 * StandardPieURLGenerator.java
029 * ----------------------------
030 * (C) Copyright 2002-2013, 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 org.jfree.chart.util.ParamChecks;
056
057import org.jfree.data.general.PieDataset;
058import org.jfree.util.ObjectUtilities;
059
060/**
061 * A URL generator for pie charts.  Instances of this class are immutable.
062 */
063public class StandardPieURLGenerator implements PieURLGenerator, Serializable {
064
065    /** For serialization. */
066    private static final long serialVersionUID = 1626966402065883419L;
067
068    /** The prefix. */
069    private String prefix = "index.html";
070
071    /** The category parameter name. */
072    private String categoryParameterName = "category";
073
074    /** The pie index parameter name. */
075    private String indexParameterName = "pieIndex";
076
077    /**
078     * Default constructor.
079     */
080    public StandardPieURLGenerator() {
081        this("index.html");
082    }
083
084    /**
085     * Creates a new generator.
086     *
087     * @param prefix  the prefix (<code>null</code> not permitted).
088     */
089    public StandardPieURLGenerator(String prefix) {
090        this(prefix, "category");
091    }
092
093    /**
094     * Creates a new generator.
095     *
096     * @param prefix  the prefix (<code>null</code> not permitted).
097     * @param categoryParameterName  the category parameter name
098     *     (<code>null</code> not permitted).
099     */
100    public StandardPieURLGenerator(String prefix,
101                                   String categoryParameterName) {
102        this(prefix, categoryParameterName, "pieIndex");
103    }
104
105    /**
106     * Creates a new generator.
107     *
108     * @param prefix  the prefix (<code>null</code> not permitted).
109     * @param categoryParameterName  the category parameter name
110     *     (<code>null</code> not permitted).
111     * @param indexParameterName  the index parameter name (<code>null</code>
112     *     permitted).
113     */
114    public StandardPieURLGenerator(String prefix, String categoryParameterName,
115            String indexParameterName) {
116        ParamChecks.nullNotPermitted(prefix, "prefix");
117        ParamChecks.nullNotPermitted(categoryParameterName, "categoryParameterName");
118        this.prefix = prefix;
119        this.categoryParameterName = categoryParameterName;
120        this.indexParameterName = indexParameterName;
121    }
122
123    /**
124     * Generates a URL.
125     *
126     * @param dataset  the dataset (ignored).
127     * @param key  the item key (<code>null</code> 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        if (url.indexOf("?") > -1) {
137            url += "&amp;" + this.categoryParameterName + "="
138                    + URLUtilities.encode(key.toString(), "UTF-8");
139        }
140        else {
141            url += "?" + this.categoryParameterName + "="
142                    + URLUtilities.encode(key.toString(), "UTF-8");
143        }
144        if (this.indexParameterName != null) {
145            url += "&amp;" + this.indexParameterName + "="
146                   + String.valueOf(pieIndex);
147        }
148        return url;
149    }
150
151    /**
152     * Tests if this object is equal to another.
153     *
154     * @param obj  the object (<code>null</code> permitted).
155     *
156     * @return A boolean.
157     */
158    @Override
159    public boolean equals(Object obj) {
160        if (obj == this) {
161            return true;
162        }
163        if (!(obj instanceof StandardPieURLGenerator)) {
164            return false;
165        }
166        StandardPieURLGenerator that = (StandardPieURLGenerator) obj;
167        if (!this.prefix.equals(that.prefix)) {
168            return false;
169        }
170        if (!this.categoryParameterName.equals(that.categoryParameterName)) {
171            return false;
172        }
173        if (!ObjectUtilities.equal(this.indexParameterName,
174                that.indexParameterName)) {
175            return false;
176        }
177        return true;
178    }
179}