SHA256
60 lines
2.2 KiB
Java
60 lines
2.2 KiB
Java
package shine.db.channels;
|
|
|
|
import java.util.Locale;
|
|
import java.util.regex.Pattern;
|
|
|
|
public final class ChannelNameRules {
|
|
private static final int MIN_DISPLAY_NAME_LENGTH = 3;
|
|
private static final int MAX_DISPLAY_NAME_LENGTH = 32;
|
|
private static final Pattern DISPLAY_ALLOWED_PATTERN =
|
|
Pattern.compile("^[A-Za-z0-9_-]+$");
|
|
private static final Pattern PUBLIC_CHANNEL_ALLOWED_PATTERN =
|
|
Pattern.compile("^[A-Za-z0-9_-]+$");
|
|
|
|
private ChannelNameRules() {}
|
|
|
|
public static String normalizeDisplayName(String value) {
|
|
if (value == null) return "";
|
|
return value.trim();
|
|
}
|
|
|
|
public static String requireValidDisplayNameForCreate(String rawName) {
|
|
String normalized = normalizeDisplayName(rawName);
|
|
if (normalized.isEmpty()) {
|
|
throw new IllegalArgumentException("channelName is blank");
|
|
}
|
|
|
|
int length = normalized.codePointCount(0, normalized.length());
|
|
if (length < MIN_DISPLAY_NAME_LENGTH || length > MAX_DISPLAY_NAME_LENGTH) {
|
|
throw new IllegalArgumentException("channelName length must be 3..32");
|
|
}
|
|
|
|
if (!DISPLAY_ALLOWED_PATTERN.matcher(normalized).matches()) {
|
|
throw new IllegalArgumentException("channelName contains unsupported characters");
|
|
}
|
|
|
|
return normalized;
|
|
}
|
|
|
|
public static String requireValidPublicDisplayNameForCreate(String rawName) {
|
|
String normalized = requireValidDisplayNameForCreate(rawName);
|
|
if (!PUBLIC_CHANNEL_ALLOWED_PATTERN.matcher(normalized).matches()) {
|
|
throw new IllegalArgumentException("channelName contains unsupported characters");
|
|
}
|
|
return normalized;
|
|
}
|
|
|
|
public static String toCanonicalSlug(String rawName) {
|
|
String normalized = normalizeDisplayName(rawName);
|
|
if (normalized.isEmpty()) {
|
|
throw new IllegalArgumentException("channelName is blank");
|
|
}
|
|
|
|
String lowered = normalized.toLowerCase(Locale.ROOT);
|
|
if (!DISPLAY_ALLOWED_PATTERN.matcher(lowered).matches()) {
|
|
throw new IllegalArgumentException("channelName contains unsupported characters");
|
|
}
|
|
return lowered;
|
|
}
|
|
}
|