001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 @Override 117 public String toString() { 118 return this.name; 119 } 120 121 /** 122 * Returns <code>true</code> if this object is equal to the specified 123 * object, and <code>false</code> otherwise. 124 * 125 * @param obj the other object. 126 * 127 * @return A boolean. 128 */ 129 @Override 130 public boolean equals(Object obj) { 131 if (this == obj) { 132 return true; 133 } 134 if (!(obj instanceof DateTickUnitType)) { 135 return false; 136 } 137 DateTickUnitType t = (DateTickUnitType) obj; 138 if (!this.name.equals(t.toString())) { 139 return false; 140 } 141 return true; 142 } 143 144 /** 145 * Ensures that serialization returns the unique instances. 146 * 147 * @return The object. 148 * 149 * @throws ObjectStreamException if there is a problem. 150 */ 151 private Object readResolve() throws ObjectStreamException { 152 if (this.equals(DateTickUnitType.YEAR)) { 153 return DateTickUnitType.YEAR; 154 } 155 else if (this.equals(DateTickUnitType.MONTH)) { 156 return DateTickUnitType.MONTH; 157 } 158 else if (this.equals(DateTickUnitType.DAY)) { 159 return DateTickUnitType.DAY; 160 } 161 else if (this.equals(DateTickUnitType.HOUR)) { 162 return DateTickUnitType.HOUR; 163 } 164 else if (this.equals(DateTickUnitType.MINUTE)) { 165 return DateTickUnitType.MINUTE; 166 } 167 else if (this.equals(DateTickUnitType.SECOND)) { 168 return DateTickUnitType.SECOND; 169 } 170 else if (this.equals(DateTickUnitType.MILLISECOND)) { 171 return DateTickUnitType.MILLISECOND; 172 } 173 return null; 174 } 175 176}