Source Code Cross Referenced for Duration.java in  » Project-Management » OpenProj » com » projity » datatype » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Project Management » OpenProj » com.projity.datatype 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:        The contents of this file are subject to the Common Public Attribution License 
003:        Version 1.0 (the "License"); you may not use this file except in compliance with 
004:        the License. You may obtain a copy of the License at 
005:        http://www.projity.com/license . The License is based on the Mozilla Public 
006:        License Version 1.1 but Sections 14 and 15 have been added to cover use of 
007:        software over a computer network and provide for limited attribution for the 
008:        Original Developer. In addition, Exhibit A has been modified to be consistent 
009:        with Exhibit B.
010:
011:        Software distributed under the License is distributed on an "AS IS" basis, 
012:        WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
013:        specific language governing rights and limitations under the License. The 
014:        Original Code is OpenProj. The Original Developer is the Initial Developer and 
015:        is Projity, Inc. All portions of the code written by Projity are Copyright (c) 
016:        2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018:        Alternatively, the contents of this file may be used under the terms of the 
019:        Projity End-User License Agreeement (the Projity License), in which case the 
020:        provisions of the Projity License are applicable instead of those above. If you 
021:        wish to allow use of your version of this file only under the terms of the 
022:        Projity License and not to allow others to use your version of this file under 
023:        the CPAL, indicate your decision by deleting the provisions above and replace 
024:        them with the notice and other provisions required by the Projity  License. If 
025:        you do not delete the provisions above, a recipient may use your version of this 
026:        file under either the CPAL or the Projity License.
027:
028:        [NOTE: The text of this license may differ slightly from the text of the notices 
029:        in Exhibits A and B of the license at http://www.projity.com/license. You should 
030:        use the latest text at http://www.projity.com/license for your modifications.
031:        You may not remove this license text from the source files.]
032:
033:        Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007 
034:        Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj, 
035:        an open source solution from Projity. Attribution URL: http://www.projity.com 
036:        Graphic Image as provided in the Covered Code as file:  openproj_logo.png with 
037:        alternatives listed on http://www.projity.com/logo
038:
039:        Display of Attribution Information is required in Larger Works which are defined 
040:        in the CPAL as a work which combines Covered Code or portions thereof with code 
041:        not governed by the terms of the CPAL. However, in addition to the other notice 
042:        obligations, all copies of the Covered Code in Executable and Source Code form 
043:        distributed must, as a form of attribution of the original author, include on 
044:        each user interface screen the "OpenProj" logo visible to all users.  The 
045:        OpenProj logo should be located horizontally aligned with the menu bar and left 
046:        justified on the top left of the screen adjacent to the File menu.  The logo 
047:        must be at least 100 x 25 pixels.  When users click on the "OpenProj" logo it 
048:        must direct them back to http://www.projity.com.  
049:         */
050:        package com.projity.datatype;
051:
052:        import com.projity.options.CalendarOption;
053:        import com.projity.options.ScheduleOption;
054:        import com.projity.util.MathUtils;
055:
056:        /**
057:         * Instead of creating a true Duration type, I use unused bits of the long which stores durations.
058:         * The idea is that the algorithms will run faster because there is no object churn and fewer function
059:         * calls.
060:         */
061:        public class Duration extends Number implements  Comparable {
062:            private static final long serialVersionUID = 1489291902577173002L;
063:            private long encodedMillis;
064:            protected boolean work = false;
065:
066:            public static final long MAX_DURATION = 10 * 365 * 8 * 60 * 60
067:                    * 1000L; // biggest possible value
068:
069:            private Duration(Double doubleObject) {
070:                this (doubleObject.longValue());
071:            }
072:
073:            public Duration(long encodedMillis) {
074:                this .encodedMillis = encodedMillis;
075:            }
076:
077:            public String toString() {
078:                return DurationFormat.getInstance().format(this );
079:            }
080:
081:            public boolean equals(Object arg0) {
082:                if (arg0 != null && arg0 instanceof  Duration)
083:                    return encodedMillis == ((Duration) arg0).encodedMillis;
084:                else
085:                    return false;
086:            }
087:
088:            public int compareTo(Object arg0) {
089:                if (arg0 == null)
090:                    throw new NullPointerException();
091:                if (!(arg0 instanceof  Duration))
092:                    throw new ClassCastException();
093:
094:                return MathUtils.signum(getValue(encodedMillis)
095:                        - getValue(((Duration) arg0).getEncodedMillis()));
096:            }
097:
098:            public long getEncodedMillis() {
099:                return encodedMillis;
100:            }
101:
102:            public void setEncodedMillis(long encodedMillis) {
103:                this .encodedMillis = encodedMillis;
104:            }
105:
106:            public static final Duration ZERO = new Duration(0);
107:
108:            public void setWork(boolean work) {
109:                this .work = work;
110:                //TODO figure out why lines below don't work when formatting
111:                //		if (work)
112:                //			encodedMillis = Duration.setAsTimeUnit(encodedMillis,ScheduleOption.getInstance().getWorkUnit());
113:            }
114:
115:            public boolean isWork() {
116:                return work;
117:            }
118:
119:            private static int SHIFT = 57; // 6 bits are used: bits 62-57 (bit 63 is the sign bit and is not used
120:            private static long ESTIMATED_BIT = 0x20L << SHIFT; // 1<< 62
121:            private static long ELAPSED_BIT = 0x10L << SHIFT;
122:            private static long PERCENT_BIT = 0x0fL << SHIFT;
123:            private static long YEARS_BIT = 0x0eL << SHIFT;
124:            private static long MONTHS_BIT = 0x0dL << SHIFT;
125:            private static long WEEKS_BIT = 0x0cL << SHIFT;
126:            private static long DAYS_BIT = 0x0bL << SHIFT;
127:            private static long HOURS_BIT = 0x0aL << SHIFT;
128:            private static long MINUTES_BIT = 0x09L << SHIFT;
129:            private static long SECONDS_BIT = 0x08L << SHIFT;
130:            private static long NON_TEMPORAL_BIT = 0x07L << SHIFT;
131:
132:            private static long UNITS_MASK = 0x0fL << SHIFT;
133:            private static long ELAPSED_AND_UNITS_MASK = (0x1fL << SHIFT);
134:            private static long MILLIS_MASK = ~(0x3fL << SHIFT);
135:
136:            public static long clear(long duration) {
137:                return duration & ELAPSED_AND_UNITS_MASK;
138:            }
139:
140:            public static boolean isZero(long duration) {
141:                return millis(duration) == 0L;
142:            }
143:
144:            private static long getBits(long flags, long duration) {
145:                if (duration < 0)
146:                    duration = -duration;
147:                return (duration & flags);
148:            }
149:
150:            private static long setBits(long flags, long duration) {
151:                boolean negative = (duration < 0);
152:                if (negative)
153:                    duration = -duration;
154:                duration |= flags;
155:                if (negative)
156:                    duration = -duration;
157:                return duration;
158:            }
159:
160:            private static long clearBits(long flags, long duration) {
161:                boolean negative = (duration < 0);
162:                if (negative)
163:                    duration = -duration;
164:                duration &= ~flags;
165:                if (negative)
166:                    duration = -duration;
167:                return duration;
168:            }
169:
170:            private static long setBits(long duration, long clearThese,
171:                    long setThese) {
172:                boolean negative = (duration < 0);
173:                if (negative)
174:                    duration = -duration;
175:                long unitsOnly = duration & (~clearThese);
176:                duration = unitsOnly | setThese;
177:                if (negative)
178:                    duration = -duration;
179:                return duration;
180:            }
181:
182:            public static boolean isElapsed(long duration) {
183:                return getBits(ELAPSED_BIT, duration) == ELAPSED_BIT;
184:            }
185:
186:            public static long setAsElapsed(long duration) {
187:                return setBits(duration, ELAPSED_BIT, ELAPSED_BIT);
188:            }
189:
190:            public static boolean isEstimated(long duration) {
191:                return getBits(ESTIMATED_BIT, duration) == ESTIMATED_BIT;
192:            }
193:
194:            public static long setAsEstimated(long duration, boolean estimated) {
195:                if (estimated)
196:                    return setBits(ESTIMATED_BIT, duration);
197:                else
198:                    return clearBits(ESTIMATED_BIT, duration);
199:            }
200:
201:            public static boolean hasUnits(long duration) {
202:                return getBits(UNITS_MASK, duration) != 0;
203:            }
204:
205:            public static long millis(long duration) {
206:                boolean negative = (duration < 0);
207:                if (negative)
208:                    duration = -duration;
209:                duration &= MILLIS_MASK;
210:                if (negative)
211:                    duration = -duration;
212:                return duration;
213:            }
214:
215:            /**
216:             Decimals are stored in lower order bytes as a float
217:             */
218:            public static float getPercentAsDecimal(long duration) {
219:                int right = (int) (duration & 0xFFFFFFFF);
220:                return Float.intBitsToFloat(right);
221:            }
222:
223:            public static long setPercentAsDecimal(long duration,
224:                    float percentAsDecimal) {
225:                duration &= (0xFFFFFFFF00000000L); // clear out low int
226:                duration |= (Float.floatToIntBits(percentAsDecimal));
227:                return duration;
228:            }
229:
230:            public static boolean isPercent(long duration) {
231:                return getBits(UNITS_MASK, duration) == PERCENT_BIT;
232:            }
233:
234:            public static long setAsPercent(long duration) {
235:                return setBits(duration, ELAPSED_AND_UNITS_MASK, PERCENT_BIT);
236:            }
237:
238:            public static long setAsElapsedPercent(long duration) {
239:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
240:                        | PERCENT_BIT);
241:            }
242:
243:            public static long setAsNonTemporal(long duration) {
244:                return setBits(duration, ELAPSED_AND_UNITS_MASK,
245:                        NON_TEMPORAL_BIT);
246:            }
247:
248:            public static long setAsSeconds(long duration) {
249:                return setBits(duration, ELAPSED_AND_UNITS_MASK, SECONDS_BIT);
250:            }
251:
252:            public static long setAsElapsedSeconds(long duration) {
253:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
254:                        | SECONDS_BIT);
255:            }
256:
257:            public static long setAsMinutes(long duration) {
258:                return setBits(duration, ELAPSED_AND_UNITS_MASK, MINUTES_BIT);
259:            }
260:
261:            public static long setAsElapsedMinutes(long duration) {
262:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
263:                        | MINUTES_BIT);
264:            }
265:
266:            public static long setAsHours(long duration) {
267:                return setBits(duration, ELAPSED_AND_UNITS_MASK, HOURS_BIT);
268:            }
269:
270:            public static long setAsElapsedHours(long duration) {
271:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
272:                        | SECONDS_BIT);
273:            }
274:
275:            public static long setAsDays(long duration) {
276:                return setBits(duration, ELAPSED_AND_UNITS_MASK, DAYS_BIT);
277:            }
278:
279:            public static long setAsElapsedDays(long duration) {
280:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
281:                        | DAYS_BIT);
282:            }
283:
284:            public static long setAsWeeks(long duration) {
285:                return setBits(duration, ELAPSED_AND_UNITS_MASK, WEEKS_BIT);
286:            }
287:
288:            public static long setAsElapsedWeeks(long duration) {
289:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
290:                        | WEEKS_BIT);
291:            }
292:
293:            public static long setAsMonths(long duration) {
294:                return setBits(duration, ELAPSED_AND_UNITS_MASK, MONTHS_BIT);
295:            }
296:
297:            public static long setAsElapsedMonths(long duration) {
298:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
299:                        | MONTHS_BIT);
300:            }
301:
302:            public static long setAsYears(long duration) {
303:                return setBits(duration, ELAPSED_AND_UNITS_MASK, YEARS_BIT);
304:            }
305:
306:            public static long setAsElapsedYears(long duration) {
307:                return setBits(duration, ELAPSED_AND_UNITS_MASK, ELAPSED_BIT
308:                        | YEARS_BIT);
309:            }
310:
311:            /**
312:             * Use the time unit of another duration if this duration has none
313:             * @param duration input duraiton
314:             * @param durationWithUnit duration that has time unit
315:             * @return the original duration if it had a time unit, otherwise the duration with the time unit of durationWithUnit
316:             */
317:            public static long useTimeUnitOfInNone(long duration,
318:                    long durationWithUnit) {
319:                if (getType(duration) == TimeUnit.NONE)
320:                    return setAsTimeUnit(duration, getType(durationWithUnit));
321:                return duration;
322:            }
323:
324:            public static long setAsTimeUnit(long duration, int type) {
325:                switch (type) {
326:                case TimeUnit.PERCENT:
327:                    return setAsPercent(duration);
328:                case TimeUnit.ELAPSED_PERCENT:
329:                    return setAsElapsedPercent(duration);
330:                case TimeUnit.NON_TEMPORAL:
331:                    return setAsNonTemporal(duration);
332:                case TimeUnit.MINUTES:
333:                    return setAsMinutes(duration);
334:                case TimeUnit.ELAPSED_MINUTES:
335:                    return setAsElapsedMinutes(duration);
336:                case TimeUnit.HOURS:
337:                    return setAsHours(duration);
338:                case TimeUnit.ELAPSED_HOURS:
339:                    return setAsElapsedHours(duration);
340:                case TimeUnit.DAYS:
341:                    return setAsDays(duration);
342:                case TimeUnit.ELAPSED_DAYS:
343:                    long x = setAsDays(duration);
344:                    long y = setAsElapsedDays(duration);
345:
346:                    return setAsElapsedDays(duration);
347:                case TimeUnit.WEEKS:
348:                    return setAsWeeks(duration);
349:                case TimeUnit.ELAPSED_WEEKS:
350:                    return setAsElapsedWeeks(duration);
351:                case TimeUnit.MONTHS:
352:                    return setAsMonths(duration);
353:                case TimeUnit.ELAPSED_MONTHS: // microsoft treats elapsed months as 30 days
354:                    return setAsElapsedMonths(duration);
355:                case TimeUnit.YEARS:
356:                    return setAsYears(duration);
357:                case TimeUnit.ELAPSED_YEARS:
358:                    return setAsElapsedYears(duration);
359:                    // 	don't bother for NONE
360:                }
361:                return duration;
362:            }
363:
364:            public static double timeUnitFactor(int type) {
365:                double result = 1.0;
366:                if (type == TimeUnit.NONE)
367:                    type = ScheduleOption.getInstance().getDurationEnteredIn();
368:
369:                switch (type) {
370:                case TimeUnit.NON_TEMPORAL:
371:                    return 1.0D;
372:                case TimeUnit.MINUTES:
373:                case TimeUnit.ELAPSED_MINUTES:
374:                    return 60.0 * 1000;
375:                case TimeUnit.HOURS:
376:                case TimeUnit.ELAPSED_HOURS:
377:                    return 60.0 * 60 * 1000;
378:                case TimeUnit.DAYS:
379:                    return CalendarOption.getInstance().getHoursPerDay() * 60.0 * 60 * 1000;
380:                case TimeUnit.ELAPSED_DAYS:
381:                    return 24.0 * 60 * 60 * 1000;
382:                case TimeUnit.WEEKS:
383:                    return CalendarOption.getInstance().getHoursPerWeek() * 60.0 * 60 * 1000;
384:                case TimeUnit.ELAPSED_WEEKS:
385:                    return 7.0 * 24 * 60 * 60 * 1000;
386:                case TimeUnit.MONTHS:
387:                    return CalendarOption.getInstance().hoursPerMonth() * 60.0 * 60 * 1000;
388:                case TimeUnit.ELAPSED_MONTHS: // microsoft treats elapsed months as 30 days
389:                // Note that there was a bug in this in project 2000: see http://www.kbalertz.com/Feedback_242628.aspx				
390:                    return 30.0 * 24 * 60 * 60 * 1000;
391:                case TimeUnit.YEARS:
392:                    // TODO verify years and elapsed years as 365.  Project 2003 doesn't seem to treat years
393:                    return 365 * CalendarOption.getInstance().getHoursPerDay()
394:                            * 60 * 60 * 1000;
395:                case TimeUnit.ELAPSED_YEARS:
396:                    return 365 * 24 * 60 * 60 * 1000;
397:                }
398:                return result;
399:            }
400:
401:            public static long getInstance(double value, int type) {
402:                long result = 0;
403:                if (type == TimeUnit.PERCENT
404:                        || type == TimeUnit.ELAPSED_PERCENT) { // percentages are treated specially
405:                    result = Duration
406:                            .setPercentAsDecimal(result, (float) value);
407:                } else {
408:                    result = Math.round(value * timeUnitFactor(type));
409:                }
410:                result = setAsTimeUnit(result, type);
411:                return result;
412:            }
413:
414:            public static double getValue(long duration) {
415:                int type = getEffectiveType(duration);
416:                if (type == TimeUnit.PERCENT
417:                        || type == TimeUnit.ELAPSED_PERCENT) { // percentages are treated specially
418:                    return getPercentAsDecimal(duration);
419:                } else {
420:                    return ((double) millis(duration)) / timeUnitFactor(type);
421:                    //TODO confirm no rounding error above
422:
423:                }
424:            }
425:
426:            public static int getType(long duration) {
427:                long unitBits = getBits(ELAPSED_AND_UNITS_MASK, duration);
428:                if (unitBits == PERCENT_BIT)
429:                    return TimeUnit.PERCENT;
430:                else if (unitBits == (ELAPSED_BIT | PERCENT_BIT))
431:                    return TimeUnit.ELAPSED_PERCENT;
432:                else if (unitBits == YEARS_BIT)
433:                    return TimeUnit.YEARS;
434:                else if (unitBits == (ELAPSED_BIT | YEARS_BIT))
435:                    return TimeUnit.ELAPSED_YEARS;
436:                else if (unitBits == MONTHS_BIT)
437:                    return TimeUnit.MONTHS;
438:                else if (unitBits == (ELAPSED_BIT | MONTHS_BIT))
439:                    return TimeUnit.ELAPSED_MONTHS;
440:                else if (unitBits == WEEKS_BIT)
441:                    return TimeUnit.WEEKS;
442:                else if (unitBits == (ELAPSED_BIT | WEEKS_BIT))
443:                    return TimeUnit.ELAPSED_WEEKS;
444:                else if (unitBits == DAYS_BIT)
445:                    return TimeUnit.DAYS;
446:                else if (unitBits == (ELAPSED_BIT | DAYS_BIT))
447:                    return TimeUnit.ELAPSED_DAYS;
448:                else if (unitBits == HOURS_BIT)
449:                    return TimeUnit.HOURS;
450:                else if (unitBits == (ELAPSED_BIT | HOURS_BIT))
451:                    return TimeUnit.ELAPSED_HOURS;
452:                else if (unitBits == MINUTES_BIT)
453:                    return TimeUnit.MINUTES;
454:                else if (unitBits == (ELAPSED_BIT | MINUTES_BIT))
455:                    return TimeUnit.ELAPSED_MINUTES;
456:                else if (unitBits == NON_TEMPORAL_BIT)
457:                    return TimeUnit.NON_TEMPORAL;
458:                return TimeUnit.NONE;
459:            }
460:
461:            // same as get type but converts NONE to current preference
462:            public static int getEffectiveType(long duration) {
463:                int type = getType(duration);
464:                if (type == TimeUnit.NONE)
465:                    type = ScheduleOption.getInstance().getDurationEnteredIn();
466:                return type;
467:            }
468:
469:            public static Duration getInstanceFromDouble(Double doubleObject) {
470:                return new Duration(doubleObject);
471:            }
472:
473:            /* (non-Javadoc)
474:             * @see java.lang.Number#doubleValue()
475:             */
476:            public double doubleValue() {
477:                return millis(encodedMillis);
478:            }
479:
480:            /* (non-Javadoc)
481:             * @see java.lang.Number#floatValue()
482:             */
483:            public float floatValue() {
484:                return millis(encodedMillis);
485:            }
486:
487:            /* (non-Javadoc)
488:             * @see java.lang.Number#intValue()
489:             */
490:            public int intValue() {
491:                return (int) millis(encodedMillis);
492:            }
493:
494:            /* (non-Javadoc)
495:             * @see java.lang.Number#longValue()
496:             */
497:            public long longValue() {
498:                return millis(encodedMillis);
499:            }
500:
501:            public double getAsHours() {
502:                return doubleValue() / timeUnitFactor(TimeUnit.HOURS);
503:            }
504:
505:            public double getAsDays() {
506:                return doubleValue() / timeUnitFactor(TimeUnit.DAYS);
507:            }
508:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.