forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeRange.java
More file actions
145 lines (131 loc) · 4.01 KB
/
Copy pathDateTimeRange.java
File metadata and controls
145 lines (131 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright (c) 2006 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.gbase.client;
import com.google.gdata.data.DateTime;
/**
* A date or date/time range, with a start and end {@link DateTime}.
*
* Since <code>dateTimeRange</code> is a superclass of <code>date</code>
* and <code>dateTime</code>, a DateTimeRange object can sometimes be
* used to represent a single date or dateTime. When this is the case,
* the start and end value will be identical and {@link #isDateTimeOnly()}
* will return true. You can then get the {@link DateTime} the 'range'
* corresponds to using {@link #toDateTime()}.
*/
public class DateTimeRange {
/**
* The start date/time of this range.
*/
private final DateTime start;
/**
* The end date/time of this range.
*/
private final DateTime end;
/**
* Creates a new range.
*
* @param start
* @param end
* @exception IllegalArgumentException if either start or end is null
*/
public DateTimeRange(DateTime start, DateTime end) {
assertArgumentNotNull(start, "start");
assertArgumentNotNull(end, "end");
this.start = start;
this.end = end;
}
/**
* Creates a new range that is actually a date or a dateTime.
*
* Empty ranges (with start date the same as the end dates) can
* be detected using {@link #isDateTimeOnly()} and then converted
* using {@link #toDateTime()}.
*
* @param dateTime
*/
public DateTimeRange(DateTime dateTime) {
this(dateTime, dateTime);
}
/**
* Checks whether this is a real range, with a start and an
* end date, or a date/dateTime posing as a range.
*
* This hack is necessary to handle treating DateTime as
* a DateTimeRange, because dateTimeRange is a supertype of
* date and dateTime.
*
* If this method returns true, you can get the {@link DateTime}
* object this object corresponds to using {@link #toDateTime()}
*
* @return true if start is the same as end
*/
public boolean isDateTimeOnly() {
return start.equals(end);
}
/**
* Converts empty ranges into {@link DateTime}.
*
* @return a DateTime object
* @exception IllegalStateException if the start and end date are
* distinct, which means the dateTimeRange is neither a date
* nor a dateTime (see {@link #isDateTimeOnly()})
*/
public DateTime toDateTime() {
if (!isDateTimeOnly()) {
throw new IllegalStateException("This is a valid range, with distinct " +
"start and end date. It cannot be converted to one DateTime value. " +
"(Check with isDateTimeOnly() first): " + this);
}
return start;
}
private void assertArgumentNotNull(Object object, String name) {
if (object == null) {
throw new NullPointerException(name + " should not be null");
}
}
/**
* Gets the start date or date+time.
*/
public DateTime getStart() {
return start;
}
/**
* Gets the end date or date/time.
*/
public DateTime getEnd() {
return end;
}
/**
* Returns the canonical string representation for a range, as used
* in the XML (start " " end).
*/
@Override
public String toString() {
return start + " " + end;
}
@Override
public int hashCode() {
return 37 * start.hashCode() + end.hashCode();
}
@Override
public boolean equals(Object o) {
if (!(o instanceof DateTimeRange)) {
return false;
} else {
DateTimeRange other = (DateTimeRange)o;
return other.start.equals(start) && other.end.equals(end);
}
}
}