Source for org.jfree.chart.urls.CustomXYURLGenerator

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------------
  28:  * CustomXYURLGenerator.java
  29:  * -------------------------
  30:  * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributors:     David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: CustomXYURLGenerator.java,v 1.5.2.2 2007/02/02 15:51:05 mungady Exp $
  36:  *
  37:  * Changes:
  38:  * --------
  39:  * 05-Aug-2002 : Version 1, contributed by Richard Atkinson;
  40:  * 09-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * 23-Mar-2003 : Implemented Serializable (DG);
  42:  * 20-Jan-2005 : Minor Javadoc update (DG);
  43:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  44:  * 02-Feb-2007 : Removed author tags from all over JFreeChart sources (DG);
  45:  *
  46:  */
  47: 
  48: package org.jfree.chart.urls;
  49: 
  50: import java.io.Serializable;
  51: import java.util.ArrayList;
  52: import java.util.List;
  53: 
  54: import org.jfree.data.xy.XYDataset;
  55: 
  56: /**
  57:  * A custom URL generator.
  58:  */
  59: public class CustomXYURLGenerator implements XYURLGenerator, Serializable {
  60: 
  61:     /** For serialization. */
  62:     private static final long serialVersionUID = -8565933356596551832L;
  63:     
  64:     /** Storage for the URLs. */
  65:     private ArrayList urlSeries = new ArrayList();
  66: 
  67:     /**
  68:      * Default constructor.
  69:      */
  70:     public CustomXYURLGenerator() {
  71:         super();
  72:     }
  73: 
  74:     /**
  75:      * Returns the number of URL lists stored by the renderer.
  76:      *
  77:      * @return The list count.
  78:      */
  79:     public int getListCount() {
  80:         return this.urlSeries.size();
  81:     }
  82: 
  83:     /**
  84:      * Returns the number of URLs in a given list.
  85:      *
  86:      * @param list  the list index (zero based).
  87:      *
  88:      * @return The URL count.
  89:      */
  90:     public int getURLCount(int list) {
  91:         int result = 0;
  92:         List urls = (List) this.urlSeries.get(list);
  93:         if (urls != null) {
  94:             result = urls.size();
  95:         }
  96:         return result;
  97:     }
  98: 
  99:     /**
 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: }