KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 License
20  * along with this library; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
24  * in the United States and other countries.]
25  *
26  * --------------
27  * TickUnits.java
28  * --------------
29  * (C) Copyright 2001-2005, by Object Refinery Limited.
30  *
31  * Original Author: David Gilbert (for Object Refinery Limited);
32  * Contributor(s): -;
33  *
34  * $Id: TickUnits.java,v 1.4 2005/05/19 13:58:11 mungady Exp $
35  *
36  * Changes
37  * -------
38  * 23-Nov-2001 : Version 1 (DG);
39  * 18-Feb-2002 : Fixed bug in getNearestTickUnit (thanks to Mario Inchiosa for
40  * reporting this, SourceForge bug id 518073) (DG);
41  * 25-Feb-2002 : Moved createStandardTickUnits() method from NumberAxis, and
42  * added createIntegerTickUnits() method (DG);
43  * 01-May-2002 : Updated for changes to the TickUnit class (DG);
44  * 18-Sep-2002 : Added standardTickUnit methods which take a Locale
45  * instance (AS);
46  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
47  * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
48  * 26-Mar-2003 : Implemented Serializable (DG);
49  * 13-Aug-2003 : Implemented Cloneable (DG);
50  * 23-Sep-2003 : Implemented TickUnitSource interface (DG);
51  * 03-Dec-2003 : Adding null values now throws exceptions (TM);
52  * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
53  * release (DG);
54  *
55  */

56
57 package org.jfree.chart.axis;
58
59 import java.io.Serializable JavaDoc;
60 import java.util.ArrayList JavaDoc;
61 import java.util.Collections JavaDoc;
62 import java.util.List JavaDoc;
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 JavaDoc, Serializable JavaDoc {
69
70     /** For serialization. */
71     private static final long serialVersionUID = 1134174035901467545L;
72     
73     /** Storage for the tick units. */
74     private List JavaDoc tickUnits;
75
76     /**
77      * Constructs a new collection of tick units.
78      */

79     public TickUnits() {
80         this.tickUnits = new ArrayList JavaDoc();
81     }
82
83     /**
84      * Adds a tick unit to the collection.
85      * <P>
86      * The tick units are maintained in ascending order.
87      *
88      * @param unit the tick unit to add.
89      */

90     public void add(TickUnit unit) {
91
92         if (unit == null) {
93             throw new NullPointerException JavaDoc("Null 'unit' argument.");
94         }
95         this.tickUnits.add(unit);
96         Collections.sort(this.tickUnits);
97
98     }
99
100     /**
101      * Returns the number of tick units in this collection.
102      * <P>
103      * This method is required for the XML writer.
104      *
105      * @return The number of units in this collection.
106      */

107     public int size() {
108         return this.tickUnits.size();
109     }
110
111     /**
112      * Returns the tickunit on the given position.
113      * <P>
114      * This method is required for the XML writer.
115      *
116      * @param pos the position in the list.
117      *
118      * @return The tickunit.
119      */

120     public TickUnit get(int pos) {
121         return (TickUnit) this.tickUnits.get(pos);
122     }
123
124     /**
125      * Returns a tick unit that is larger than the supplied unit.
126      *
127      * @param unit the unit.
128      *
129      * @return A tick unit that is larger than the supplied unit.
130      */

131     public TickUnit getLargerTickUnit(TickUnit unit) {
132
133         int index = Collections.binarySearch(this.tickUnits, unit);
134         if (index >= 0) {
135             index = index + 1;
136         }
137         else {
138             index = -index;
139         }
140
141         return (TickUnit) this.tickUnits.get(
142             Math.min(index, this.tickUnits.size() - 1)
143         );
144
145     }
146
147     /**
148      * Returns the tick unit in the collection that is greater than or equal
149      * to (in size) the specified unit.
150      *
151      * @param unit the unit.
152      *
153      * @return A unit from the collection.
154      */

155     public TickUnit getCeilingTickUnit(TickUnit unit) {
156
157         int index = Collections.binarySearch(this.tickUnits, unit);
158         if (index >= 0) {
159             return (TickUnit) this.tickUnits.get(index);
160         }
161         else {
162             index = -(index + 1);
163             return (TickUnit) this.tickUnits.get(
164                 Math.min(index, this.tickUnits.size() - 1)
165             );
166         }
167
168     }
169
170     /**
171      * Returns the tick unit in the collection that is greater than or equal
172      * to the specified size.
173      *
174      * @param size the size.
175      *
176      * @return A unit from the collection.
177      */

178     public TickUnit getCeilingTickUnit(double size) {
179         return getCeilingTickUnit(new NumberTickUnit(size, null));
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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
191         TickUnits clone = (TickUnits) super.clone();
192         clone.tickUnits = new java.util.ArrayList JavaDoc(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 JavaDoc 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 }
218
Popular Tags