001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, 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-2022, 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 * @since 1.0.10. 100 */ 101 public long getStartMillis() { 102 return this.start; 103 } 104 105 /** 106 * Returns the end date/time. 107 * 108 * @return The end date/time (never {@code null}). 109 */ 110 @Override 111 public Date getEnd() { 112 return new Date(this.end); 113 } 114 115 /** 116 * Returns the end date/time in milliseconds. 117 * 118 * @return The end. 119 * 120 * @since 1.0.10. 121 */ 122 public long getEndMillis() { 123 return this.end; 124 } 125 126 /** 127 * Tests this time period instance for equality with an arbitrary object. 128 * The object is considered equal if it is an instance of {@link TimePeriod} 129 * and it has the same start and end dates. 130 * 131 * @param obj the other object ({@code null} permitted). 132 * 133 * @return A boolean. 134 */ 135 @Override 136 public boolean equals(Object obj) { 137 if (obj == this) { 138 return true; 139 } 140 if (!(obj instanceof TimePeriod)) { 141 return false; 142 } 143 TimePeriod that = (TimePeriod) obj; 144 if (!this.getStart().equals(that.getStart())) { 145 return false; 146 } 147 if (!this.getEnd().equals(that.getEnd())) { 148 return false; 149 } 150 return true; 151 } 152 153 /** 154 * Returns an integer that indicates the relative ordering of two 155 * time periods. 156 * 157 * @param obj the object ({@code null} not permitted). 158 * 159 * @return An integer. 160 * 161 * @throws ClassCastException if {@code obj} is not an instance of 162 * {@link TimePeriod}. 163 */ 164 @Override 165 public int compareTo(Object obj) { 166 TimePeriod that = (TimePeriod) obj; 167 long t0 = getStart().getTime(); 168 long t1 = getEnd().getTime(); 169 long m0 = t0 + (t1 - t0) / 2L; 170 long t2 = that.getStart().getTime(); 171 long t3 = that.getEnd().getTime(); 172 long m1 = t2 + (t3 - t2) / 2L; 173 if (m0 < m1) { 174 return -1; 175 } 176 else if (m0 > m1) { 177 return 1; 178 } 179 else { 180 if (t0 < t2) { 181 return -1; 182 } 183 else if (t0 > t2) { 184 return 1; 185 } 186 else { 187 if (t1 < t3) { 188 return -1; 189 } 190 else if (t1 > t3) { 191 return 1; 192 } 193 else { 194 return 0; 195 } 196 } 197 } 198 } 199 200 /** 201 * Returns a hash code for this object instance. The approach described by 202 * Joshua Bloch in "Effective Java" has been used here - see: 203 * <p> 204 * {@code http://developer.java.sun.com/ 205 * developer/Books/effectivejava/Chapter3.pdf} 206 * 207 * @return A hash code. 208 */ 209 @Override 210 public int hashCode() { 211 int result = 17; 212 result = 37 * result + (int) this.start; 213 result = 37 * result + (int) this.end; 214 return result; 215 } 216 217}