Skip to content

Commit 4c02514

Browse files
committed
Started to implement strict mode and implemented support for epoch dates
1 parent 935734d commit 4c02514

File tree

12 files changed

+357
-142
lines changed

12 files changed

+357
-142
lines changed

src/main/java/org/javawebstack/abstractdata/AbstractArray.java

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.javawebstack.abstractdata;
22

33
import org.javawebstack.abstractdata.collector.AbstractArrayCollector;
4+
import org.javawebstack.abstractdata.exception.AbstractCoercingException;
45

56
import java.util.*;
67
import java.util.function.Function;
@@ -15,109 +16,118 @@ public boolean isArray() {
1516
return true;
1617
}
1718

18-
public AbstractArray array() {
19+
public AbstractArray array(boolean strict) throws AbstractCoercingException {
1920
return this;
2021
}
2122

22-
public AbstractObject object(String key) {
23+
public AbstractObject object(boolean strict) throws AbstractCoercingException {
24+
if(strict)
25+
throw new AbstractCoercingException(Type.OBJECT, Type.ARRAY);
26+
AbstractObject object = new AbstractObject();
27+
for(int i=0; i< elements.size(); i++)
28+
object.set(String.valueOf(i), elements.get(i));
29+
return object;
30+
}
31+
32+
public AbstractObject object(String key) throws AbstractCoercingException {
2333
return query(key).object();
2434
}
2535

26-
public AbstractArray array(String key) {
36+
public AbstractArray array(String key) throws AbstractCoercingException {
2737
return query(key).array();
2838
}
2939

30-
public AbstractPrimitive primitive(String key) {
40+
public AbstractPrimitive primitive(String key) throws AbstractCoercingException {
3141
return query(key).primitive();
3242
}
3343

34-
public String string(String key) {
44+
public String string(String key) throws AbstractCoercingException {
3545
return query(key).string();
3646
}
3747

38-
public Boolean bool(String key) {
48+
public Boolean bool(String key) throws AbstractCoercingException {
3949
return query(key).bool();
4050
}
4151

42-
public Number number(String key) {
52+
public Number number(String key) throws AbstractCoercingException {
4353
return query(key).number();
4454
}
4555

46-
public AbstractObject object(String key, AbstractObject orElse) {
56+
public AbstractObject object(String key, AbstractObject orElse) throws AbstractCoercingException {
4757
return query(key, orElse).object();
4858
}
4959

50-
public AbstractArray array(String key, AbstractArray orElse) {
60+
public AbstractArray array(String key, AbstractArray orElse) throws AbstractCoercingException {
5161
return query(key, orElse).array();
5262
}
5363

54-
public AbstractPrimitive primitive(String key, AbstractPrimitive orElse) {
64+
public AbstractPrimitive primitive(String key, AbstractPrimitive orElse) throws AbstractCoercingException {
5565
return query(key, orElse).primitive();
5666
}
5767

58-
public String string(String key, String orElse) {
68+
public String string(String key, String orElse) throws AbstractCoercingException {
5969
return query(key, new AbstractPrimitive(orElse)).string();
6070
}
6171

62-
public Boolean bool(String key, Boolean orElse) {
72+
public Boolean bool(String key, Boolean orElse) throws AbstractCoercingException {
6373
return query(key, new AbstractPrimitive(orElse)).bool();
6474
}
6575

66-
public Number number(String key, Number orElse) {
76+
public Number number(String key, Number orElse) throws AbstractCoercingException {
6777
return query(key, new AbstractPrimitive(orElse)).number();
6878
}
6979

70-
public AbstractObject object(int index) {
80+
public AbstractObject object(int index) throws AbstractCoercingException {
7181
return get(index).object();
7282
}
7383

74-
public AbstractArray array(int index) {
84+
public AbstractArray array(int index) throws AbstractCoercingException {
7585
return get(index).array();
7686
}
7787

78-
public AbstractPrimitive primitive(int index) {
88+
public AbstractPrimitive primitive(int index) throws AbstractCoercingException {
7989
return get(index).primitive();
8090
}
8191

82-
public String string(int index) {
92+
public String string(int index) throws AbstractCoercingException {
8393
return get(index).string();
8494
}
8595

86-
public Boolean bool(int index) {
96+
public Boolean bool(int index) throws AbstractCoercingException {
8797
return get(index).bool();
8898
}
8999

90-
public Number number(int index) {
100+
public Number number(int index) throws AbstractCoercingException {
91101
return get(index).number();
92102
}
93103

94-
public AbstractObject object(int index, AbstractObject orElse) {
104+
public AbstractObject object(int index, AbstractObject orElse) throws AbstractCoercingException {
95105
return get(index, orElse).object();
96106
}
97107

98-
public AbstractArray array(int index, AbstractArray orElse) {
108+
public AbstractArray array(int index, AbstractArray orElse) throws AbstractCoercingException {
99109
return get(index, orElse).array();
100110
}
101111

102-
public AbstractPrimitive primitive(int index, AbstractPrimitive orElse) {
112+
public AbstractPrimitive primitive(int index, AbstractPrimitive orElse) throws AbstractCoercingException {
103113
return get(index, orElse).primitive();
104114
}
105115

106-
public String string(int index, String orElse) {
116+
public String string(int index, String orElse) throws AbstractCoercingException {
107117
return get(index, new AbstractPrimitive(orElse)).string();
108118
}
109119

110-
public Boolean bool(int index, Boolean orElse) {
120+
public Boolean bool(int index, Boolean orElse) throws AbstractCoercingException {
111121
return get(index, new AbstractPrimitive(orElse)).bool();
112122
}
113123

114-
public Number number(int index, Number orElse) {
124+
public Number number(int index, Number orElse) throws AbstractCoercingException {
115125
return get(index, new AbstractPrimitive(orElse)).number();
116126
}
117127

118128
public AbstractArray add(AbstractElement element) {
119129
if (element == null)
120-
element = AbstractNull.INSTANCE;
130+
element = AbstractNull.VALUE;
121131
elements.add(element);
122132
return this;
123133
}
@@ -141,7 +151,7 @@ public AbstractArray(Collection<Object> abstractElements) {
141151
}
142152

143153
public AbstractArray addNull() {
144-
return add(AbstractNull.INSTANCE);
154+
return add(AbstractNull.VALUE);
145155
}
146156

147157
public AbstractArray add(Number value) {
@@ -163,7 +173,7 @@ public AbstractArray add(String value) {
163173
}
164174

165175
public AbstractArray setNull(int i) {
166-
return set(i, AbstractNull.INSTANCE);
176+
return set(i, AbstractNull.VALUE);
167177
}
168178

169179
public AbstractArray set(int i, AbstractElement element) {

src/main/java/org/javawebstack/abstractdata/AbstractElement.java

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.bson.BsonValue;
44
import org.javawebstack.abstractdata.bson.BsonConverter;
55
import org.javawebstack.abstractdata.bson.BsonTypeAdapter;
6+
import org.javawebstack.abstractdata.exception.AbstractCoercingException;
67
import org.javawebstack.abstractdata.json.JsonDumper;
78
import org.javawebstack.abstractdata.json.JsonParser;
89
import org.javawebstack.abstractdata.util.QueryString;
@@ -46,34 +47,48 @@ default boolean isString() {
4647
return false;
4748
}
4849

49-
default AbstractPrimitive primitive() {
50-
return null;
50+
default AbstractPrimitive primitive() throws AbstractCoercingException {
51+
throw new AbstractCoercingException("PRIMITIVE", getType());
5152
}
5253

53-
default AbstractArray array() {
54-
return null;
54+
default AbstractArray array() throws AbstractCoercingException {
55+
return array(false);
5556
}
5657

57-
default AbstractObject object() {
58-
return null;
58+
default AbstractObject object() throws AbstractCoercingException {
59+
return object(false);
5960
}
6061

61-
default String string() {
62-
if (!isString())
63-
return null;
64-
return primitive().string();
62+
default AbstractArray array(boolean strict) throws AbstractCoercingException {
63+
throw new AbstractCoercingException("ARRAY", getType());
6564
}
6665

67-
default Boolean bool() {
68-
if (!isBoolean())
69-
return null;
70-
return primitive().bool();
66+
default AbstractObject object(boolean strict) throws AbstractCoercingException {
67+
throw new AbstractCoercingException("OBJECT", getType());
7168
}
7269

73-
default Number number() {
74-
if (!isNumber())
75-
return null;
76-
return primitive().number();
70+
default String string() throws AbstractCoercingException {
71+
return string(false);
72+
}
73+
74+
default Boolean bool() throws AbstractCoercingException {
75+
return bool(false);
76+
}
77+
78+
default Number number() throws AbstractCoercingException {
79+
return number(false);
80+
}
81+
82+
default String string(boolean strict) throws AbstractCoercingException {
83+
return primitive().string(strict);
84+
}
85+
86+
default Boolean bool(boolean strict) throws AbstractCoercingException {
87+
return primitive().bool(strict);
88+
}
89+
90+
default Number number(boolean strict) throws AbstractCoercingException {
91+
return primitive().number(strict);
7792
}
7893

7994
default BsonValue toBson() {
@@ -131,7 +146,7 @@ static AbstractElement fromYaml(String source) {
131146

132147
static AbstractElement fromAbstractObject(Object object) {
133148
if (object == null)
134-
return AbstractNull.INSTANCE;
149+
return AbstractNull.VALUE;
135150
if (object instanceof List) {
136151
List<Object> list = (List<Object>) object;
137152
AbstractArray array = new AbstractArray();
@@ -150,7 +165,7 @@ static AbstractElement fromAbstractObject(Object object) {
150165
return new AbstractPrimitive((String) object);
151166
if (object instanceof Boolean)
152167
return new AbstractPrimitive((Boolean) object);
153-
return AbstractNull.INSTANCE;
168+
return AbstractNull.VALUE;
154169
}
155170

156171
default QueryString toFormData() {

src/main/java/org/javawebstack/abstractdata/AbstractNull.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.javawebstack.abstractdata;
22

3+
import org.javawebstack.abstractdata.exception.AbstractCoercingException;
4+
35
import java.util.HashMap;
46
import java.util.Map;
57

68
public class AbstractNull implements AbstractElement {
79

8-
public static final AbstractNull INSTANCE = new AbstractNull();
10+
public static final AbstractNull VALUE = new AbstractNull();
11+
@Deprecated
12+
public static final AbstractNull INSTANCE = VALUE;
913

1014
private AbstractNull() {
1115
}
@@ -14,6 +18,24 @@ public boolean isNull() {
1418
return true;
1519
}
1620

21+
public String string(boolean strict) throws AbstractCoercingException {
22+
if(strict)
23+
throw new AbstractCoercingException(Type.STRING, Type.NULL);
24+
return null;
25+
}
26+
27+
public Boolean bool(boolean strict) throws AbstractCoercingException {
28+
if(strict)
29+
throw new AbstractCoercingException(Type.STRING, Type.NULL);
30+
return null;
31+
}
32+
33+
public Number number(boolean strict) throws AbstractCoercingException {
34+
if(strict)
35+
throw new AbstractCoercingException(Type.STRING, Type.NULL);
36+
return null;
37+
}
38+
1739
public Object toObject() {
1840
return null;
1941
}

0 commit comments

Comments
 (0)