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 * SimpleTimePeriod.java 029 * --------------------- 030 * (C) Copyright 2002-present, by David Gilbert and Contributors. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.data.time; 038 039import java.io.Serializable; 040import java.util.Date; 041 042/** 043 * An arbitrary period of time, measured to millisecond precision using 044 * {@code java.util.Date}. 045 * <p> 046 * This class is intentionally immutable (that is, once constructed, you cannot 047 * alter the start and end attributes). 048 */ 049public class SimpleTimePeriod implements TimePeriod, Comparable, Serializable { 050 051 /** For serialization. */ 052 private static final long serialVersionUID = 8684672361131829554L; 053 054 /** The start date/time. */ 055 private long start; 056 057 /** The end date/time. */ 058 private long end; 059 060 /** 061 * Creates a new time allocation. 062 * 063 * @param start the start date/time in milliseconds. 064 * @param end the end date/time in milliseconds. 065 */ 066 public SimpleTimePeriod(long start, long end) { 067 if (start > end) { 068 throw new IllegalArgumentException("Requires start <= end."); 069 } 070 this.start = start; 071 this.end = end; 072 } 073 074 /** 075 * Creates a new time allocation. 076 * 077 * @param start the start date/time ({@code null} not permitted). 078 * @param end the end date/time ({@code null} not permitted). 079 */ 080 public SimpleTimePeriod(Date start, Date end) { 081 this(start.getTime(), end.getTime()); 082 } 083 084 /** 085 * Returns the start date/time. 086 * 087 * @return The start date/time (never {@code null}). 088 */ 089 @Override 090 public Date getStart() { 091 return new Date(this.start); 092 } 093 094 /** 095 * Returns the start date/time in milliseconds. 096 * 097 * @return The start. 098 */ 099 public long getStartMillis() { 100 return this.start; 101 } 102 103 /** 104 * Returns the end date/time. 105 * 106 * @return The end date/time (never {@code null}). 107 */ 108 @Override 109 public Date getEnd() { 110 return new Date(this.end); 111 } 112 113 /** 114 * Returns the end date/time in milliseconds. 115 * 116 * @return The end. 117 */ 118 public long getEndMillis() { 119 return this.end; 120 } 121 122 /** 123 * Tests this time period instance for equality with an arbitrary object. 124 * The object is considered equal if it is an instance of {@link TimePeriod} 125 * and it has the same start and end dates. 126 * 127 * @param obj the other object ({@code null} permitted). 128 * 129 * @return A boolean. 130 */ 131 @Override 132 public boolean equals(Object obj) { 133 if (obj == this) { 134 return true; 135 } 136 if (!(obj instanceof TimePeriod)) { 137 return false; 138 } 139 TimePeriod that = (TimePeriod) obj; 140 if (!this.getStart().equals(that.getStart())) { 141 return false; 142 } 143 if (!this.getEnd().equals(that.getEnd())) { 144 return false; 145 } 146 return true; 147 } 148 149 /** 150 * Returns an integer that indicates the relative ordering of two 151 * time periods. 152 * 153 * @param obj the object ({@code null} not permitted). 154 * 155 * @return An integer. 156 * 157 * @throws ClassCastException if {@code obj} is not an instance of 158 * {@link TimePeriod}. 159 */ 160 @Override 161 public int compareTo(Object obj) { 162 TimePeriod that = (TimePeriod) obj; 163 long t0 = getStart().getTime(); 164 long t1 = getEnd().getTime(); 165 long m0 = t0 + (t1 - t0) / 2L; 166 long t2 = that.getStart().getTime(); 167 long t3 = that.getEnd().getTime(); 168 long m1 = t2 + (t3 - t2) / 2L; 169 if (m0 < m1) { 170 return -1; 171 } 172 else if (m0 > m1) { 173 return 1; 174 } 175 else { 176 if (t0 < t2) { 177 return -1; 178 } 179 else if (t0 > t2) { 180 return 1; 181 } 182 else { 183 if (t1 < t3) { 184 return -1; 185 } 186 else if (t1 > t3) { 187 return 1; 188 } 189 else { 190 return 0; 191 } 192 } 193 } 194 } 195 196 /** 197 * Returns a hash code for this object instance. The approach described by 198 * Joshua Bloch in "Effective Java" has been used here - see: 199 * <p> 200 * {@code http://developer.java.sun.com/ 201 * developer/Books/effectivejava/Chapter3.pdf} 202 * 203 * @return A hash code. 204 */ 205 @Override 206 public int hashCode() { 207 int result = 17; 208 result = 37 * result + (int) this.start; 209 result = 37 * result + (int) this.end; 210 return result; 211 } 212 213}