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 * StandardXYURLGenerator.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.util.Objects;
041import org.jfree.chart.util.Args;
042
043import org.jfree.data.xy.XYDataset;
044
045/**
046 * A URL generator.
047 */
048public class StandardXYURLGenerator implements XYURLGenerator, Serializable {
049
050    /** For serialization. */
051    private static final long serialVersionUID = -1771624523496595382L;
052
053    /** The default prefix. */
054    public static final String DEFAULT_PREFIX = "index.html";
055
056    /** The default series parameter. */
057    public static final String DEFAULT_SERIES_PARAMETER = "series";
058
059    /** The default item parameter. */
060    public static final String DEFAULT_ITEM_PARAMETER = "item";
061
062    /** Prefix to the URL */
063    private String prefix;
064
065    /** Series parameter name to go in each URL */
066    private String seriesParameterName;
067
068    /** Item parameter name to go in each URL */
069    private String itemParameterName;
070
071    /**
072     * Creates a new default generator.  This constructor is equivalent to
073     * calling {@code StandardXYURLGenerator("index.html", "series", "item");}.
074     */
075    public StandardXYURLGenerator() {
076        this(DEFAULT_PREFIX, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
077    }
078
079    /**
080     * Creates a new generator with the specified prefix.  This constructor
081     * is equivalent to calling
082     * {@code StandardXYURLGenerator(prefix, "series", "item");}.
083     *
084     * @param prefix  the prefix to the URL ({@code null} not permitted).
085     */
086    public StandardXYURLGenerator(String prefix) {
087        this(prefix, DEFAULT_SERIES_PARAMETER, DEFAULT_ITEM_PARAMETER);
088    }
089
090    /**
091     * Constructor that overrides all the defaults
092     *
093     * @param prefix  the prefix to the URL ({@code null} not permitted).
094     * @param seriesParameterName  the name of the series parameter to go in
095     *                             each URL ({@code null} not permitted).
096     * @param itemParameterName  the name of the item parameter to go in each
097     *                           URL ({@code null} not permitted).
098     */
099    public StandardXYURLGenerator(String prefix, String seriesParameterName,
100            String itemParameterName) {
101        Args.nullNotPermitted(prefix, "prefix");
102        Args.nullNotPermitted(seriesParameterName, "seriesParameterName");
103        Args.nullNotPermitted(itemParameterName, "itemParameterName");
104        this.prefix = prefix;
105        this.seriesParameterName = seriesParameterName;
106        this.itemParameterName = itemParameterName;
107    }
108
109    /**
110     * Generates a URL for a particular item within a series.
111     *
112     * @param dataset  the dataset.
113     * @param series  the series number (zero-based index).
114     * @param item  the item number (zero-based index).
115     *
116     * @return The generated URL.
117     */
118    @Override
119    public String generateURL(XYDataset dataset, int series, int item) {
120        // TODO: URLEncode?
121        String url = this.prefix;
122        boolean firstParameter = !url.contains("?");
123        url += firstParameter ? "?" : "&";
124        url += this.seriesParameterName + "=" + series
125                + "&" + this.itemParameterName + "=" + item;
126        return url;
127    }
128
129    /**
130     * Tests this generator for equality with an arbitrary object.
131     *
132     * @param obj  the object ({@code null} permitted).
133     *
134     * @return A boolean.
135     */
136    @Override
137    public boolean equals(Object obj) {
138        if (obj == this) {
139            return true;
140        }
141        if (!(obj instanceof StandardXYURLGenerator)) {
142            return false;
143        }
144        StandardXYURLGenerator that = (StandardXYURLGenerator) obj;
145        if (!Objects.equals(that.prefix, this.prefix)) {
146            return false;
147        }
148        if (!Objects.equals(that.seriesParameterName,
149                this.seriesParameterName)) {
150            return false;
151        }
152        if (!Objects.equals(that.itemParameterName,
153                this.itemParameterName)) {
154            return false;
155        }
156        return true;
157    }
158
159    @Override
160    public int hashCode() {
161        int hash = 7;
162        hash = 79 * hash + Objects.hashCode(this.prefix);
163        hash = 79 * hash + Objects.hashCode(this.seriesParameterName);
164        hash = 79 * hash + Objects.hashCode(this.itemParameterName);
165        return hash;
166    }
167
168}