| 1 | package com.ancientprogramming.fixedformat4j.format.impl; | |
| 2 | ||
| 3 | import com.ancientprogramming.fixedformat4j.exception.FixedFormatException; | |
| 4 | ||
| 5 | import java.text.SimpleDateFormat; | |
| 6 | import java.time.LocalDate; | |
| 7 | import java.time.LocalDateTime; | |
| 8 | import java.time.format.DateTimeFormatter; | |
| 9 | import java.util.Date; | |
| 10 | ||
| 11 | /** | |
| 12 | * Validates date/time pattern strings for the types supported by this library. | |
| 13 | * | |
| 14 | * <p>Validation is performed eagerly — before any data is parsed or formatted — so that | |
| 15 | * configuration errors surface immediately when a record class is first used, rather than | |
| 16 | * at the point a specific field is processed. | |
| 17 | * | |
| 18 | * @author Jacob von Eyben - <a href="https://eybenconsult.com">https://eybenconsult.com</a> | |
| 19 | * @since 1.6.0 | |
| 20 | */ | |
| 21 | class PatternValidator { | |
| 22 | ||
| 23 | /** | |
| 24 | * Validates that {@code pattern} is a legal format pattern for {@code datatype}. | |
| 25 | * | |
| 26 | * <p>Validation is performed only for date/time types ({@link Date}, {@link LocalDate}, | |
| 27 | * {@link LocalDateTime}). For all other types the method returns immediately. | |
| 28 | * | |
| 29 | * @param datatype the field's Java type | |
| 30 | * @param pattern the pattern string from {@link com.ancientprogramming.fixedformat4j.annotation.FixedFormatPattern} | |
| 31 | * @throws FixedFormatException if the pattern is invalid for the given type | |
| 32 | */ | |
| 33 | static void validate(Class<?> datatype, String pattern) { | |
| 34 |
1
1. validate : negated conditional → KILLED |
if (Date.class.equals(datatype)) { |
| 35 |
1
1. validate : removed call to com/ancientprogramming/fixedformat4j/format/impl/PatternValidator::validateSimpleDateFormat → KILLED |
validateSimpleDateFormat(pattern); |
| 36 |
2
1. validate : negated conditional → KILLED 2. validate : negated conditional → KILLED |
} else if (LocalDate.class.equals(datatype) || LocalDateTime.class.equals(datatype)) { |
| 37 |
1
1. validate : removed call to com/ancientprogramming/fixedformat4j/format/impl/PatternValidator::validateDateTimeFormatter → KILLED |
validateDateTimeFormatter(pattern); |
| 38 | } | |
| 39 | } | |
| 40 | ||
| 41 | private static void validateSimpleDateFormat(String pattern) { | |
| 42 | try { | |
| 43 | new SimpleDateFormat(pattern); | |
| 44 | } catch (IllegalArgumentException e) { | |
| 45 | throw new FixedFormatException(String.format("Invalid date pattern '%s': %s", pattern, e.getMessage()), e); | |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | private static void validateDateTimeFormatter(String pattern) { | |
| 50 | try { | |
| 51 | DateTimeFormatter.ofPattern(pattern); | |
| 52 | } catch (IllegalArgumentException e) { | |
| 53 | throw new FixedFormatException(String.format("Invalid date/time pattern '%s': %s", pattern, e.getMessage()), e); | |
| 54 | } | |
| 55 | } | |
| 56 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 35 |
1.1 |
|
| 36 |
1.1 2.2 |
|
| 37 |
1.1 |