| 1 |
/* |
| 2 |
* @(#)TimeRange.java |
| 3 |
* |
| 4 |
* 2002 - 2012 JIDE Software Incorporated. All rights reserved. |
| 5 |
* Copyright (c) 2005 - 2012 Catalysoft Limited. All rights reserved. |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.jidesoft.range; |
| 9 |
|
| 10 |
import com.jidesoft.swing.JideSwingUtilities; |
| 11 |
|
| 12 |
import java.text.DateFormat; |
| 13 |
import java.util.Calendar; |
| 14 |
import java.util.Date; |
| 15 |
import java.util.TimeZone; |
| 16 |
|
| 17 |
|
| 18 |
/** |
| 19 |
* Specifies upper and lower bounds for a range of values along a time line. |
| 20 |
* |
| 21 |
* @author Simon White ([email protected]) |
| 22 |
*/ |
| 23 |
public class TimeRange extends AbstractRange<Date> { |
| 24 |
protected Date _min; |
| 25 |
protected Date _max; |
| 26 |
|
| 27 |
/** |
| 28 |
* The time zone used when formatting time/date strings for output For example, to see new Date(0) formatted as |
| 29 |
* 00:00:00 on Jan 1, 1970 (instead of 01:00 GMT for the UK), set the TimeZone to be TimeZone.getTimeZone("UTC"); |
| 30 |
*/ |
| 31 |
private TimeZone timeZone = TimeZone.getDefault(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Sets a default time range of the whole of today |
| 35 |
*/ |
| 36 |
public TimeRange() { |
| 37 |
Calendar today = Calendar.getInstance(); |
| 38 |
today.set(Calendar.HOUR_OF_DAY, 0); |
| 39 |
today.set(Calendar.MINUTE, 0); |
| 40 |
today.set(Calendar.SECOND, 0); |
| 41 |
today.set(Calendar.MILLISECOND, 0); |
| 42 |
long todayLong = today.getTimeInMillis(); |
| 43 |
today.add(Calendar.HOUR, 24); |
| 44 |
long tomorrowLong = today.getTimeInMillis(); |
| 45 |
_min = new Date(todayLong); |
| 46 |
_max = new Date(tomorrowLong); |
| 47 |
} |
| 48 |
|
| 49 |
public TimeRange(Calendar from, Calendar to) { |
| 50 |
_min = from.getTime(); |
| 51 |
_max = to.getTime(); |
| 52 |
assert (from.before(to)); |
| 53 |
} |
| 54 |
|
| 55 |
public TimeRange(Date from, Date to) { |
| 56 |
_min = from; |
| 57 |
_max = to; |
| 58 |
} |
| 59 |
|
| 60 |
public TimeRange(long from, long to) { |
| 61 |
_min = new Date(from); |
| 62 |
_max = new Date(to); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Constructs a copy of the supplied time range |
| 67 |
* @param timeRange the timeRange to copy |
| 68 |
*/ |
| 69 |
public TimeRange(TimeRange timeRange) { |
| 70 |
this((long) timeRange.minimum(), (long) timeRange.maximum()); |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public Range<Date> copy() { |
| 75 |
return new TimeRange(this); |
| 76 |
} |
| 77 |
|
| 78 |
public void setMin(Date from) { |
| 79 |
Date oldValue = _min; |
| 80 |
_min = from; |
| 81 |
firePropertyChange(PROPERTY_MIN, oldValue, _min); |
| 82 |
} |
| 83 |
|
| 84 |
public void setMin(long from) { |
| 85 |
Date oldValue = _min; |
| 86 |
_min = new Date(from); |
| 87 |
firePropertyChange(PROPERTY_MIN, oldValue, _min); |
| 88 |
} |
| 89 |
|
| 90 |
public void adjust(Date lower, Date upper) { |
| 91 |
double size = size(); // save it |
| 92 |
if (lower != null) { |
| 93 |
setMin(lower); |
| 94 |
} |
| 95 |
else if (upper != null) { |
| 96 |
setMin(new Date(upper.getTime() - (long) size)); |
| 97 |
} |
| 98 |
if (upper != null) { |
| 99 |
setMax(upper); |
| 100 |
} |
| 101 |
else if (lower != null) { |
| 102 |
setMax(new Date(lower.getTime() + (long) size)); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
public void setMax(Date to) { |
| 107 |
Date oldValue = _max; |
| 108 |
_max = to; |
| 109 |
firePropertyChange(PROPERTY_MAX, oldValue, _max); |
| 110 |
} |
| 111 |
|
| 112 |
public void setMax(long to) { |
| 113 |
Date oldValue = _max; |
| 114 |
_max = new Date(to); |
| 115 |
firePropertyChange(PROPERTY_MAX, oldValue, _max); |
| 116 |
} |
| 117 |
|
| 118 |
public double minimum() { |
| 119 |
return _min == null ? Double.MIN_VALUE : _min.getTime(); |
| 120 |
} |
| 121 |
|
| 122 |
public double maximum() { |
| 123 |
return _max == null ? Double.MAX_VALUE : _max.getTime(); |
| 124 |
} |
| 125 |
|
| 126 |
public double size() { |
| 127 |
return maximum() - minimum(); |
| 128 |
} |
| 129 |
|
| 130 |
public Date lower() { |
| 131 |
return _min; |
| 132 |
} |
| 133 |
|
| 134 |
public Date upper() { |
| 135 |
return _max; |
| 136 |
} |
| 137 |
|
| 138 |
public boolean contains(Date x) { |
| 139 |
if (x == null) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
else { |
| 143 |
long value = x.getTime(); |
| 144 |
return value >= minimum() && value <= maximum(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
public TimeZone getTimeZone() { |
| 149 |
return timeZone; |
| 150 |
} |
| 151 |
|
| 152 |
public void setTimeZone(TimeZone timeZone) { |
| 153 |
this.timeZone = timeZone; |
| 154 |
} |
| 155 |
|
| 156 |
public Range<Date> createIntermediate(Range<Date> target, double position) { |
| 157 |
double sourceMin = this.minimum(); |
| 158 |
double sourceMax = this.maximum(); |
| 159 |
double targetMin = target.minimum(); |
| 160 |
double targetMax = target.maximum(); |
| 161 |
double min = sourceMin + position * (targetMin - sourceMin); |
| 162 |
double max= sourceMax + position * (targetMax - sourceMax); |
| 163 |
return new TimeRange((long) min, (long) max); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Creates a new TimeRange as the union of two existing TimeRanges. The date format of the first time range is |
| 168 |
* retained in the returned result. |
| 169 |
* |
| 170 |
* @param r1 the first TimeRange |
| 171 |
* @param r2 the second TimeRange |
| 172 |
* @return the union of the supplied TimeRanges |
| 173 |
*/ |
| 174 |
public static TimeRange union(TimeRange r1, TimeRange r2) { |
| 175 |
if (r1 == null) { |
| 176 |
return r2; |
| 177 |
} |
| 178 |
else if (r2 == null) { |
| 179 |
return r1; |
| 180 |
} |
| 181 |
long r1Min = r1._min == null ? Long.MAX_VALUE : r1._min.getTime(); |
| 182 |
long r2Min = r2._min == null ? Long.MAX_VALUE : r2._min.getTime(); |
| 183 |
long r1Max = r1._max == null ? Long.MIN_VALUE : r1._max.getTime(); |
| 184 |
long r2Max = r2._max == null ? Long.MIN_VALUE : r2._max.getTime(); |
| 185 |
|
| 186 |
long min = Math.min(r1Min, r2Min); |
| 187 |
long max = Math.max(r1Max, r2Max); |
| 188 |
return new TimeRange(min, max); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
@Override |
| 193 |
public boolean equals(Object other) { |
| 194 |
if (other instanceof TimeRange) { |
| 195 |
TimeRange otherRange = (TimeRange) other; |
| 196 |
return JideSwingUtilities.equals(_min, otherRange._min) && JideSwingUtilities.equals(_max, otherRange._max); |
| 197 |
} |
| 198 |
else { |
| 199 |
return false; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public int hashCode() { |
| 205 |
return (_max == null ? 0 : _max.hashCode()) * 29 + (_min == null ? 0 : _min.hashCode()); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public String toString() { |
| 210 |
DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); |
| 211 |
return String.format("[%s, %s] rounded to [%s, %s]", _min == null ? "null" : f.format(_min), _max == null ? "null" : f.format(_max), _min == null ? "null" : f.format(minimum()), _max == null ? "null" : f.format(maximum())); |
| 212 |
} |
| 213 |
} |