|
| 1 | +package checkmo.common.nickname; |
| 2 | + |
| 3 | +import java.text.Normalizer; |
| 4 | +import java.util.Locale; |
| 5 | + |
| 6 | +public final class NicknamePolicy { |
| 7 | + |
| 8 | + public static final int MAX_LENGTH = 20; |
| 9 | + public static final String ALLOWED_SPECIAL_CHARACTERS = "!@#$%^&*()_+-=[]{};':\"\\|,.<>/?"; |
| 10 | + |
| 11 | + private NicknamePolicy() { |
| 12 | + } |
| 13 | + |
| 14 | + public static String normalize(String nickname) { |
| 15 | + if (nickname == null) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + return Normalizer.normalize(nickname, Normalizer.Form.NFC); |
| 19 | + } |
| 20 | + |
| 21 | + public static String comparisonKey(String nickname) { |
| 22 | + String normalized = normalize(nickname); |
| 23 | + if (normalized == null || normalized.isEmpty()) { |
| 24 | + return null; |
| 25 | + } |
| 26 | + return normalized.toLowerCase(Locale.ROOT); |
| 27 | + } |
| 28 | + |
| 29 | + public static String normalizeForStorage(String nickname) { |
| 30 | + ValidationError validationError = validate(nickname, false); |
| 31 | + if (validationError != ValidationError.NONE) { |
| 32 | + throw new IllegalArgumentException(validationError.getMessage()); |
| 33 | + } |
| 34 | + |
| 35 | + String normalized = normalize(nickname); |
| 36 | + return normalized == null || normalized.isEmpty() ? null : normalized; |
| 37 | + } |
| 38 | + |
| 39 | + public static boolean isSameIdentity(String first, String second) { |
| 40 | + String firstKey = comparisonKey(first); |
| 41 | + return firstKey != null && firstKey.equals(comparisonKey(second)); |
| 42 | + } |
| 43 | + |
| 44 | + public static ValidationError validate(String nickname, boolean required) { |
| 45 | + if (nickname == null || nickname.isEmpty()) { |
| 46 | + return required ? ValidationError.REQUIRED : ValidationError.NONE; |
| 47 | + } |
| 48 | + |
| 49 | + String normalized = normalize(nickname); |
| 50 | + if (normalized.codePoints().anyMatch(NicknamePolicy::isWhitespace)) { |
| 51 | + return ValidationError.WHITESPACE; |
| 52 | + } |
| 53 | + if (normalized.codePointCount(0, normalized.length()) > MAX_LENGTH) { |
| 54 | + return ValidationError.TOO_LONG; |
| 55 | + } |
| 56 | + if (!normalized.codePoints().allMatch(NicknamePolicy::isAllowed)) { |
| 57 | + return ValidationError.INVALID_CHARACTER; |
| 58 | + } |
| 59 | + return ValidationError.NONE; |
| 60 | + } |
| 61 | + |
| 62 | + private static boolean isWhitespace(int codePoint) { |
| 63 | + return Character.isWhitespace(codePoint) || Character.isSpaceChar(codePoint); |
| 64 | + } |
| 65 | + |
| 66 | + private static boolean isAllowed(int codePoint) { |
| 67 | + return isHangulSyllable(codePoint) |
| 68 | + || isHangulCompatibilityJamo(codePoint) |
| 69 | + || isAsciiLetter(codePoint) |
| 70 | + || isAsciiDigit(codePoint) |
| 71 | + || ALLOWED_SPECIAL_CHARACTERS.indexOf(codePoint) >= 0; |
| 72 | + } |
| 73 | + |
| 74 | + private static boolean isHangulSyllable(int codePoint) { |
| 75 | + return codePoint >= '\uAC00' && codePoint <= '\uD7A3'; |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean isHangulCompatibilityJamo(int codePoint) { |
| 79 | + return (codePoint >= '\u3131' && codePoint <= '\u314E') |
| 80 | + || (codePoint >= '\u314F' && codePoint <= '\u3163'); |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean isAsciiLetter(int codePoint) { |
| 84 | + return (codePoint >= 'A' && codePoint <= 'Z') |
| 85 | + || (codePoint >= 'a' && codePoint <= 'z'); |
| 86 | + } |
| 87 | + |
| 88 | + private static boolean isAsciiDigit(int codePoint) { |
| 89 | + return codePoint >= '0' && codePoint <= '9'; |
| 90 | + } |
| 91 | + |
| 92 | + public enum ValidationError { |
| 93 | + NONE(""), |
| 94 | + REQUIRED("닉네임은 필수입니다."), |
| 95 | + WHITESPACE("닉네임에는 공백을 사용할 수 없습니다."), |
| 96 | + TOO_LONG("닉네임은 최대 20자까지 가능합니다."), |
| 97 | + INVALID_CHARACTER("닉네임은 한글, 영문, 숫자, 허용된 특수문자만 사용할 수 있습니다."); |
| 98 | + |
| 99 | + private final String message; |
| 100 | + |
| 101 | + ValidationError(String message) { |
| 102 | + this.message = message; |
| 103 | + } |
| 104 | + |
| 105 | + public String getMessage() { |
| 106 | + return message; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments