View Javadoc
1   /*
2    * Copyright 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.commons.core.datetime;
17  
18  import java.util.Date;
19  import java.util.regex.Pattern;
20  
21  /**
22   * @author Martin Krasser
23   */
24  public class Duration {
25  
26      private static final String EMPTY = "";
27      private static final Pattern PATTERN = Pattern.compile("^[0-9]+[smhd]?$");
28      private static final Pattern PATTERN_UNIT = Pattern.compile("[smhd]");
29      private static final Pattern PATTERN_NUMBER = Pattern.compile("[0-9]+");
30      
31      private static final String SECOND = "s";
32      private static final String MINUTE = "m";
33      private static final String HOUR = "h";
34      private static final String DAY = "d";
35      
36      
37      private final long milliseconds;
38      
39      public Duration(long milliseconds) {
40          this.milliseconds = milliseconds;
41      }
42      
43      public long getValue() {
44          return milliseconds;
45      }
46      
47      public Date since() {
48          return since(new Date());
49      }
50      
51      public Date since(Date date) {
52          return new Date(date.getTime() - milliseconds);
53      }
54      
55      /**
56       * Parses the string representation of a duration. The string representation
57       * must follow the format <number>[<unit>] where <number>
58       * is a valid <code>long</code> number and &lt;unit&gt; is one of the
59       * following
60       * 
61       * <ul>
62       * <li><code>s</code>: second</li>
63       * <li><code>m</code>: minute</li>
64       * <li><code>h</code>: hour</li>
65       * <li><code>d</code>: day</li>
66       * </ul>
67       * 
68       * If no unit is given it defaults to milliseconds.
69       * 
70       * @param duration duration string
71       * @return parsed duration
72       */
73      public static Duration parse(String duration) {
74          String d = duration.trim();
75          if (!PATTERN.matcher(d).matches()) {
76              throw new DurationFormatException(
77                      "Duration string " + duration + " doesn't match pattern " + PATTERN);
78          }
79          String u = PATTERN_NUMBER.matcher(d).replaceFirst(EMPTY);
80          long v = Long.parseLong(PATTERN_UNIT.matcher(d).replaceFirst(EMPTY));
81          if (u.length() == 0) {
82              // v already in units of milliseconds
83          } else if (u.equals(SECOND)) {
84              v = v * 1000L;
85          } else if (u.equals(MINUTE)) {
86              v = v * 1000L * 60L;
87          } else if (u.equals(HOUR)) {
88              v = v * 1000L * 60L * 60L;
89          } else if (u.equals(DAY)) {
90              v = v * 1000L * 60L * 60L * 24L;
91          } 
92          return new Duration(v);
93      }
94  
95  }