001/* ====================================== 002 * JFreeChart : a free Java chart library 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 * CustomCategoryURLGenerator.java 029 * ------------------------------- 030 * (C) Copyright 2008-present, by Diego Pierangeli and Contributors. 031 * 032 * Original Author: Diego Pierangeli; 033 * Contributors: David Gilbert; 034 * 035 */ 036package org.jfree.chart.urls; 037 038import java.io.Serializable; 039import java.util.ArrayList; 040import java.util.List; 041import org.jfree.chart.util.PublicCloneable; 042 043import org.jfree.data.category.CategoryDataset; 044 045/** 046 * A custom URL generator. 047 */ 048public class CustomCategoryURLGenerator implements CategoryURLGenerator, 049 Cloneable, PublicCloneable, Serializable { 050 051 /** Storage for the URLs. */ 052 private ArrayList urlSeries = new ArrayList(); 053 054 /** 055 * Default constructor. 056 */ 057 public CustomCategoryURLGenerator() { 058 super(); 059 } 060 061 /** 062 * Returns the number of URL lists stored by the renderer. 063 * 064 * @return The list count. 065 */ 066 public int getListCount() { 067 return this.urlSeries.size(); 068 } 069 070 /** 071 * Returns the number of URLs in a given list. 072 * 073 * @param list the list index (zero based). 074 * 075 * @return The URL count. 076 */ 077 public int getURLCount(int list) { 078 int result = 0; 079 List urls = (List) this.urlSeries.get(list); 080 if (urls != null) { 081 result = urls.size(); 082 } 083 return result; 084 } 085 086 /** 087 * Returns the URL for an item. 088 * 089 * @param series the series index. 090 * @param item the item index. 091 * 092 * @return The URL (possibly {@code null}). 093 */ 094 public String getURL(int series, int item) { 095 String result = null; 096 if (series < getListCount()) { 097 List urls = (List) this.urlSeries.get(series); 098 if (urls != null) { 099 if (item < urls.size()) { 100 result = (String) urls.get(item); 101 } 102 } 103 } 104 return result; 105 } 106 107 /** 108 * Generates a URL. 109 * 110 * @param dataset the dataset (ignored in this implementation). 111 * @param series the series (zero-based index). 112 * @param item the item (zero-based index). 113 * 114 * @return A string containing the URL (possibly {@code null}). 115 */ 116 @Override 117 public String generateURL(CategoryDataset dataset, int series, int item) { 118 return getURL(series, item); 119 } 120 121 /** 122 * Adds a list of URLs. 123 * 124 * @param urls the list of URLs ({@code null} permitted). 125 */ 126 public void addURLSeries(List urls) { 127 List listToAdd = null; 128 if (urls != null) { 129 listToAdd = new java.util.ArrayList(urls); 130 } 131 this.urlSeries.add(listToAdd); 132 } 133 134 /** 135 * Tests if this object is equal to another. 136 * 137 * @param obj the other object. 138 * 139 * @return A boolean. 140 */ 141 @Override 142 public boolean equals(Object obj) { 143 if (obj == this) { 144 return true; 145 } 146 if (!(obj instanceof CustomCategoryURLGenerator)) { 147 return false; 148 } 149 CustomCategoryURLGenerator generator = (CustomCategoryURLGenerator) obj; 150 int listCount = getListCount(); 151 if (listCount != generator.getListCount()) { 152 return false; 153 } 154 155 for (int series = 0; series < listCount; series++) { 156 int urlCount = getURLCount(series); 157 if (urlCount != generator.getURLCount(series)) { 158 return false; 159 } 160 161 for (int item = 0; item < urlCount; item++) { 162 String u1 = getURL(series, item); 163 String u2 = generator.getURL(series, item); 164 if (u1 != null) { 165 if (!u1.equals(u2)) { 166 return false; 167 } 168 } else { 169 if (u2 != null) { 170 return false; 171 } 172 } 173 } 174 } 175 return true; 176 } 177 178 /** 179 * Returns a new generator that is a copy of, and independent from, this 180 * generator. 181 * 182 * @return A clone. 183 * 184 * @throws CloneNotSupportedException if there is a problem with cloning. 185 */ 186 @Override 187 public Object clone() throws CloneNotSupportedException { 188 CustomCategoryURLGenerator clone 189 = (CustomCategoryURLGenerator) super.clone(); 190 clone.urlSeries = new java.util.ArrayList(this.urlSeries); 191 return clone; 192 } 193 194}