Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4aabb01fbed9203dced05cc2d4da2fa3515767d6 (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylar.tasks.core;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Rob Elves
 * @author Mik Kersten
 */
public class DateRangeContainer extends AbstractTaskContainer {

	private Set<DateRangeActivityDelegate> dateRangeDelegates = new HashSet<DateRangeActivityDelegate>();

	private Map<DateRangeActivityDelegate, Long> taskToDuration = new HashMap<DateRangeActivityDelegate, Long>();

	private Calendar startDate;

	private Calendar endDate;

	private long totalElapsed = 0;

	private long totalEstimated = 0;

	public DateRangeContainer(GregorianCalendar startDate, GregorianCalendar endDate, String description) {
		super(description);
		this.startDate = startDate;
		this.endDate = endDate;
	}

	public DateRangeContainer(Calendar startDate, Calendar endDate, String description) {
		super(description);
		this.startDate = startDate;
		this.endDate = endDate;
	}

	public DateRangeContainer(GregorianCalendar startDate, GregorianCalendar endDate) {
		super(DateFormat.getDateInstance(DateFormat.FULL).format(startDate.getTime()) + " to "
				+ DateFormat.getDateInstance(DateFormat.FULL).format(endDate.getTime()));
		// super(startDate.hashCode() + endDate.hashCode() + "");
		// String start =
		// DateFormat.getDateInstance(DateFormat.FULL).format(startDate.getTime());
		// String end =
		// DateFormat.getDateInstance(DateFormat.FULL).format(endDate.getTime());
		// this.description = start + " to " + end;
		this.startDate = startDate;
		this.endDate = endDate;
	}

	public DateRangeContainer(Date time, Date time2, String description) {
		super(description);
		startDate = new GregorianCalendar();
		startDate.setTime(time);
		endDate = new GregorianCalendar();
		endDate.setTime(time2);
		// this.description = summary;
	}

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

	public void clear() {
		totalEstimated = 0;
		totalElapsed = 0;
		taskToDuration.clear();
		dateRangeDelegates.clear();
		super.clear();
	}

	public void addTask(DateRangeActivityDelegate taskWrapper) {
		long taskActivity = taskWrapper.getActivity();
		if (taskActivity < 0)
			taskActivity = 0;
		totalElapsed += taskActivity;
		dateRangeDelegates.remove(taskWrapper);
		dateRangeDelegates.add(taskWrapper);
		if (taskToDuration.containsKey(taskWrapper)) {
			long previous = taskToDuration.get(taskWrapper);
			long newDuration = previous + taskActivity;
			taskToDuration.put(taskWrapper, newDuration);
		} else {
			taskToDuration.put(taskWrapper, taskActivity);
		}
		super.add(taskWrapper.getCorrespondingTask());
	}

	public void remove(DateRangeActivityDelegate taskWrapper) {
		dateRangeDelegates.remove(taskWrapper);
		super.remove(taskWrapper.getCorrespondingTask());
	}

	public Calendar getStart() {
		return startDate;
	}

	public Calendar getEnd() {
		return endDate;
	}

	public long getTotalElapsed() {
		return totalElapsed;
	}

	public long getElapsed(DateRangeActivityDelegate taskWrapper) {
		if (taskToDuration.containsKey(taskWrapper)) {
			return taskToDuration.get(taskWrapper);
		} else {
			return 0;
		}
	}

	public long getTotalEstimated() {
		totalEstimated = 0;
		for (ITask task : dateRangeDelegates) {
			totalEstimated += task.getEstimateTimeHours();
		}
		return totalEstimated;
	}

	public boolean isArchive() {
		return false;
	}

	public void setIsArchive(boolean isArchive) {
		// ignore
	}

	public String getPriority() {
		return "";
	}

	@Override
	public void setHandleIdentifier(String id) {
		// ignore
	}

	public Set<DateRangeActivityDelegate> getDateRangeDelegates() {
		return dateRangeDelegates;
	}

	public boolean isFuture() {
		return !isPresent() && getStart().after(Calendar.getInstance());
	}

	public boolean isPresent() {
		return getStart().before(Calendar.getInstance()) && getEnd().after(Calendar.getInstance());
	}

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

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

	@Override
	public boolean isLocal() {
		return true;
	}

	/**
	 * The handle for most containers is their summary. Override to specify a
	 * different natural ordering.
	 */
	@Override
	public int compareTo(ITaskListElement taskListElement) {
		return startDate.compareTo(((DateRangeContainer) taskListElement).startDate);
	}
}

Back to the top