feat(04-03): implement DefineValidation helpers (name regex + componentwise min/max)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package com.mythlane.gravityflip.command;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Pure-data validation helpers for {@code /gravityflip define <name>}.
|
||||
*
|
||||
* <p>No Hytale runtime dependency — same philosophy as {@code FallDamageGuard}
|
||||
* and {@code WandSelectionStore}. Testable with JUnit alone.
|
||||
*
|
||||
* <p>Exposes :
|
||||
* <ul>
|
||||
* <li>{@link #isValidName(String)} — region name regex gate.</li>
|
||||
* <li>{@link #componentwiseMin(int[], int[])} / {@link #componentwiseMax(int[], int[])}
|
||||
* — so the builder can click pos1/pos2 in any order.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>See {@code DefineValidationTest} for the accepted/rejected-name corpus and
|
||||
* the rationale for the inflate-max-by-1 convention applied in
|
||||
* {@code GravityFlipDefineSubCommand}.
|
||||
*/
|
||||
public final class DefineValidation {
|
||||
|
||||
private static final Pattern NAME = Pattern.compile("^[a-zA-Z0-9_-]{1,32}$");
|
||||
|
||||
private DefineValidation() {}
|
||||
|
||||
/**
|
||||
* Returns {@code true} iff {@code n} matches {@code ^[a-zA-Z0-9_-]{1,32}$}.
|
||||
* Rejects {@code null}, blank, spaces, path separators, non-ASCII.
|
||||
*/
|
||||
public static boolean isValidName(String n) {
|
||||
return n != null && NAME.matcher(n).matches();
|
||||
}
|
||||
|
||||
/** Returns {@code {min(a.x,b.x), min(a.y,b.y), min(a.z,b.z)}}. */
|
||||
public static int[] componentwiseMin(int[] a, int[] b) {
|
||||
return new int[]{
|
||||
Math.min(a[0], b[0]),
|
||||
Math.min(a[1], b[1]),
|
||||
Math.min(a[2], b[2])
|
||||
};
|
||||
}
|
||||
|
||||
/** Returns {@code {max(a.x,b.x), max(a.y,b.y), max(a.z,b.z)}}. */
|
||||
public static int[] componentwiseMax(int[] a, int[] b) {
|
||||
return new int[]{
|
||||
Math.max(a[0], b[0]),
|
||||
Math.max(a[1], b[1]),
|
||||
Math.max(a[2], b[2])
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user