001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2020, 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 * [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-2008, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 23-Nov-2001 : Version 1 (DG);
038 * 18-Feb-2002 : Fixed bug in getNearestTickUnit (thanks to Mario Inchiosa for
039 *               reporting this, SourceForge bug id 518073) (DG);
040 * 25-Feb-2002 : Moved createStandardTickUnits() method from NumberAxis, and
041 *               added createIntegerTickUnits() method (DG);
042 * 01-May-2002 : Updated for changes to the TickUnit class (DG);
043 * 18-Sep-2002 : Added standardTickUnit methods which take a Locale
044 *               instance (AS);
045 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
046 * 08-Nov-2002 : Moved to new package com.jrefinery.chart.axis (DG);
047 * 26-Mar-2003 : Implemented Serializable (DG);
048 * 13-Aug-2003 : Implemented Cloneable (DG);
049 * 23-Sep-2003 : Implemented TickUnitSource interface (DG);
050 * 03-Dec-2003 : Adding null values now throws exceptions (TM);
051 * 11-Jan-2005 : Removed deprecated methods in preparation for 1.0.0
052 *               release (DG);
053 *
054 */
055
056package org.jfree.chart.axis;
057
058import java.io.Serializable;
059import java.text.NumberFormat;
060import java.util.ArrayList;
061import java.util.Collections;
062import java.util.List;
063
064/**
065 * A collection of tick units, used by the {@link DateAxis} and
066 * {@link NumberAxis} classes.
067 */
068public class TickUnits implements TickUnitSource, Cloneable, Serializable {
069
070    /** For serialization. */
071    private static final long serialVersionUID = 1134174035901467545L;
072
073    /** Storage for the tick units. */
074    private List tickUnits;
075
076    /**
077     * Constructs a new collection of tick units.
078     */
079    public TickUnits() {
080        this.tickUnits = new ArrayList();
081    }
082
083    /**
084     * Adds a tick unit to the collection.  The tick units are maintained in
085     * ascending order.
086     *
087     * @param unit  the tick unit to add ({@code null} not permitted).
088     */
089    public void add(TickUnit unit) {
090        if (unit == null) {
091            throw new NullPointerException("Null 'unit' argument.");
092        }
093        this.tickUnits.add(unit);
094        Collections.sort(this.tickUnits);
095    }
096
097    /**
098     * Returns the number of tick units in this collection.
099     * <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    @Override
129    public TickUnit getLargerTickUnit(TickUnit unit) {
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     * Returns the tick unit in the collection that is greater than or equal
144     * to (in size) the specified unit.
145     *
146     * @param unit  the unit.
147     *
148     * @return A unit from the collection.
149     */
150    @Override
151    public TickUnit getCeilingTickUnit(TickUnit unit) {
152        int index = Collections.binarySearch(this.tickUnits, unit);
153        if (index >= 0) {
154            return (TickUnit) this.tickUnits.get(index);
155        }
156        else {
157            index = -(index + 1);
158            return (TickUnit) this.tickUnits.get(Math.min(index,
159                    this.tickUnits.size() - 1));
160        }
161    }
162
163    /**
164     * Returns the tick unit in the collection that is greater than or equal
165     * to the specified size.
166     *
167     * @param size  the size.
168     *
169     * @return A unit from the collection.
170     */
171    @Override
172    public TickUnit getCeilingTickUnit(double size) {
173        return getCeilingTickUnit(new NumberTickUnit(size,
174                NumberFormat.getInstance()));
175    }
176
177    /**
178     * Returns a clone of the collection.
179     *
180     * @return A clone.
181     *
182     * @throws CloneNotSupportedException if an item in the collection does not
183     *         support cloning.
184     */
185    @Override
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} permitted).
196     *
197     * @return A boolean.
198     */
199    @Override
200    public boolean equals(Object obj) {
201        if (obj == this) {
202            return true;
203        }
204        if (!(obj instanceof TickUnits)) {
205            return false;
206        }
207        TickUnits that = (TickUnits) obj;
208        return that.tickUnits.equals(this.tickUnits);
209    }
210
211}