29 lines
1.1 KiB
Java
29 lines
1.1 KiB
Java
package games.dmg.fantasystick.party;
|
|
|
|
import java.text.Normalizer;
|
|
import java.util.Locale;
|
|
import java.util.Optional;
|
|
|
|
/** Validated display and globally comparable identities for persistent parties. */
|
|
public final class PartyNames {
|
|
public record Name(String display, String canonical) { }
|
|
|
|
private PartyNames() { }
|
|
|
|
public static Optional<Name> parse(String input) {
|
|
if (input == null) return Optional.empty();
|
|
String display = Normalizer.normalize(input.strip(), Normalizer.Form.NFC);
|
|
int length = display.codePointCount(0, display.length());
|
|
if (length < 1 || length > 32 || display.codePoints().anyMatch(PartyNames::forbidden)) {
|
|
return Optional.empty();
|
|
}
|
|
String canonical = Normalizer.normalize(display.toLowerCase(Locale.ROOT), Normalizer.Form.NFC);
|
|
return Optional.of(new Name(display, canonical));
|
|
}
|
|
|
|
private static boolean forbidden(int codePoint) {
|
|
int type = Character.getType(codePoint);
|
|
return type == Character.CONTROL || type == Character.FORMAT || type == Character.SURROGATE;
|
|
}
|
|
}
|