001 /* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2007, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025 * in the United States and other countries.]
026 *
027 * -------------------------
028 * CustomXYURLGenerator.java
029 * -------------------------
030 * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
031 *
032 * Original Author: Richard Atkinson;
033 * Contributors: David Gilbert (for Object Refinery Limited);
034 *
035 * $Id: CustomXYURLGenerator.java,v 1.5.2.2 2007/02/02 15:51:05 mungady Exp $
036 *
037 * Changes:
038 * --------
039 * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
040 * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
041 * 23-Mar-2003 : Implemented Serializable (DG);
042 * 20-Jan-2005 : Minor Javadoc update (DG);
043 * ------------- JFREECHART 1.0.x ---------------------------------------------
044 * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (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
056 /**
057 * A custom URL generator.
058 */
059 public class CustomXYURLGenerator implements XYURLGenerator, Serializable {
060
061 /** For serialization. */
062 private static final long serialVersionUID = -8565933356596551832L;
063
064 /** Storage for the URLs. */
065 private ArrayList urlSeries = new ArrayList();
066
067 /**
068 * Default constructor.
069 */
070 public CustomXYURLGenerator() {
071 super();
072 }
073
074 /**
075 * Returns the number of URL lists stored by the renderer.
076 *
077 * @return The list count.
078 */
079 public int getListCount() {
080 return this.urlSeries.size();
081 }
082
083 /**
084 * Returns the number of URLs in a given list.
085 *
086 * @param list the list index (zero based).
087 *
088 * @return The URL count.
089 */
090 public int getURLCount(int list) {
091 int result = 0;
092 List urls = (List) this.urlSeries.get(list);
093 if (urls != null) {
094 result = urls.size();
095 }
096 return result;
097 }
098
099 /**
100 * Returns the URL for an item.
101 *
102 * @param series the series index.
103 * @param item the item index.
104 *
105 * @return The URL (possibly <code>null</code>).
106 */
107 public String getURL(int series, int item) {
108 String result = null;
109 if (series < getListCount()) {
110 List urls = (List) this.urlSeries.get(series);
111 if (urls != null) {
112 if (item < urls.size()) {
113 result = (String) urls.get(item);
114 }
115 }
116 }
117 return result;
118 }
119
120 /**
121 * Generates a URL.
122 *
123 * @param dataset the dataset.
124 * @param series the series (zero-based index).
125 * @param item the item (zero-based index).
126 *
127 * @return A string containing the URL (possibly <code>null</code>).
128 */
129 public String generateURL(XYDataset dataset, int series, int item) {
130 return getURL(series, item);
131 }
132
133 /**
134 * Adds a list of URLs.
135 *
136 * @param urls the list of URLs.
137 */
138 public void addURLSeries(List urls) {
139 this.urlSeries.add(urls);
140 }
141
142 /**
143 * Tests if this object is equal to another.
144 *
145 * @param o the other object.
146 *
147 * @return A boolean.
148 */
149 public boolean equals(Object o) {
150
151 if (o == null) {
152 return false;
153 }
154 if (o == this) {
155 return true;
156 }
157
158 if (!(o instanceof CustomXYURLGenerator)) {
159 return false;
160 }
161 CustomXYURLGenerator generator = (CustomXYURLGenerator) o;
162 int listCount = getListCount();
163 if (listCount != generator.getListCount()) {
164 return false;
165 }
166
167 for (int series = 0; series < listCount; series++) {
168 int urlCount = getURLCount(series);
169 if (urlCount != generator.getURLCount(series)) {
170 return false;
171 }
172
173 for (int item = 0; item < urlCount; item++) {
174 String u1 = getURL(series, item);
175 String u2 = generator.getURL(series, item);
176 if (u1 != null) {
177 if (!u1.equals(u2)) {
178 return false;
179 }
180 }
181 else {
182 if (u2 != null) {
183 return false;
184 }
185 }
186 }
187 }
188 return true;
189
190 }
191
192 }