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.model.types;
17  
18  /**
19   * Although the UDA 1.0 spec doesn't say it, we assume that "int" is a 4 byte regular Java integer.
20   *
21   * @author Christian Bauer
22   */
23  public class IntegerDatatype extends AbstractDatatype<Integer> {
24  
25      private int byteSize;
26  
27      public IntegerDatatype(int byteSize) {
28          this.byteSize = byteSize;
29      }
30  
31      @Override
32      public boolean isHandlingJavaType(Class type) {
33          return type == Integer.TYPE || Integer.class.isAssignableFrom(type);
34      }
35  
36      public Integer valueOf(String s) throws InvalidValueException {
37          if (s.equals("")) return null;
38          try {
39              Integer value = Integer.parseInt(s.trim());
40              if (!isValid(value)) {
41                  throw new InvalidValueException("Not a " + getByteSize() + " byte(s) integer: " + s)
42                          ;
43              }
44              return value;
45          } catch (NumberFormatException ex) {
46              // TODO: UPNP VIOLATION: Some renderers (like PacketVideo TMM Player) send
47              // RelCount and AbsCount as "NOT_IMPLEMENTED" in GetPositionInfoResponse action.
48              // The spec says: If not implemented the value shall be Max Integer value.
49          	if(s.equals("NOT_IMPLEMENTED")) {
50          		return getMaxValue();
51          	} else {
52              	throw new InvalidValueException("Can't convert string to number: " + s, ex);
53          	}
54          }
55      }
56  
57      public boolean isValid(Integer value) {
58          return value == null || (value >= getMinValue() && value <= getMaxValue());
59      }
60  
61      public int getMinValue() {
62          switch(getByteSize()) {
63              case 1:
64                  return Byte.MIN_VALUE;
65              case 2:
66                  return Short.MIN_VALUE;
67              case 4:
68                  return Integer.MIN_VALUE;
69          }
70          throw new IllegalArgumentException("Invalid integer byte size: " + getByteSize());
71      }
72  
73      public int getMaxValue() {
74          switch(getByteSize()) {
75              case 1:
76                  return Byte.MAX_VALUE;
77              case 2:
78                  return Short.MAX_VALUE;
79              case 4:
80                  return Integer.MAX_VALUE;
81          }
82          throw new IllegalArgumentException("Invalid integer byte size: " + getByteSize());
83      }
84  
85      public int getByteSize() {
86          return byteSize;
87      }
88  
89  }