Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7d3ca39e81a029ed8cd4576f6c0a9620620a41f7 (plain) (blame)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core;

import java.text.DateFormat;
import java.util.Calendar;

import org.eclipse.core.runtime.Assert;

/**
 * @author Rob Elves
 * @since 3.0
 */
public class DateRange implements Comparable<DateRange> {

	private static final long DAY = 1000 * 60 * 60 * 24;

	private static final String DESCRIPTION_PREVIOUS_WEEK = "Previous Week";

	private static final String DESCRIPTION_THIS_WEEK = "Someday This Week";

	private static final String DESCRIPTION_NEXT_WEEK = "Someday Next Week";

	private static final String DESCRIPTION_WEEK_AFTER_NEXT = "Two Weeks";

	private final Calendar startDate;

	private final Calendar endDate;

	/**
	 * create an instance of a date range that represents a finite point in time
	 */
	public DateRange(Calendar time) {
		startDate = time;
		endDate = time;
	}

	public DateRange(Calendar startDate, Calendar endDate) {
		Assert.isNotNull(startDate);
		Assert.isNotNull(endDate);
		this.startDate = startDate;
		this.endDate = endDate;
	}

	public boolean includes(DateRange range) {
		return (startDate.getTimeInMillis() <= range.getStartDate().getTimeInMillis())
				&& (endDate.getTimeInMillis() >= range.getEndDate().getTimeInMillis());
	}

	public boolean includes(Calendar cal) {
		return (startDate.getTimeInMillis() <= cal.getTimeInMillis())
				&& (endDate.getTimeInMillis() >= cal.getTimeInMillis());
	}

	public Calendar getStartDate() {
		return startDate;
	}

	public Calendar getEndDate() {
		return endDate;
	}

	/**
	 * TODO: Move into label provider
	 */
	@Override
	public String toString() {
		return toString(true);
	}

	public String toString(boolean useDayOfWeekForNextWeek) {
		boolean isThisWeek = TaskActivityUtil.getCurrentWeek().includes(this);
		Calendar endNextWeek = TaskActivityUtil.getCalendar();
		endNextWeek.add(Calendar.DAY_OF_YEAR, 7);
		boolean isNextWeek = TaskActivityUtil.getNextWeek().includes(this) && this.before(endNextWeek);
		if (isDay() && (isThisWeek || (useDayOfWeekForNextWeek && isNextWeek))) {
			String day = "";
			switch (getStartDate().get(Calendar.DAY_OF_WEEK)) {
			case Calendar.MONDAY:
				day = "Monday";
				break;
			case Calendar.TUESDAY:
				day = "Tuesday";
				break;
			case Calendar.WEDNESDAY:
				day = "Wednesday";
				break;
			case Calendar.THURSDAY:
				day = "Thursday";
				break;
			case Calendar.FRIDAY:
				day = "Friday";
				break;
			case Calendar.SATURDAY:
				day = "Saturday";
				break;
			case Calendar.SUNDAY:
				day = "Sunday";
				break;
			}
			if (Calendar.getInstance().get(Calendar.DAY_OF_WEEK) == getStartDate().get(Calendar.DAY_OF_WEEK)) {
				return day + " - Today";
			} else {
				return day;
			}
		} else if (isThisWeek()) {
			return DESCRIPTION_THIS_WEEK;
		} else if (isNextWeek()) {
			return DESCRIPTION_NEXT_WEEK;
		} else if (isWeekAfterNext()) {
			return DESCRIPTION_WEEK_AFTER_NEXT;
		} else if (isPreviousWeek()) {
			return DESCRIPTION_PREVIOUS_WEEK;
		}
		return DateFormat.getDateInstance(DateFormat.MEDIUM).format(startDate.getTime());
		/* + " to "+ DateFormat.getDateInstance(DateFormat.MEDIUM).format(endDate.getTime());*/
	}

	private boolean isWeekAfterNext() {
		return TaskActivityUtil.getCurrentWeek().next().next().compareTo(this) == 0;
	}

	public DateRange next() {
		if (isDay()) {
			return create(Calendar.DAY_OF_YEAR, 1);
		} else if (isWeek()) {
			return create(Calendar.WEEK_OF_YEAR, 1);
		}
		return null;
	}

	public DateRange previous() {
		if (isDay()) {
			return create(Calendar.DAY_OF_YEAR, -1);
		} else if (isWeek()) {
			return create(Calendar.WEEK_OF_YEAR, -1);
		}
		return null;
	}

	private DateRange create(int field, int multiplier) {
		Calendar previousStart = (Calendar) getStartDate().clone();
		Calendar previousEnd = (Calendar) getEndDate().clone();
		previousStart.add(field, 1 * multiplier);
		previousEnd.add(field, 1 * multiplier);
		return new DateRange(previousStart, previousEnd);
	}

	private boolean isNextWeek() {
		return TaskActivityUtil.getCurrentWeek().next().compareTo(this) == 0;
	}

	public boolean isThisWeek() {
		if (isWeek()) {
			return this.includes(Calendar.getInstance());
		}
		return false;
	}

	private boolean isPreviousWeek() {
		if (isWeek()) {
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.WEEK_OF_YEAR, -1);
			return this.includes(cal);
		}
		return false;
	}

	public boolean isDay() {
		return ((getEndDate().getTimeInMillis() - getStartDate().getTimeInMillis()) == DAY - 1);
	}

	public boolean isWeek() {
		return ((getEndDate().getTimeInMillis() - getStartDate().getTimeInMillis()) == (DAY * 7) - 1);
	}

	public boolean isPast() {
		return getEndDate().compareTo(Calendar.getInstance()) < 0;
	}

	public boolean isBefore(DateRange scheduledDate) {
		return this.getEndDate().compareTo(scheduledDate.getStartDate()) < 0;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((endDate == null) ? 0 : endDate.hashCode());
		result = prime * result + ((startDate == null) ? 0 : startDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof DateRange)) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
//		if (getClass() != obj.getClass()) {
//			return false;
//		}
		DateRange other = (DateRange) obj;
		if (endDate == null) {
			if (other.endDate != null) {
				return false;
			}
		} else if (!endDate.equals(other.endDate)) {
			return false;
		}
		if (startDate == null) {
			if (other.startDate != null) {
				return false;
			}
		} else if (!startDate.equals(other.startDate)) {
			return false;
		}
		return true;
	}

	public boolean before(Calendar cal) {
		return getEndDate().before(cal);
	}

	public boolean after(Calendar cal) {
		return cal.before(getEndDate());
	}

	public int compareTo(DateRange range) {
		if (range.getStartDate().equals(startDate) && range.getEndDate().equals(endDate)) {
			return 0;
		} else if (includes(range)) {
			return 1;
		} else if (before(range.getStartDate())) {
			return -1;
		} else if (after(range.getEndDate())) {
			return 1;
		}
		return -1;
	}

}

Back to the top