Skip to content

Commit 29c0faf

Browse files
committed
Eliminate TypeMatcher allocations in Value.isNull(). (#207)
A value is `null` iff its `type` is `null`. Ensure consistency through common constructor.
1 parent c7c824f commit 29c0faf

File tree

1 file changed

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

1 file changed

+23
-22
lines changed

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

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,28 @@ public class Value {
5656

5757
private String path;
5858

59-
public Value(final Array array) {
60-
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;
6161

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;
6274
this.array = array;
63-
this.hash = null;
64-
this.string = null;
65-
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);
6681
}
6782

6883
public Value(final List<Value> array) {
@@ -74,12 +89,7 @@ public Value(final List<Value> array) {
7489
}
7590

7691
public Value(final Hash hash) {
77-
type = hash != null ? Type.Hash : null;
78-
79-
this.array = null;
80-
this.hash = hash;
81-
this.string = null;
82-
this.path = null;
92+
this(hash != null ? Type.Hash : null, null, hash, null);
8393
}
8494

8595
public Value(final Map<String, Value> hash) {
@@ -99,11 +109,7 @@ public Value(final int integer) {
99109
}
100110

101111
public Value(final String string, final String path) {
102-
type = string != null ? Type.String : null;
103-
104-
this.array = null;
105-
this.hash = null;
106-
this.string = string;
112+
this(string != null ? Type.String : null, null, null, string);
107113
this.path = path;
108114
}
109115

@@ -152,12 +158,7 @@ private boolean isType(final Type targetType) {
152158
}
153159

154160
public boolean isNull() {
155-
return type == null || this.<Boolean>extractType((m, c) -> m
156-
.ifArray(a -> c.accept(a == null))
157-
.ifHash(h -> c.accept(h == null))
158-
.ifString(s -> c.accept(s == null))
159-
.orElseThrow()
160-
);
161+
return isType(null);
161162
}
162163

163164
public static boolean isNull(final Value value) {

0 commit comments

Comments
 (0)