001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * --------------
028 * TickUnits.java
029 * --------------
030 * (C) Copyright 2001-present, by David Gilbert.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.axis;
038
039import java.io.Serializable;
040import java.text.NumberFormat;
041import java.util.ArrayList;
042import java.util.Collections;
043import java.util.List;
044
045/**
046 * A collection of tick units, used by the {@link DateAxis} and
047 * {@link NumberAxis} classes.
048 */
049public class TickUnits implements TickUnitSource, Cloneable, Serializable {
050
051    /** For serialization. */
052    private static final long serialVersionUID = 1134174035901467545L;
053
054    /** Storage for the tick units. */
055    private List tickUnits;
056
057    /**
058     * Constructs a new collection of tick units.
059     */
060    public TickUnits() {
061        this.tickUnits = new ArrayList();
062    }
063
064    /**
065     * Adds a tick unit to the collection.  The tick units are maintained in
066     * ascending order.
067     *
068     * @param unit  the tick unit to add ({@code null} not permitted).
069     */
070    public void add(TickUnit unit) {
071        if (unit == null) {
072            throw new NullPointerException("Null 'unit' argument.");
073        }
074        this.tickUnits.add(unit);
075        Collections.sort(this.tickUnits);
076    }
077
078    /**
079     * Returns the number of tick units in this collection.
080     * <P>
081     * This method is required for the XML writer.
082     *
083     * @return The number of units in this collection.
084     */
085    public int size() {
086        return this.tickUnits.size();
087    }
088
089    /**
090     * Returns the tickunit on the given position.
091     * <P>
092     * This method is required for the XML writer.
093     *
094     * @param pos the position in the list.
095     *
096     * @return The tickunit.
097     */
098    public TickUnit get(int pos) {
099        return (TickUnit) this.tickUnits.get(pos);
100    }
101
102    /**
103     * Returns a tick unit that is larger than the supplied unit.
104     *
105     * @param unit   the unit.
106     *
107     * @return A tick unit that is larger than the supplied unit.
108     */
109    @Override
110    public TickUnit getLargerTickUnit(TickUnit unit) {
111        int index = Collections.binarySearch(this.tickUnits, unit);
112        if (index >= 0) {
113            index = index + 1;
114        }
115        else {
116            index = -index;
117        }
118
119        return (TickUnit) this.tickUnits.get(Math.min(index,
120                this.tickUnits.size() - 1));
121    }
122
123    /**
124     * Returns the tick unit in the collection that is greater than or equal
125     * to (in size) the specified unit.
126     *
127     * @param unit  the unit.
128     *
129     * @return A unit from the collection.
130     */
131    @Override
132    public TickUnit getCeilingTickUnit(TickUnit unit) {
133        int index = Collections.binarySearch(this.tickUnits, unit);
134        if (index >= 0) {
135            return (TickUnit) this.tickUnits.get(index);
136        }
137        else {
138            index = -(index + 1);
139            return (TickUnit) this.tickUnits.get(Math.min(index,
140                    this.tickUnits.size() - 1));
141        }
142    }
143
144    /**
145     * Returns the tick unit in the collection that is greater than or equal
146     * to the specified size.
147     *
148     * @param size  the size.
149     *
150     * @return A unit from the collection.
151     */
152    @Override
153    public TickUnit getCeilingTickUnit(double size) {
154        return getCeilingTickUnit(new NumberTickUnit(size,
155                NumberFormat.getInstance()));
156    }
157
158    /**
159     * Returns a clone of the collection.
160     *
161     * @return A clone.
162     *
163     * @throws CloneNotSupportedException if an item in the collection does not
164     *         support cloning.
165     */
166    @Override
167    public Object clone() throws CloneNotSupportedException {
168        TickUnits clone = (TickUnits) super.clone();
169        clone.tickUnits = new java.util.ArrayList(this.tickUnits);
170        return clone;
171    }
172
173    /**
174     * Tests an object for equality with this instance.
175     *
176     * @param obj  the object to test ({@code null} permitted).
177     *
178     * @return A boolean.
179     */
180    @Override
181    public boolean equals(Object obj) {
182        if (obj == this) {
183            return true;
184        }
185        if (!(obj instanceof TickUnits)) {
186            return false;
187        }
188        TickUnits that = (TickUnits) obj;
189        return that.tickUnits.equals(this.tickUnits);
190    }
191
192}