KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > taskblocks > Utils


1 /*
2  * Copyright (C) Jakub Neubauer, 2007
3  *
4  * This file is part of TaskBlocks
5  *
6  * TaskBlocks is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * TaskBlocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */

19
20 package taskblocks;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 public class Utils {
30
31     public static final long MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;
32
33     /** The first saturday since the Epoch (time 0). */
34     public static final long FIRST_SATURDAY = 1;
35     
36     /**
37      * This method adds 'duration' working days to 'startTime'. Saturday is also the regular
38      * end of the work. (This is different from beginning of work - there saturday should be recounted
39      * to monday
40      */

41     public static long countFinishTime(long startTime, long duration) {
42         long startDayInWeek = getDayInWeek(startTime);
43         
44         long durationWeeks = (duration) / 5;
45         long durationRest = (duration) % 5;
46         
47         long daysAdd = durationWeeks*2; // every week of work means 2 days of weekend
48

49         if(startDayInWeek + durationRest > 5) {
50             daysAdd += 2;
51         }
52         
53         if(duration > 0 && startDayInWeek == 0 && duration % 5 == 0) {
54             daysAdd-=2;
55         }
56         return startTime + duration + daysAdd;
57     }
58     
59     /** Returns day in week for given time. Time is the count of days, not milliseconds
60      *
61      * @param time Time in number of days.
62      * @return
63      */

64     public static int getDayInWeek(long time) {
65         return (int)((time + FIRST_SATURDAY + 2) % 7);
66     }
67     
68     public static long repairStartTime(long startTime) {
69         long startDayInWeek = Utils.getDayInWeek(startTime);
70         if(startDayInWeek == 5) {
71             return startTime+2;
72         } else if(startDayInWeek == 6) {
73             return startTime+1;
74         }
75         return startTime;
76     }
77     
78     /** Counts duration between given times, counting only working days */
79     public static long countWorkDuration(long start, long end) {
80         long result;
81         long weeks = (end-start) / 7;
82         result = weeks * 5;
83         long rest = (end-start) % 7;
84         int startInWeek = getDayInWeek(start);
85         for(int i = 1; i <= rest; i++) {
86             long stepInWeek = (startInWeek + i) % 7;
87             if(stepInWeek != 0 && stepInWeek != 6) {
88                 result ++;
89             }
90         }
91         return result;
92     }
93
94     public static Element JavaDoc[] getChilds(Element JavaDoc e, String JavaDoc name) {
95         List JavaDoc<Element JavaDoc>childs = new ArrayList JavaDoc<Element JavaDoc>();
96         NodeList JavaDoc nl = e.getChildNodes();
97         for(int i = 0; i < nl.getLength(); i++) {
98             Node JavaDoc n = nl.item(i);
99             if(n.getNodeType() == Node.ELEMENT_NODE && name.equals(n.getNodeName())) {
100                 childs.add((Element JavaDoc)n);
101             }
102         }
103         return childs.toArray(new Element JavaDoc[childs.size()]);
104     }
105     
106     /**
107      * Returns text enclosed in the first child element of the parent element
108      *
109      * @param parent
110      * @param childName
111      * @return
112      */

113     public static String JavaDoc getFirstElemText(Element JavaDoc parent, String JavaDoc childName) {
114         return null; // TODO
115
}
116
117 }
118
Popular Tags