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