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