Internally use a negated mask class to prevent russian doll wrapping (#1877)

This commit is contained in:
Maddy Miller 2021-08-21 15:45:57 +10:00 committed by GitHub
parent 9de6b87948
commit c5a4450373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,26 +66,12 @@ public static Mask negate(final Mask mask) {
return ALWAYS_FALSE;
} else if (mask instanceof AlwaysFalse) {
return ALWAYS_TRUE;
} else if (mask instanceof NegatedMask) {
return ((NegatedMask) mask).mask;
}
checkNotNull(mask);
return new AbstractMask() {
@Override
public boolean test(BlockVector3 vector) {
return !mask.test(vector);
}
@Nullable
@Override
public Mask2D toMask2D() {
Mask2D mask2d = mask.toMask2D();
if (mask2d != null) {
return negate(mask2d);
} else {
return null;
}
}
};
return new NegatedMask(mask);
}
/**
@ -99,15 +85,12 @@ public static Mask2D negate(final Mask2D mask) {
return ALWAYS_FALSE;
} else if (mask instanceof AlwaysFalse) {
return ALWAYS_TRUE;
} else if (mask instanceof NegatedMask2D) {
return ((NegatedMask2D) mask).mask;
}
checkNotNull(mask);
return new AbstractMask2D() {
@Override
public boolean test(BlockVector2 vector) {
return !mask.test(vector);
}
};
return new NegatedMask2D(mask);
}
/**
@ -167,4 +150,39 @@ public Mask2D toMask2D() {
}
}
private static class NegatedMask implements Mask {
private final Mask mask;
private NegatedMask(Mask mask) {
this.mask = mask;
}
@Override
public boolean test(BlockVector3 vector) {
return !mask.test(vector);
}
@Nullable
@Override
public Mask2D toMask2D() {
Mask2D mask2D = mask.toMask2D();
if (mask2D == null) {
return null;
}
return negate(mask2D);
}
}
private static class NegatedMask2D implements Mask2D {
private final Mask2D mask;
private NegatedMask2D(Mask2D mask) {
this.mask = mask;
}
@Override
public boolean test(BlockVector2 vector) {
return !mask.test(vector);
}
}
}