001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2005, 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: * TickUnits.java
029: * --------------
030: * (C) Copyright 2001-2005, by Object Refinery Limited.
031: *
032: * Original Author: David Gilbert (for Object Refinery Limited);
033: * Contributor(s): -;
034: *
035: * $Id: TickUnits.java,v 1.4.2.2 2005/10/25 20:37:34 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 23-Nov-2001 : Version 1 (DG);
040: * 18-Feb-2002 : Fixed bug in getNearestTickUnit (thanks to Mario Inchiosa for
041: * reporting this, SourceForge bug id 518073) (DG);
042: * 25-Feb-2002 : Moved createStandardTickUnits() method from NumberAxis, and
043: * added createIntegerTickUnits() method (DG);
044: * 01-May-2002 : Updated for changes to the TickUnit class (DG);
045: * 18-Sep-2002 : Added standardTickUnit methods which take a Locale
046: * instance (AS);
047: * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
048: * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
049: * 26-Mar-2003 : Implemented Serializable (DG);
050: * 13-Aug-2003 : Implemented Cloneable (DG);
051: * 23-Sep-2003 : Implemented TickUnitSource interface (DG);
052: * 03-Dec-2003 : Adding null values now throws exceptions (TM);
053: * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
054: * release (DG);
055: *
056: */
057:
058: package org.jfree.chart.axis;
059:
060: import java.io.Serializable;
061: import java.text.NumberFormat;
062: import java.util.ArrayList;
063: import java.util.Collections;
064: import java.util.List;
065:
066: /**
067: * A collection of tick units, used by the {@link DateAxis} and
068: * {@link NumberAxis} classes.
069: */
070: public class TickUnits implements TickUnitSource, Cloneable,
071: Serializable {
072:
073: /** For serialization. */
074: private static final long serialVersionUID = 1134174035901467545L;
075:
076: /** Storage for the tick units. */
077: private List tickUnits;
078:
079: /**
080: * Constructs a new collection of tick units.
081: */
082: public TickUnits() {
083: this .tickUnits = new ArrayList();
084: }
085:
086: /**
087: * Adds a tick unit to the collection.
088: * <P>
089: * The tick units are maintained in ascending order.
090: *
091: * @param unit the tick unit to add.
092: */
093: public void add(TickUnit unit) {
094:
095: if (unit == null) {
096: throw new NullPointerException("Null 'unit' argument.");
097: }
098: this .tickUnits.add(unit);
099: Collections.sort(this .tickUnits);
100:
101: }
102:
103: /**
104: * Returns the number of tick units in this collection.
105: * <P>
106: * This method is required for the XML writer.
107: *
108: * @return The number of units in this collection.
109: */
110: public int size() {
111: return this .tickUnits.size();
112: }
113:
114: /**
115: * Returns the tickunit on the given position.
116: * <P>
117: * This method is required for the XML writer.
118: *
119: * @param pos the position in the list.
120: *
121: * @return The tickunit.
122: */
123: public TickUnit get(int pos) {
124: return (TickUnit) this .tickUnits.get(pos);
125: }
126:
127: /**
128: * Returns a tick unit that is larger than the supplied unit.
129: *
130: * @param unit the unit.
131: *
132: * @return A tick unit that is larger than the supplied unit.
133: */
134: public TickUnit getLargerTickUnit(TickUnit unit) {
135:
136: int index = Collections.binarySearch(this .tickUnits, unit);
137: if (index >= 0) {
138: index = index + 1;
139: } else {
140: index = -index;
141: }
142:
143: return (TickUnit) this .tickUnits.get(Math.min(index,
144: this .tickUnits.size() - 1));
145:
146: }
147:
148: /**
149: * Returns the tick unit in the collection that is greater than or equal
150: * to (in size) the specified unit.
151: *
152: * @param unit the unit.
153: *
154: * @return A unit from the collection.
155: */
156: public TickUnit getCeilingTickUnit(TickUnit unit) {
157:
158: int index = Collections.binarySearch(this .tickUnits, unit);
159: if (index >= 0) {
160: return (TickUnit) this .tickUnits.get(index);
161: } else {
162: index = -(index + 1);
163: return (TickUnit) this .tickUnits.get(Math.min(index,
164: this .tickUnits.size() - 1));
165: }
166:
167: }
168:
169: /**
170: * Returns the tick unit in the collection that is greater than or equal
171: * to the specified size.
172: *
173: * @param size the size.
174: *
175: * @return A unit from the collection.
176: */
177: public TickUnit getCeilingTickUnit(double size) {
178: return getCeilingTickUnit(new NumberTickUnit(size, NumberFormat
179: .getInstance()));
180: }
181:
182: /**
183: * Returns a clone of the collection.
184: *
185: * @return A clone.
186: *
187: * @throws CloneNotSupportedException if an item in the collection does not
188: * support cloning.
189: */
190: public Object clone() throws CloneNotSupportedException {
191: TickUnits clone = (TickUnits) super .clone();
192: clone.tickUnits = new java.util.ArrayList(this .tickUnits);
193: return clone;
194: }
195:
196: /**
197: * Tests an object for equality with this instance.
198: *
199: * @param object the object to test.
200: *
201: * @return A boolean.
202: */
203: public boolean equals(Object object) {
204: if (object == null) {
205: return false;
206: }
207: if (object == this ) {
208: return true;
209: }
210: if (object instanceof TickUnits) {
211: TickUnits tu = (TickUnits) object;
212: return tu.tickUnits.equals(this .tickUnits);
213: }
214: return false;
215: }
216:
217: }
|