    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 * DateTickUnitType.java
    029 * ---------------------
    030 * (C) Copyright 2009, by Object Refinery Limited.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   -;
    034 *
    035 * Changes
    036 * -------
    037 * 09-Jan-2009 : Version 1 (DG);
    038 *
    039 */
    040
    041package org.jfree.chart.axis;
    042
    043import java.io.ObjectStreamException;
    044import java.io.Serializable;
    045import java.util.Calendar;
    046
    047/**
    048 * An enumeration of the unit types for a {@link DateTickUnit} instance.
    049 *
    050 * @since 1.0.13
    051 */
    052public class DateTickUnitType implements Serializable {
    053
    054    /** Year. */
    055    public static final DateTickUnitType YEAR
    056            = new DateTickUnitType("DateTickUnitType.YEAR", Calendar.YEAR);
    057
    058    /** Month. */
    059    public static final DateTickUnitType MONTH
    060            = new DateTickUnitType("DateTickUnitType.MONTH", Calendar.MONTH);
    061
    062    /** Day. */
    063    public static final DateTickUnitType DAY
    064            = new DateTickUnitType("DateTickUnitType.DAY", Calendar.DATE);
    065
    066
    067    /** Hour. */
    068    public static final DateTickUnitType HOUR
    069            = new DateTickUnitType("DateTickUnitType.HOUR",
    070                    Calendar.HOUR_OF_DAY);
    071
    072    /** Minute. */
    073    public static final DateTickUnitType MINUTE
    074            = new DateTickUnitType("DateTickUnitType.MINUTE", Calendar.MINUTE);
    075
    076    /** Second. */
    077    public static final DateTickUnitType SECOND
    078            = new DateTickUnitType("DateTickUnitType.SECOND", Calendar.SECOND);
    079
    080    /** Millisecond. */
    081    public static final DateTickUnitType MILLISECOND
    082            = new DateTickUnitType("DateTickUnitType.MILLISECOND",
    083                    Calendar.MILLISECOND);
    084
    085    /** The name. */
    086    private String name;
    087
    088    /** The corresponding field value in Java's Calendar class. */
    089    private int calendarField;
    090
    091    /**
    092     * Private constructor.
    093     *
    094     * @param name  the name.
    095     * @param calendarField  the calendar field.
    096     */
    097    private DateTickUnitType(String name, int calendarField) {
    098        this.name = name;
    099        this.calendarField = calendarField;
    100    }
    101
    102    /**
    103     * Returns the calendar field.
    104     *
    105     * @return The calendar field.
    106     */
    107    public int getCalendarField() {
    108        return this.calendarField;
    109    }
    110
    111    /**
    112     * Returns a string representing the object.
    113     *
    114     * @return The string.
    115     */
    116    public String toString() {
    117        return this.name;
    118    }
    119
    120    /**
    121     * Returns <code>true</code> if this object is equal to the specified
    122     * object, and <code>false</code> otherwise.
    123     *
    124     * @param obj  the other object.
    125     *
    126     * @return A boolean.
    127     */
    128    public boolean equals(Object obj) {
    129        if (this == obj) {
    130            return true;
    131        }
    132        if (!(obj instanceof DateTickUnitType)) {
    133            return false;
    134        }
    135        DateTickUnitType t = (DateTickUnitType) obj;
    136        if (!this.name.equals(t.toString())) {
    137            return false;
    138        }
    139        return true;
    140    }
    141
    142    /**
    143     * Ensures that serialization returns the unique instances.
    144     *
    145     * @return The object.
    146     *
    147     * @throws ObjectStreamException if there is a problem.
    148     */
    149    private Object readResolve() throws ObjectStreamException {
    150        if (this.equals(DateTickUnitType.YEAR)) {
    151            return DateTickUnitType.YEAR;
    152        }
    153        else if (this.equals(DateTickUnitType.MONTH)) {
    154            return DateTickUnitType.MONTH;
    155        }
    156        else if (this.equals(DateTickUnitType.DAY)) {
    157            return DateTickUnitType.DAY;
    158        }
    159        else if (this.equals(DateTickUnitType.HOUR)) {
    160            return DateTickUnitType.HOUR;
    161        }
    162        else if (this.equals(DateTickUnitType.MINUTE)) {
    163            return DateTickUnitType.MINUTE;
    164        }
    165        else if (this.equals(DateTickUnitType.SECOND)) {
    166            return DateTickUnitType.SECOND;
    167        }
    168        else if (this.equals(DateTickUnitType.MILLISECOND)) {
    169            return DateTickUnitType.MILLISECOND;
    170        }
    171        return null;
    172    }
    173
    174}