Repository navigation
Expand file tree
/
Copy pathAbstractPrimitive.java
More file actions
198 lines (172 loc) · 5.89 KB
/
Copy pathAbstractPrimitive.java
File metadata and controls
198 lines (172 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package org.javawebstack.abstractdata;
import org.javawebstack.abstractdata.exception.AbstractCoercingException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;
public class AbstractPrimitive implements AbstractElement {
private final Object value;
public AbstractPrimitive(Number value) {
if(value == null)
throw new NullPointerException("AbstractPrimitive value can not be null");
this.value = value;
}
public AbstractPrimitive(Boolean value) {
if(value == null)
throw new NullPointerException("AbstractPrimitive value can not be null");
this.value = value;
}
public AbstractPrimitive(String value) {
if(value == null)
throw new NullPointerException("AbstractPrimitive value can not be null");
this.value = value;
}
public boolean isNumber() {
return value instanceof Number;
}
public boolean isString() {
return value instanceof String;
}
public boolean isBoolean() {
return value instanceof Boolean;
}
public boolean isPrimitive() {
return true;
}
public AbstractPrimitive primitive() {
return this;
}
public String string() throws AbstractCoercingException {
return string(false);
}
public String string(boolean strict) throws AbstractCoercingException {
if(!(value instanceof String)) {
if(strict)
throw new AbstractCoercingException(Type.STRING, getType());
switch (getType()) {
case BOOLEAN:
return bool().toString();
case NUMBER:
return number().toString();
default:
throw new AbstractCoercingException(Type.STRING, getType());
}
}
return (String) value;
}
public Number number() throws AbstractCoercingException {
return number(false);
}
public Number number(boolean strict) throws AbstractCoercingException {
if(!(value instanceof Number)) {
if(strict)
throw new AbstractCoercingException(Type.NUMBER, getType());
switch (getType()) {
case BOOLEAN:
return ((Boolean) value) ? 1 : 0;
case STRING: {
try {
return NumberFormat.getNumberInstance().parse((String) value);
} catch (ParseException e) {
throw new AbstractCoercingException(Type.NUMBER, this);
}
}
default:
throw new AbstractCoercingException(Type.NUMBER, getType());
}
}
return (Number) value;
}
public Boolean bool() throws AbstractCoercingException {
return bool(false);
}
public Boolean bool(boolean strict) throws AbstractCoercingException {
if(!(value instanceof Boolean)) {
if(strict)
throw new AbstractCoercingException(Type.BOOLEAN, getType());
switch (getType()) {
case STRING: {
switch (((String) value)) {
case "1":
case "true":
case "yes":
case "y":
return true;
case "0":
case "false":
case "no":
case "n":
return false;
default:
throw new AbstractCoercingException(Type.BOOLEAN, this);
}
}
case NUMBER: {
long l = ((Number) value).longValue();
if(l == 0)
return false;
if(l == 1)
return true;
throw new AbstractCoercingException(Type.BOOLEAN, this);
}
default:
throw new AbstractCoercingException(Type.BOOLEAN, getType());
}
}
return (Boolean) value;
}
public Object value() {
return value;
}
public Object toObject() {
return value;
}
public Map<String[], Object> toTree() {
Map<String[], Object> tree = new HashMap<>();
tree.put(new String[0], value);
return tree;
}
public static AbstractPrimitive from(Object object) {
if (object instanceof Number)
return new AbstractPrimitive((Number) object);
if (object instanceof Boolean)
return new AbstractPrimitive((Boolean) object);
if (object instanceof String)
return new AbstractPrimitive((String) object);
return null;
}
public Type getType() {
if (isBoolean())
return Type.BOOLEAN;
if (isNumber())
return Type.NUMBER;
if (isString())
return Type.STRING;
return null;
}
public String toString() {
return value.toString();
}
public AbstractElement clone() {
return from(value);
}
public boolean equals(Object obj, boolean strict) {
if (obj == null)
return false;
if (!(obj instanceof AbstractPrimitive))
return false;
AbstractPrimitive primitive = (AbstractPrimitive) obj;
try {
if(isBoolean())
return bool() == primitive.bool(strict);
if(isNumber())
return number().equals(primitive.number(strict));
if(isString())
return string().equals(primitive.string(strict));
} catch (AbstractCoercingException ignored) {}
return false;
}
public boolean equals (Object obj) {
return equals(obj, false);
}
}