22
22
import java .util .ArrayList ;
23
23
import java .util .Collection ;
24
24
import java .util .ConcurrentModificationException ;
25
+ import java .util .EnumSet ;
25
26
import java .util .HashMap ;
26
27
import java .util .HashSet ;
27
28
import java .util .LinkedHashMap ;
@@ -55,13 +56,28 @@ public class Value {
55
56
56
57
private String path ;
57
58
58
- public Value (final Array array ) {
59
- type = array != null ? Type . Array : null ;
59
+ private Value (final Type type , final Array array , final Hash hash , final String string ) {
60
+ final boolean hasValue = array != null || hash != null || string != null ;
60
61
62
+ if (type == null ) {
63
+ if (hasValue ) {
64
+ throw new IllegalArgumentException ("Value without type" );
65
+ }
66
+ }
67
+ else {
68
+ if (!hasValue ) {
69
+ throw new IllegalArgumentException ("Type without value" );
70
+ }
71
+ }
72
+
73
+ this .type = type ;
61
74
this .array = array ;
62
- this .hash = null ;
63
- this .string = null ;
64
- this .path = null ;
75
+ this .hash = hash ;
76
+ this .string = string ;
77
+ }
78
+
79
+ public Value (final Array array ) {
80
+ this (array != null ? Type .Array : null , array , null , null );
65
81
}
66
82
67
83
public Value (final List <Value > array ) {
@@ -73,12 +89,7 @@ public Value(final List<Value> array) {
73
89
}
74
90
75
91
public Value (final Hash hash ) {
76
- type = hash != null ? Type .Hash : null ;
77
-
78
- this .array = null ;
79
- this .hash = hash ;
80
- this .string = null ;
81
- this .path = null ;
92
+ this (hash != null ? Type .Hash : null , null , hash , null );
82
93
}
83
94
84
95
public Value (final Map <String , Value > hash ) {
@@ -98,11 +109,7 @@ public Value(final int integer) {
98
109
}
99
110
100
111
public Value (final String string , final String path ) {
101
- type = string != null ? Type .String : null ;
102
-
103
- this .array = null ;
104
- this .hash = null ;
105
- this .string = string ;
112
+ this (string != null ? Type .String : null , null , null , string );
106
113
this .path = path ;
107
114
}
108
115
@@ -151,12 +158,7 @@ private boolean isType(final Type targetType) {
151
158
}
152
159
153
160
public boolean isNull () {
154
- return type == null || this .<Boolean >extractType ((m , c ) -> m
155
- .ifArray (a -> c .accept (a == null ))
156
- .ifHash (h -> c .accept (h == null ))
157
- .ifString (s -> c .accept (s == null ))
158
- .orElseThrow ()
159
- );
161
+ return isType (null );
160
162
}
161
163
162
164
public static boolean isNull (final Value value ) {
@@ -304,7 +306,7 @@ enum Type {
304
306
305
307
public static class TypeMatcher {
306
308
307
- private final Set <Type > expected = new HashSet <>( );
309
+ private final Set <Type > expected = EnumSet . noneOf ( Type . class );
308
310
private final Value value ;
309
311
310
312
private TypeMatcher (final Value value ) {
0 commit comments