Skip to content

Commit 4cd18b9

Browse files
committed
Revert "Use primitive bit field for TypeMatcher instead of elaborate data structure. (#207)"
This reverts commit c7c824f. Decided against in #224.
1 parent 29c0faf commit 4cd18b9

File tree

1 file changed

+5
-20
lines changed
  • metafix/src/main/java/org/metafacture/metafix

1 file changed

+5
-20
lines changed

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

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
import org.metafacture.commons.tries.WildcardTrie;
2121

2222
import java.util.ArrayList;
23-
import java.util.Arrays;
2423
import java.util.Collection;
2524
import java.util.ConcurrentModificationException;
25+
import java.util.EnumSet;
2626
import java.util.HashMap;
2727
import java.util.HashSet;
2828
import java.util.LinkedHashMap;
@@ -306,10 +306,9 @@ enum Type {
306306

307307
public static class TypeMatcher {
308308

309+
private final Set<Type> expected = EnumSet.noneOf(Type.class);
309310
private final Value value;
310311

311-
private byte expected; // NOTE: Covers at most 7 `Type`s.
312-
313312
private TypeMatcher(final Value value) {
314313
this.value = value;
315314
}
@@ -327,26 +326,20 @@ public TypeMatcher ifString(final Consumer<String> consumer) {
327326
}
328327

329328
public void orElse(final Consumer<Value> consumer) {
330-
if (!expecting(value.type)) {
329+
if (!expected.contains(value.type)) {
331330
consumer.accept(value);
332331
}
333332
}
334333

335334
public void orElseThrow() {
336335
orElse(v -> {
337-
final String types = Arrays.stream(Type.values()).filter(this::expecting)
338-
.map(Type::name).collect(Collectors.joining(" or "));
339-
336+
final String types = expected.stream().map(Type::name).collect(Collectors.joining(" or "));
340337
throw new IllegalStateException("Expected " + types + ", got " + value.type);
341338
});
342339
}
343340

344341
private <T> TypeMatcher match(final Type type, final Consumer<T> consumer, final T rawValue) {
345-
final byte newExpected = (byte) (expected | bit(type));
346-
347-
if (expected != newExpected) {
348-
expected = newExpected;
349-
342+
if (expected.add(type)) {
350343
if (value.isType(type)) {
351344
consumer.accept(rawValue);
352345
}
@@ -358,14 +351,6 @@ private <T> TypeMatcher match(final Type type, final Consumer<T> consumer, final
358351
}
359352
}
360353

361-
private boolean expecting(final Type type) {
362-
return (expected & bit(type)) != 0;
363-
}
364-
365-
private byte bit(final Type type) {
366-
return (byte) (1 << type.ordinal());
367-
}
368-
369354
}
370355

371356
private abstract static class AbstractValueType {

0 commit comments

Comments
 (0)