    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, 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</code> 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    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}