Source for org.jfree.chart.axis.TickUnits

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2005, 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:  * TickUnits.java
  29:  * --------------
  30:  * (C) Copyright 2001-2005, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: TickUnits.java,v 1.4.2.2 2005/10/25 20:37:34 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 23-Nov-2001 : Version 1 (DG);
  40:  * 18-Feb-2002 : Fixed bug in getNearestTickUnit (thanks to Mario Inchiosa for 
  41:  *               reporting this, SourceForge bug id 518073) (DG);
  42:  * 25-Feb-2002 : Moved createStandardTickUnits() method from NumberAxis, and 
  43:  *               added createIntegerTickUnits() method (DG);
  44:  * 01-May-2002 : Updated for changes to the TickUnit class (DG);
  45:  * 18-Sep-2002 : Added standardTickUnit methods which take a Locale 
  46:  *               instance (AS);
  47:  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  48:  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
  49:  * 26-Mar-2003 : Implemented Serializable (DG);
  50:  * 13-Aug-2003 : Implemented Cloneable (DG);
  51:  * 23-Sep-2003 : Implemented TickUnitSource interface (DG);
  52:  * 03-Dec-2003 : Adding null values now throws exceptions (TM);
  53:  * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0 
  54:  *               release (DG);
  55:  * 
  56:  */
  57: 
  58: package org.jfree.chart.axis;
  59: 
  60: import java.io.Serializable;
  61: import java.text.NumberFormat;
  62: import java.util.ArrayList;
  63: import java.util.Collections;
  64: import java.util.List;
  65: 
  66: /**
  67:  * A collection of tick units, used by the {@link DateAxis} and 
  68:  * {@link NumberAxis} classes.
  69:  */
  70: public class TickUnits implements TickUnitSource, Cloneable, Serializable {
  71: 
  72:     /** For serialization. */
  73:     private static final long serialVersionUID = 1134174035901467545L;
  74:     
  75:     /** Storage for the tick units. */
  76:     private List tickUnits;
  77: 
  78:     /**
  79:      * Constructs a new collection of tick units.
  80:      */
  81:     public TickUnits() {
  82:         this.tickUnits = new ArrayList();
  83:     }
  84: 
  85:     /**
  86:      * Adds a tick unit to the collection.
  87:      * <P>
  88:      * The tick units are maintained in ascending order.
  89:      *
  90:      * @param unit  the tick unit to add.
  91:      */
  92:     public void add(TickUnit unit) {
  93: 
  94:         if (unit == null) {
  95:             throw new NullPointerException("Null 'unit' argument.");
  96:         }
  97:         this.tickUnits.add(unit);
  98:         Collections.sort(this.tickUnits);
  99: 
 100:     }
 101: 
 102:     /**
 103:      * Returns the number of tick units in this collection.
 104:      * <P>
 105:      * This method is required for the XML writer.
 106:      *
 107:      * @return The number of units in this collection.
 108:      */
 109:     public int size() {
 110:         return this.tickUnits.size();
 111:     }
 112: 
 113:     /**
 114:      * Returns the tickunit on the given position.
 115:      * <P>
 116:      * This method is required for the XML writer.
 117:      *
 118:      * @param pos the position in the list.
 119:      * 
 120:      * @return The tickunit.
 121:      */
 122:     public TickUnit get(int pos) {
 123:         return (TickUnit) this.tickUnits.get(pos);
 124:     }
 125: 
 126:     /**
 127:      * Returns a tick unit that is larger than the supplied unit.
 128:      *
 129:      * @param unit   the unit.
 130:      *
 131:      * @return A tick unit that is larger than the supplied unit.
 132:      */
 133:     public TickUnit getLargerTickUnit(TickUnit unit) {
 134: 
 135:         int index = Collections.binarySearch(this.tickUnits, unit);
 136:         if (index >= 0) {
 137:             index = index + 1;
 138:         }
 139:         else {
 140:             index = -index;
 141:         }
 142: 
 143:         return (TickUnit) this.tickUnits.get(
 144:             Math.min(index, this.tickUnits.size() - 1)
 145:         );
 146: 
 147:     }
 148: 
 149:     /**
 150:      * Returns the tick unit in the collection that is greater than or equal
 151:      * to (in size) the specified unit.
 152:      *
 153:      * @param unit  the unit.
 154:      *
 155:      * @return A unit from the collection.
 156:      */
 157:     public TickUnit getCeilingTickUnit(TickUnit unit) {
 158: 
 159:         int index = Collections.binarySearch(this.tickUnits, unit);
 160:         if (index >= 0) {
 161:             return (TickUnit) this.tickUnits.get(index);
 162:         }
 163:         else {
 164:             index = -(index + 1);
 165:             return (TickUnit) this.tickUnits.get(
 166:                 Math.min(index, this.tickUnits.size() - 1)
 167:             );
 168:         }
 169: 
 170:     }
 171: 
 172:     /**
 173:      * Returns the tick unit in the collection that is greater than or equal
 174:      * to the specified size.
 175:      *
 176:      * @param size  the size.
 177:      *
 178:      * @return A unit from the collection.
 179:      */
 180:     public TickUnit getCeilingTickUnit(double size) {
 181:         return getCeilingTickUnit(new NumberTickUnit(size, 
 182:                 NumberFormat.getInstance()));
 183:     }
 184: 
 185:     /**
 186:      * Returns a clone of the collection.
 187:      *
 188:      * @return A clone.
 189:      *
 190:      * @throws CloneNotSupportedException if an item in the collection does not 
 191:      *         support cloning.
 192:      */
 193:     public Object clone() throws CloneNotSupportedException {
 194:         TickUnits clone = (TickUnits) super.clone();
 195:         clone.tickUnits = new java.util.ArrayList(this.tickUnits);
 196:         return clone;
 197:     }
 198: 
 199:     /**
 200:      * Tests an object for equality with this instance.
 201:      *
 202:      * @param object  the object to test.
 203:      *
 204:      * @return A boolean.
 205:      */
 206:     public boolean equals(Object object) {
 207:         if (object == null) {
 208:             return false;
 209:         }
 210:         if (object == this) {
 211:             return true;
 212:         }
 213:         if (object instanceof TickUnits) {
 214:             TickUnits tu = (TickUnits) object;
 215:             return tu.tickUnits.equals(this.tickUnits);
 216:         }
 217:         return false;
 218:     }
 219: 
 220: }