Skip to content

Commit e35f821

Browse files
authored
Merge pull request #224 from metafacture/207-optimizeTypeMatcher
Optimize `TypeMatcher` data structure.
2 parents 42cc86f + 4cd18b9 commit e35f821

File tree

1 file changed

+25
-23
lines changed
  • metafix/src/main/java/org/metafacture/metafix

1 file changed

+25
-23
lines changed

metafix/src/main/java/org/metafacture/metafix/Value.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Collection;
2424
import java.util.ConcurrentModificationException;
25+
import java.util.EnumSet;
2526
import java.util.HashMap;
2627
import java.util.HashSet;
2728
import java.util.LinkedHashMap;
@@ -55,13 +56,28 @@ public class Value {
5556

5657
private String path;
5758

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;
6061

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;
6174
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);
6581
}
6682

6783
public Value(final List<Value> array) {
@@ -73,12 +89,7 @@ public Value(final List<Value> array) {
7389
}
7490

7591
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);
8293
}
8394

8495
public Value(final Map<String, Value> hash) {
@@ -98,11 +109,7 @@ public Value(final int integer) {
98109
}
99110

100111
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);
106113
this.path = path;
107114
}
108115

@@ -151,12 +158,7 @@ private boolean isType(final Type targetType) {
151158
}
152159

153160
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);
160162
}
161163

162164
public static boolean isNull(final Value value) {
@@ -304,7 +306,7 @@ enum Type {
304306

305307
public static class TypeMatcher {
306308

307-
private final Set<Type> expected = new HashSet<>();
309+
private final Set<Type> expected = EnumSet.noneOf(Type.class);
308310
private final Value value;
309311

310312
private TypeMatcher(final Value value) {

0 commit comments

Comments
 (0)