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  
16  package org.fourthline.cling.support.model;
17  
18  import org.fourthline.cling.model.ModelUtil;
19  import org.fourthline.cling.model.action.ActionArgumentValue;
20  import org.fourthline.cling.model.types.UnsignedIntegerFourBytes;
21  
22  import java.util.Map;
23  
24  /**
25   * @author Christian Bauer
26   */
27  public class PositionInfo {
28  
29      private UnsignedIntegerFourBytes track = new UnsignedIntegerFourBytes(0);
30      private String trackDuration = "00:00:00";
31      private String trackMetaData = "NOT_IMPLEMENTED";
32      private String trackURI = "";
33      private String relTime = "00:00:00";
34      private String absTime = "00:00:00"; // TODO: MORE VALUES IN DOMAIN!
35      private int relCount = Integer.MAX_VALUE; // Indicates that we don't support this
36      private int absCount = Integer.MAX_VALUE;
37  
38      public PositionInfo() {
39      }
40  
41      public PositionInfo(Map<String, ActionArgumentValue> args) {
42          this(
43                  ((UnsignedIntegerFourBytes) args.get("Track").getValue()).getValue(),
44                  (String) args.get("TrackDuration").getValue(),
45                  (String) args.get("TrackMetaData").getValue(),
46                  (String) args.get("TrackURI").getValue(),
47                  (String) args.get("RelTime").getValue(),
48                  (String) args.get("AbsTime").getValue(),
49                  (Integer) args.get("RelCount").getValue(),
50                  (Integer) args.get("AbsCount").getValue()
51          );
52      }
53  
54      public PositionInfo(PositionInfo copy, String relTime, String absTime) {
55          this.track = copy.track;
56          this.trackDuration = copy.trackDuration;
57          this.trackMetaData = copy.trackMetaData;
58          this.trackURI = copy.trackURI;
59          this.relTime = relTime;
60          this.absTime = absTime;
61          this.relCount = copy.relCount;
62          this.absCount = copy.absCount;
63      }
64  
65      public PositionInfo(PositionInfo copy, long relTimeSeconds, long absTimeSeconds) {
66          this.track = copy.track;
67          this.trackDuration = copy.trackDuration;
68          this.trackMetaData = copy.trackMetaData;
69          this.trackURI = copy.trackURI;
70          this.relTime = ModelUtil.toTimeString(relTimeSeconds);
71          this.absTime = ModelUtil.toTimeString(absTimeSeconds);
72          this.relCount = copy.relCount;
73          this.absCount = copy.absCount;
74      }
75  
76      public PositionInfo(long track, String trackDuration, String trackURI,
77                          String relTime, String absTime) {
78          this.track = new UnsignedIntegerFourBytes(track);
79          this.trackDuration = trackDuration;
80          this.trackURI = trackURI;
81          this.relTime = relTime;
82          this.absTime = absTime;
83      }
84  
85      public PositionInfo(long track, String trackDuration,
86                          String trackMetaData, String trackURI,
87                          String relTime, String absTime, int relCount, int absCount) {
88          this.track = new UnsignedIntegerFourBytes(track);
89          this.trackDuration = trackDuration;
90          this.trackMetaData = trackMetaData;
91          this.trackURI = trackURI;
92          this.relTime = relTime;
93          this.absTime = absTime;
94          this.relCount = relCount;
95          this.absCount = absCount;
96      }
97  
98      public PositionInfo(long track, String trackMetaData, String trackURI) {
99          this.track = new UnsignedIntegerFourBytes(track);
100         this.trackMetaData = trackMetaData;
101         this.trackURI = trackURI;
102     }
103 
104     public UnsignedIntegerFourBytes getTrack() {
105         return track;
106     }
107 
108     public String getTrackDuration() {
109         return trackDuration;
110     }
111 
112     public String getTrackMetaData() {
113         return trackMetaData;
114     }
115 
116     public String getTrackURI() {
117         return trackURI;
118     }
119 
120     public String getRelTime() {
121         return relTime;
122     }
123 
124     public String getAbsTime() {
125         return absTime;
126     }
127 
128     public int getRelCount() {
129         return relCount;
130     }
131 
132     public int getAbsCount() {
133         return absCount;
134     }
135     
136     public void setTrackDuration(String trackDuration) {
137        this.trackDuration = trackDuration;
138     }
139 
140     public void setRelTime(String relTime) {
141        this.relTime = relTime;
142     }
143        
144     public long getTrackDurationSeconds() {
145         return getTrackDuration() == null ? 0 : ModelUtil.fromTimeString(getTrackDuration());
146     }
147 
148     public long getTrackElapsedSeconds() {
149         return getRelTime() == null || getRelTime().equals("NOT_IMPLEMENTED") ? 0 : ModelUtil.fromTimeString(getRelTime());
150     }
151 
152     public long getTrackRemainingSeconds() {
153         return getTrackDurationSeconds() - getTrackElapsedSeconds();
154     }
155 
156     public int getElapsedPercent() {
157         long elapsed = getTrackElapsedSeconds();
158         long total = getTrackDurationSeconds();
159         if (elapsed == 0 || total == 0) return 0;
160         return new Double(elapsed/((double)total/100)).intValue();
161     }
162 
163 
164     @Override
165     public String toString() {
166         return "(PositionInfo) Track: " + getTrack() + " RelTime: " + getRelTime() + " Duration: " + getTrackDuration() + " Percent: " + getElapsedPercent();
167     }
168 }