KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > usertasks > translators > SpentTimesUserTaskProcessor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Code is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.usertasks.translators;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Calendar JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.modules.tasklist.usertasks.model.UserTask;
29 import org.netbeans.modules.tasklist.usertasks.model.UserTask.WorkPeriod;
30 import org.netbeans.modules.tasklist.usertasks.model.UserTaskList;
31 import org.netbeans.modules.tasklist.usertasks.util.UnaryFunction;
32
33 /**
34  * Summarizes spent times.
35  *
36  * @author tl
37  */

38 public class SpentTimesUserTaskProcessor implements UnaryFunction {
39     private static int[] FIELDS = {
40         Calendar.DAY_OF_YEAR,
41         Calendar.WEEK_OF_YEAR,
42         Calendar.MONTH,
43         Calendar.MONTH,
44         Calendar.YEAR
45     };
46     private static int[] FIELD_ADDS = {
47         1,
48         1,
49         1,
50         3,
51         1
52     };
53             
54     private Date JavaDoc from;
55     private Date JavaDoc to;
56     private int minDuration;
57     private HistoryOptionsPanel.Group group;
58     
59     private Comparator JavaDoc comp;
60     private List JavaDoc<long[]> durations = new ArrayList JavaDoc<long[]>();
61     private List JavaDoc<Date JavaDoc> periods = new ArrayList JavaDoc<Date JavaDoc>();
62             
63     /**
64      * Creates a new instance of SpentTimesUserTaskProcessor.
65      *
66      * @param from start date
67      * @param to end date
68      * @param group grouping
69      * @param minDuration minimal task duration
70      */

71     public SpentTimesUserTaskProcessor(Date JavaDoc from, Date JavaDoc to,
72             HistoryOptionsPanel.Group group, int minDuration) {
73         this.from = from;
74         this.to = to;
75         this.group = group;
76         this.minDuration = minDuration;
77         
78         int field = FIELDS[group.ordinal()];
79         int add = FIELD_ADDS[group.ordinal()];
80         Calendar JavaDoc c = Calendar.getInstance();
81         c.setTime(from);
82         switch (group) {
83             case DAILY:
84                 break;
85             case WEEKLY:
86                 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
87                 break;
88             case MONTHLY:
89                 c.set(Calendar.DAY_OF_MONTH, 1);
90                 break;
91             case QUARTERLY:
92                 c.set(Calendar.MONTH, c.get(Calendar.MONTH) / 3 * 3);
93                 c.set(Calendar.DAY_OF_MONTH, 1);
94                 break;
95             case YEARLY:
96                 c.set(Calendar.DAY_OF_YEAR, 1);
97                 break;
98         }
99         
100         while (c.getTime().compareTo(to) < 0) {
101             periods.add(c.getTime());
102             c.add(field, add);
103         }
104         periods.add(c.getTime());
105     }
106
107     /**
108      * Returns periods.
109      *
110      * @return array of dates. This array is one element longer than
111      * returned by getTasks
112      */

113     public List JavaDoc<Date JavaDoc> getPeriods() {
114         return periods;
115     }
116     
117     /**
118      * Returns spent times.
119      *
120      * @return spent times (milliseconds)
121      */

122     public List JavaDoc<long[]> getDurations() {
123         return durations;
124     }
125     
126     /**
127      * Computes common part of 2 periods.
128      *
129      * @param start start of the first period
130      * @param end end of the first period
131      * @param start2 start of the second period
132      * @param end2 end of the second period
133      */

134     private long overlap(long start, long end, long start2, long end2) {
135         if (start2 >= end || end2 <= start)
136             return 0;
137         
138         return Math.min(end, end2) - Math.max(start, start2);
139     }
140
141     public Object JavaDoc compute(Object JavaDoc obj) {
142         UserTaskInfo info = (UserTaskInfo) obj;
143         info.spentTimes = new long[periods.size() - 1];
144         if ((info.object instanceof UserTask) &&
145                 !((UserTask) info.object).isValuesComputed()) {
146             for (WorkPeriod wp: ((UserTask) info.object).getWorkPeriods()) {
147                 for (int i = 0; i < periods.size() - 1; i++) {
148                     long ov = overlap(wp.getStart(), wp.getStart() +
149                             wp.getDuration() * 60L * 1000,
150                             periods.get(i).getTime(),
151                             periods.get(i + 1).getTime());
152                     if (ov != 0) {
153                         info.spentTimes[i] += ov;
154                     }
155                 }
156             }
157         } else {
158             for (UserTaskInfo ch: info.children) {
159                 for (int i = 0; i < info.spentTimes.length; i++) {
160                     info.spentTimes[i] += ch.spentTimes[i];
161                 }
162             }
163         }
164         return null;
165     }
166 }
167
Popular Tags