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 org.fourthline.cling.model.types.InvalidValueException;
18  
19  /**
20   *
21   * @author Mario Franco
22   */
23  public class NormalPlayTimeRange {
24  
25      public static final String PREFIX = "npt=";
26      
27      private NormalPlayTime timeStart;
28      private NormalPlayTime timeEnd;
29      private NormalPlayTime timeDuration;
30  
31      public NormalPlayTimeRange(long timeStart, long timeEnd) {
32          this.timeStart = new NormalPlayTime(timeStart);
33          this.timeEnd = new NormalPlayTime(timeEnd);
34      }
35  
36      public NormalPlayTimeRange(NormalPlayTime timeStart, NormalPlayTime timeEnd) {
37          this.timeStart = timeStart;
38          this.timeEnd = timeEnd;
39      }
40  
41      public NormalPlayTimeRange(NormalPlayTime timeStart, NormalPlayTime timeEnd, NormalPlayTime timeDuration) {
42          this.timeStart = timeStart;
43          this.timeEnd = timeEnd;
44          this.timeDuration = timeDuration;
45      }
46  
47      /**
48       * @return the timeStart
49       */
50      public NormalPlayTime getTimeStart() {
51          return timeStart;
52      }
53  
54      /**
55       * @return the timeEnd
56       */
57      public NormalPlayTime getTimeEnd() {
58          return timeEnd;
59      }
60  
61      /**
62       * @return the timeDuration
63       */
64      public NormalPlayTime getTimeDuration() {
65          return timeDuration;
66      }
67  
68      /**
69       * 
70       * @return String format of Normal Play Time Range for response message header 
71       */
72      public String getString() {
73          return getString(true);
74      }
75  
76      /**
77       * 
78       * @return String format of Normal Play Time Range for response message header 
79       */
80      public String getString(boolean includeDuration) {
81          String s = PREFIX;
82  
83          s += timeStart.getString() + "-";
84          if (timeEnd != null) {
85              s += timeEnd.getString();
86          }
87          if (includeDuration) {
88              s += "/" + (timeDuration != null ? timeDuration.getString() : "*");
89          }
90  
91          return s;
92      }
93  
94      public static NormalPlayTimeRange valueOf(String s) throws InvalidValueException {
95          return valueOf(s, false);
96      }
97      
98      public static NormalPlayTimeRange valueOf(String s, boolean mandatoryTimeEnd) throws InvalidValueException {
99          if (s.startsWith(PREFIX)) {
100             NormalPlayTime timeStart, timeEnd = null, timeDuration = null;
101             String[] params = s.substring(PREFIX.length()).split("[-/]");
102             switch (params.length) {
103                 case 3:
104                     if (params[2].length() != 0 && !params[2].equals("*")) {
105                         timeDuration = NormalPlayTime.valueOf(params[2]);
106                     }
107                 case 2:
108                     if (params[1].length() != 0) {
109                         timeEnd = NormalPlayTime.valueOf(params[1]);
110                     }
111                 case 1:
112                     if (params[0].length() != 0 && (!mandatoryTimeEnd || ( mandatoryTimeEnd && params.length>1))) {
113                         timeStart = NormalPlayTime.valueOf(params[0]);
114                         return new NormalPlayTimeRange(timeStart, timeEnd, timeDuration);
115                     }
116                 default:
117                     break;
118             }
119         }
120         throw new InvalidValueException("Can't parse NormalPlayTimeRange: " + s);
121     }
122 }