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.renderingcontrol.lastchange;
17  
18  import org.fourthline.cling.model.types.Datatype;
19  import org.fourthline.cling.model.types.InvalidValueException;
20  import org.fourthline.cling.model.types.UnsignedIntegerTwoBytes;
21  import org.fourthline.cling.model.types.UnsignedIntegerTwoBytesDatatype;
22  import org.fourthline.cling.support.lastchange.EventedValue;
23  import org.fourthline.cling.support.model.Channel;
24  import org.fourthline.cling.support.shared.AbstractMap;
25  
26  import java.util.Map;
27  
28  /**
29   * @author Christian Bauer
30   */
31  public class EventedValueChannelVolumeDB extends EventedValue<ChannelVolumeDB> {
32  
33      public EventedValueChannelVolumeDB(ChannelVolumeDB value) {
34          super(value);
35      }
36  
37      public EventedValueChannelVolumeDB(Map.Entry<String, String>[] attributes) {
38          super(attributes);
39      }
40  
41      @Override
42      protected ChannelVolumeDB valueOf(Map.Entry<String, String>[] attributes) throws InvalidValueException {
43          Channel channel = null;
44          Integer volumeDB = null;
45          for (Map.Entry<String, String> attribute : attributes) {
46              if (attribute.getKey().equals("channel"))
47                  channel = Channel.valueOf(attribute.getValue());
48              if (attribute.getKey().equals("val"))
49                  volumeDB = (new UnsignedIntegerTwoBytesDatatype()
50                          .valueOf(attribute.getValue()))
51                          .getValue().intValue(); // Java is fun!
52          }
53          return channel != null && volumeDB != null ? new ChannelVolumeDB(channel, volumeDB) : null;
54      }
55  
56      @Override
57      public Map.Entry<String, String>[] getAttributes() {
58          return new Map.Entry[]{
59                  new AbstractMap.SimpleEntry<>(
60                          "val",
61                          new UnsignedIntegerTwoBytesDatatype().getString(
62                                  new UnsignedIntegerTwoBytes(getValue().getVolumeDB())
63                          )
64                  ),
65                  new AbstractMap.SimpleEntry<>(
66                          "channel",
67                          getValue().getChannel().name()
68                  )
69          };
70      }
71  
72      @Override
73      public String toString() {
74          return getValue().toString();
75      }
76  
77      @Override
78      protected Datatype getDatatype() {
79          return null; // Not needed
80      }
81  }