View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program 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.
14   */
15  package org.fourthline.cling.support.model.dlna.types;
16  
17  import java.util.Locale;
18  import java.util.concurrent.TimeUnit;
19  import java.util.regex.Matcher;
20  import java.util.regex.Pattern;
21  import org.fourthline.cling.model.types.InvalidValueException;
22  
23  /**
24   * @author Mario Franco
25   */
26  public class NormalPlayTime {
27  
28      public enum Format {
29  
30          SECONDS,
31          TIME
32      }
33      final static Pattern pattern = Pattern.compile("^(\\d+):(\\d{1,2}):(\\d{1,2})(\\.(\\d{1,3}))?|(\\d+)(\\.(\\d{1,3}))?$", Pattern.CASE_INSENSITIVE);
34      private long milliseconds;
35  
36      public NormalPlayTime(long milliseconds) {
37          if (milliseconds < 0) {
38              throw new InvalidValueException("Invalid parameter milliseconds: " + milliseconds);
39          }
40  
41          this.milliseconds = milliseconds;
42      }
43  
44      public NormalPlayTime(long hours, long minutes, long seconds, long milliseconds) throws InvalidValueException {
45          if (hours < 0) {
46              throw new InvalidValueException("Invalid parameter hours: " + hours);
47          }
48  
49          if (minutes < 0 || minutes > 59) {
50              throw new InvalidValueException("Invalid parameter minutes: " + hours);
51          }
52  
53          if (seconds < 0 || seconds > 59) {
54              throw new InvalidValueException("Invalid parameter seconds: " + hours);
55          }
56          if (milliseconds < 0 || milliseconds > 999) {
57              throw new InvalidValueException("Invalid parameter milliseconds: " + milliseconds);
58          }
59  
60          this.milliseconds = (hours * 60 * 60 + minutes * 60 + seconds) * 1000 + milliseconds;
61      }
62  
63      /**
64       * @return the milliseconds
65       */
66      public long getMilliseconds() {
67          return milliseconds;
68      }
69  
70      /**
71       * @param milliseconds the milliseconds to set
72       */
73      public void setMilliseconds(long milliseconds) {
74          if (milliseconds < 0) {
75              throw new InvalidValueException("Invalid parameter milliseconds: " + milliseconds);
76          }
77  
78          this.milliseconds = milliseconds;
79      }
80  
81      public String getString() {
82          return getString(Format.SECONDS);
83      }
84  
85      /**
86       * We don't ignore the right zeros in milliseconds, a small compromise 
87       * @param format
88       */
89      public String getString(Format format) {        
90          long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);
91          long ms = milliseconds % 1000;
92          switch (format) {
93              case TIME:
94                  seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds));
95                  long hours = TimeUnit.MILLISECONDS.toHours(milliseconds);
96                  long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(milliseconds));
97                  return String.format(Locale.ROOT, "%d:%02d:%02d.%03d", hours, minutes, seconds, ms);
98              default:
99                  return String.format(Locale.ROOT, "%d.%03d", seconds, ms);
100         }
101     }
102 
103     public static NormalPlayTime valueOf(String s) throws InvalidValueException {
104         Matcher matcher = pattern.matcher(s);
105         if (matcher.matches()) {
106             int msMultiplier = 0;
107             try {
108                 if (matcher.group(1) != null) {
109                     msMultiplier = (int) Math.pow(10, 3 - matcher.group(5).length());
110                     return new NormalPlayTime(
111                             Long.parseLong(matcher.group(1)),
112                             Long.parseLong(matcher.group(2)),
113                             Long.parseLong(matcher.group(3)),
114                             Long.parseLong(matcher.group(5))*msMultiplier);
115                 } else {
116                     msMultiplier = (int) Math.pow(10, 3 - matcher.group(8).length());
117                     return new NormalPlayTime(
118                             Long.parseLong(matcher.group(6)) * 1000 + Long.parseLong(matcher.group(8))*msMultiplier);
119                 }
120             } catch (NumberFormatException ex1) {
121             }
122         }
123         throw new InvalidValueException("Can't parse NormalPlayTime: " + s);
124     }
125 }