    001    /* ===========================================================
    002     * JFreeChart : a free chart library for the Java(tm) platform
    003     * ===========================================================
    004     *
    005     * (C) Copyright 2000-2011, 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     * CustomXYURLGenerator.java
    029     * -------------------------
    030     * (C) Copyright 2002-2008, 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     * 23-Mar-2003 : Implemented Serializable (DG);
    040     * 20-Jan-2005 : Minor Javadoc update (DG);
    041     * ------------- JFREECHART 1.0.x ---------------------------------------------
    042     * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
    043     * 11-Apr-2008 : Implemented Cloneable, otherwise charts using this URL
    044     *               generator will fail to clone (DG);
    045     *
    046     */
    047
    048    package org.jfree.chart.urls;
    049
    050    import java.io.Serializable;
    051    import java.util.ArrayList;
    052    import java.util.List;
    053
    054    import org.jfree.data.xy.XYDataset;
    055    import org.jfree.util.PublicCloneable;
    056
    057    /**
    058     * A custom URL generator.
    059     */
    060    public class CustomXYURLGenerator implements XYURLGenerator, Cloneable,
    061            PublicCloneable, Serializable {
    062
    063        /** For serialization. */
    064        private static final long serialVersionUID = -8565933356596551832L;
    065
    066        /** Storage for the URLs. */
    067        private ArrayList urlSeries = new ArrayList();
    068
    069        /**
    070         * Default constructor.
    071         */
    072        public CustomXYURLGenerator() {
    073            super();
    074        }
    075
    076        /**
    077         * Returns the number of URL lists stored by the renderer.
    078         *
    079         * @return The list count.
    080         */
    081        public int getListCount() {
    082            return this.urlSeries.size();
    083        }
    084
    085        /**
    086         * Returns the number of URLs in a given list.
    087         *
    088         * @param list  the list index (zero based).
    089         *
    090         * @return The URL count.
    091         */
    092        public int getURLCount(int list) {
    093            int result = 0;
    094            List urls = (List) this.urlSeries.get(list);
    095            if (urls != null) {
    096                result = urls.size();
    097            }
    098            return result;
    099        }
    100
    101        /**
    102         * Returns the URL for an item.
    103         *
    104         * @param series  the series index.
    105         * @param item  the item index.
    106         *
    107         * @return The URL (possibly <code>null</code>).
    108         */
    109        public String getURL(int series, int item) {
    110            String result = null;
    111            if (series < getListCount()) {
    112                List urls = (List) this.urlSeries.get(series);
    113                if (urls != null) {
    114                    if (item < urls.size()) {
    115                        result = (String) urls.get(item);
    116                    }
    117                }
    118            }
    119            return result;
    120        }
    121
    122        /**
    123         * Generates a URL.
    124         *
    125         * @param dataset  the dataset.
    126         * @param series  the series (zero-based index).
    127         * @param item  the item (zero-based index).
    128         *
    129         * @return A string containing the URL (possibly <code>null</code>).
    130         */
    131        public String generateURL(XYDataset dataset, int series, int item) {
    132            return getURL(series, item);
    133        }
    134
    135        /**
    136         * Adds a list of URLs.
    137         *
    138         * @param urls  the list of URLs (<code>null</code> permitted, the list
    139         *     is copied).
    140         */
    141        public void addURLSeries(List urls) {
    142            List listToAdd = null;
    143            if (urls != null) {
    144                listToAdd = new java.util.ArrayList(urls);
    145            }
    146            this.urlSeries.add(listToAdd);
    147        }
    148
    149        /**
    150         * Tests this generator for equality with an arbitrary object.
    151         *
    152         * @param obj  the object (<code>null</code> permitted).
    153         *
    154         * @return A boolean.
    155         */
    156        public boolean equals(Object obj) {
    157            if (obj == this) {
    158                return true;
    159            }
    160            if (!(obj instanceof CustomXYURLGenerator)) {
    161                return false;
    162            }
    163            CustomXYURLGenerator that = (CustomXYURLGenerator) obj;
    164            int listCount = getListCount();
    165            if (listCount != that.getListCount()) {
    166                return false;
    167            }
    168
    169            for (int series = 0; series < listCount; series++) {
    170                int urlCount = getURLCount(series);
    171                if (urlCount != that.getURLCount(series)) {
    172                    return false;
    173                }
    174
    175                for (int item = 0; item < urlCount; item++) {
    176                    String u1 = getURL(series, item);
    177                    String u2 = that.getURL(series, item);
    178                    if (u1 != null) {
    179                        if (!u1.equals(u2)) {
    180                            return false;
    181                        }
    182                    }
    183                    else {
    184                        if (u2 != null) {
    185                            return false;
    186                        }
    187                    }
    188                }
    189            }
    190            return true;
    191
    192        }
    193
    194        /**
    195         * Returns a new generator that is a copy of, and independent from, this
    196         * generator.
    197         *
    198         * @return A clone.
    199         *
    200         * @throws CloneNotSupportedException if there is a problem with cloning.
    201         */
    202        public Object clone() throws CloneNotSupportedException {
    203            CustomXYURLGenerator clone = (CustomXYURLGenerator) super.clone();
    204            clone.urlSeries = new java.util.ArrayList(this.urlSeries);
    205            return clone;
    206        }
    207
    208    }