1 package org.jscsi.target.settings;
2
3
4 import java.util.regex.Matcher;
5
6
7
8
9
10
11
12 public final class SingleNumericalValue extends NumericalValue {
13
14
15
16
17 private final int value;
18
19
20
21
22
23
24
25
26
27 private SingleNumericalValue (final int value) {
28 this.value = value;
29 }
30
31
32
33
34
35
36 public final int getValue () {
37 return value;
38 }
39
40
41
42
43
44
45
46 public static final SingleNumericalValue create (final int value) {
47 return new SingleNumericalValue(value);
48 }
49
50
51
52
53
54
55
56
57 public static final SingleNumericalValue parseSingleNumericValue (final String value) {
58
59 final Matcher decimalMatcher = DECIMAL_CONSTANT_PATTERN.matcher(value);
60 if (decimalMatcher.matches()) return new SingleNumericalValue(parseDecimalConstantString(value));
61
62 final Matcher hexMatcher = HEX_CONSTANT_PATTERN.matcher(value);
63 if (hexMatcher.matches()) return new SingleNumericalValue(parseHexConstantString(value));
64
65 final Matcher base64Matcher = BASE_64_CONSTANT_PATTERN.matcher(value);
66 if (base64Matcher.matches()) return new SingleNumericalValue(parseBase64ConstantString(value));
67
68 return null;
69 }
70
71
72
73
74
75
76
77
78
79
80 private static final int parseDecimalConstantString (final String value) {
81 return Integer.valueOf(value);
82 }
83
84
85
86
87
88
89
90
91
92
93 private static final int parseHexConstantString (final String value) {
94 return Integer.parseInt(value.substring(2),
95 16);
96 }
97
98
99
100
101
102
103
104
105
106
107 private static final int parseBase64ConstantString (final String value) {
108 final String s = value.substring(2);
109 final int length = s.length();
110 int result = 0;
111 for (int i = 0; i < length; ++i) {
112 final char c = s.charAt(length - 1 - i);
113 result += base64ValueOf(c) * Math.pow(64, i);
114 }
115 return result;
116 }
117
118
119
120
121
122
123
124
125 private static final int base64ValueOf (final char c) {
126 if ('A' <= c && c <= 'Z') return c - 'A';
127 if ('a' <= c && c <= 'z') return c - 'a' + 26;
128 if ('0' <= c && c <= '9') return c - '0' + 52;
129 if (c == '+') return 62;
130 if (c == '/') return 63;
131 throw new NumberFormatException();
132 }
133
134 @Override
135 public final String toString () {
136 return "[" + value + "]";
137 }
138
139 @Override
140 public boolean contains (Object value) {
141 if (value == null) return false;
142 if (value instanceof SingleNumericalValue) return contains(((SingleNumericalValue) value).getValue());
143 if (value instanceof Integer) return contains((int) ((Integer) value));
144 return false;
145 }
146
147 @Override
148 public boolean contains (final int value) {
149 if (value == this.value) return true;
150 return false;
151 }
152 }