1 package org.jbpm.calendar; 2 3 import java.io.Serializable ; 4 import java.util.HashMap ; 5 import java.util.Map ; 6 import java.util.Properties ; 7 8 25 public class Duration implements Serializable { 26 27 private static final long serialVersionUID = 1L; 28 29 private static final long SECOND = 1000; 30 private static final long MINUTE = 60*SECOND; 31 private static final long HOUR = 60*MINUTE; 32 private static final long DAY = 24*HOUR; 33 private static final long WEEK = 7*DAY; 34 private static final long MONTH = 30*DAY; 35 private static final long YEAR = 365*DAY; 36 37 private static long BUSINESS_DAY = 0; 38 private static long BUSINESS_WEEK = 0; 39 private static long BUSINESS_MONTH = 0; 40 private static long BUSINESS_YEAR = 0; 41 42 static { 43 Properties businessCalendarProperties = BusinessCalendar.getBusinessCalendarProperties(); 44 String businessDayText = businessCalendarProperties.getProperty("business.day.expressed.in.hours"); 45 String businessWeekText = businessCalendarProperties.getProperty("business.week.expressed.in.hours"); 46 String businessMonthText = businessCalendarProperties.getProperty("business.month.expressed.in.business.days"); 47 String businessYearText = businessCalendarProperties.getProperty("business.year.expressed.in.business.days"); 48 49 BUSINESS_DAY = (long) (Double.parseDouble(businessDayText) * HOUR); 50 BUSINESS_WEEK = (long) (Double.parseDouble(businessWeekText) * HOUR); 51 BUSINESS_MONTH = (long) (Double.parseDouble(businessMonthText) * BUSINESS_DAY); 52 BUSINESS_YEAR = (long) (Double.parseDouble(businessYearText) * BUSINESS_DAY); 53 } 54 55 private static Map units = null; 56 static { 57 units = new HashMap (); 58 units.put("second", new Long (SECOND)); 59 units.put("seconds", new Long (SECOND)); 60 units.put("business second", new Long (SECOND)); 61 units.put("business seconds", new Long (SECOND)); 62 units.put("minute", new Long (MINUTE)); 63 units.put("minutes", new Long (MINUTE)); 64 units.put("business minute", new Long (MINUTE)); 65 units.put("business minutes", new Long (MINUTE)); 66 units.put("hour", new Long (HOUR)); 67 units.put("hours", new Long (HOUR)); 68 units.put("business hour", new Long (HOUR)); 69 units.put("business hours", new Long (HOUR)); 70 units.put("day", new Long (DAY)); 71 units.put("days", new Long (DAY)); 72 units.put("business day", new Long (BUSINESS_DAY)); 73 units.put("business days", new Long (BUSINESS_DAY)); 74 units.put("week", new Long (WEEK)); 75 units.put("weeks", new Long (WEEK)); 76 units.put("business week", new Long (BUSINESS_WEEK)); 77 units.put("business weeks", new Long (BUSINESS_WEEK)); 78 units.put("month", new Long (MONTH)); 79 units.put("months", new Long (MONTH)); 80 units.put("business month", new Long (BUSINESS_MONTH)); 81 units.put("business months", new Long (BUSINESS_MONTH)); 82 units.put("year", new Long (YEAR)); 83 units.put("years", new Long (YEAR)); 84 units.put("business year", new Long (BUSINESS_YEAR)); 85 units.put("business years", new Long (BUSINESS_YEAR)); 86 } 87 88 long milliseconds = 0; 89 boolean isBusinessTime = false; 90 91 Duration() { 92 } 93 94 public Duration(long milliseconds) { 95 this.milliseconds = milliseconds; 96 } 97 98 public Duration(Duration duration) { 99 this.milliseconds = duration.milliseconds; 100 this.isBusinessTime = duration.isBusinessTime; 101 } 102 103 125 public Duration(String duration) { 126 if (duration==null) throw new NullPointerException ("duration is null"); 127 int separatorIndex = duration.indexOf(' '); 128 if (separatorIndex==-1) throw new IllegalArgumentException ("improper format of duration '"+duration+"'"); 129 String quantityText = duration.substring(0, separatorIndex).trim().toLowerCase(); 130 String unitText = duration.substring(separatorIndex+1).trim().toLowerCase(); 131 if (unitText.startsWith("business")) { 132 isBusinessTime = true; 133 } 134 double amount = Double.parseDouble(quantityText); 135 Long unit = (Long ) units.get(unitText); 136 this.milliseconds = (long) (amount * unit.longValue()); 137 } 138 } 139 | Popular Tags |