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