|
| 1 | +package lotto.util; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +public class Validator { |
| 8 | + |
| 9 | + private final static int VALIDATE_LENGTH = 6; |
| 10 | + |
| 11 | + //여기서는 number밖에 안썼지만 확장성을 고려해 설계해봤습니다. |
| 12 | + public enum dataType {NUMBER, ALPHABET;} |
| 13 | + |
| 14 | + public static <T> List<T> isMultipleInputType(String inputLine, dataType dataType, String separator) { |
| 15 | + isBlank(inputLine); |
| 16 | + |
| 17 | + List<String> process = List.of(splitter(inputLine, separator)); |
| 18 | + isLength(process.size()); |
| 19 | + |
| 20 | + return process.stream() |
| 21 | + .map(value -> isSingleInputType(value, dataType)) |
| 22 | + .map(element -> (T) element) |
| 23 | + .toList(); |
| 24 | + } |
| 25 | + |
| 26 | + public static <T> T isSingleInputType(String inputLine, dataType dataType) { |
| 27 | + isBlank(inputLine); |
| 28 | + if (dataType == Validator.dataType.ALPHABET) { |
| 29 | + isAlphabet(inputLine); |
| 30 | + return (T) inputLine; |
| 31 | + } |
| 32 | + if (dataType == Validator.dataType.NUMBER) { |
| 33 | + isNumber(inputLine); |
| 34 | + return (T) Integer.valueOf(inputLine); |
| 35 | + } |
| 36 | + throw new IllegalArgumentException(DetailErrorMessage.DEV_TYPE_WRONG.getMessage()); |
| 37 | + } |
| 38 | + |
| 39 | + public static <T> Set<T> isListItemDuplicated(List<T> inputList) { |
| 40 | + Set<T> glossary = new HashSet<>(); |
| 41 | + for (T item : inputList) { |
| 42 | + if (!glossary.add(item)) { |
| 43 | + throw new IllegalArgumentException(DetailErrorMessage.DUPLICATED.getMessage()); |
| 44 | + } |
| 45 | + } |
| 46 | + return glossary; |
| 47 | + } |
| 48 | + |
| 49 | + public static void isListItemInRange(int startRange, int endRangeIncluded, List<Integer> inputList) { |
| 50 | + for (int item : inputList) { |
| 51 | + if (item > endRangeIncluded || item < startRange) { |
| 52 | + throw new IllegalArgumentException(DetailErrorMessage.NOT_RANGE.getMessage()); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static void isBlank(String inputLine) { |
| 58 | + if (inputLine.isBlank()) { |
| 59 | + throw new IllegalArgumentException(DetailErrorMessage.BLANK.getMessage()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public static void isNumber(String inputLine) { |
| 64 | + if (!inputLine.matches("\\d+")) { |
| 65 | + throw new NumberFormatException(DetailErrorMessage.NOT_NUMBER.getMessage()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static String isAlphabet(String inputLine) { |
| 70 | + if (!inputLine.matches("/\\^[a-zA-Z ]*\\$/")) { |
| 71 | + throw new IllegalArgumentException(DetailErrorMessage.NOT_ALPHABET.getMessage()); |
| 72 | + } |
| 73 | + return inputLine; |
| 74 | + } |
| 75 | + |
| 76 | + public static void isLength(int length) { |
| 77 | + if (length != VALIDATE_LENGTH) { |
| 78 | + throw new IllegalArgumentException(DetailErrorMessage.NOT_LENGTH.getMessage()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + static String[] splitter(String inputLine, String separator) { |
| 83 | + try { |
| 84 | + return inputLine.split(separator); |
| 85 | + } catch (Error e) { |
| 86 | + throw new IllegalArgumentException(DetailErrorMessage.NOT_COMMA.getMessage()); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments