1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model.types;
17
18
19
20
21
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
47
48
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 }