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;
16  
17  import org.fourthline.cling.model.types.InvalidValueException;
18  import org.fourthline.cling.support.avtransport.lastchange.AVTransportVariable.TransportPlaySpeed;
19  
20  /**
21   * @author Mario Franco
22   */
23  public class DLNAPlaySpeedAttribute extends DLNAAttribute<TransportPlaySpeed[]> {
24      
25      public DLNAPlaySpeedAttribute() {
26          setValue(new TransportPlaySpeed[] {});
27      }
28  
29      public DLNAPlaySpeedAttribute(TransportPlaySpeed[] speeds) {
30          setValue(speeds);
31      }
32      
33      public DLNAPlaySpeedAttribute(String[] speeds) {
34          TransportPlaySpeed[] sp = new TransportPlaySpeed[speeds.length]; 
35          try {
36              for (int i = 0; i < speeds.length; i++) {
37                  sp[i] = new TransportPlaySpeed(speeds[i]);
38              }
39          } catch (InvalidValueException invalidValueException) {
40              throw new InvalidDLNAProtocolAttributeException("Can't parse DLNA play speeds.");
41          }
42          setValue(sp);
43      }
44  
45      public void setString(String s, String cf) throws InvalidDLNAProtocolAttributeException {
46          TransportPlaySpeed[] value = null;
47          if (s != null && s.length() != 0) {
48              String[] speeds = s.split(",");
49              try {
50                  value = new TransportPlaySpeed[speeds.length]; 
51                  for (int i = 0; i < speeds.length; i++) {
52                      value[i] = new TransportPlaySpeed(speeds[i]);
53                  }
54              } catch (InvalidValueException invalidValueException) {
55                  value = null;
56              }
57          }
58          if (value == null) {
59              throw new InvalidDLNAProtocolAttributeException("Can't parse DLNA play speeds from: " + s);
60          }
61          setValue(value);
62      }
63  
64      public String getString() {
65          String s = "";
66          for (TransportPlaySpeed speed : getValue()) {
67              if (speed.getValue().equals("1"))
68                  continue;
69              s += (s.length() == 0 ? "" : ",") + speed;
70          }
71          return s;
72      }
73      
74  }