diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2022-03-30 13:38:16 +0200 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2022-03-30 13:38:16 +0200 |
commit | a86835cf73cab55ca04932ee305f9653bce2ceb8 (patch) | |
tree | 8bd545dc72a4d63010313ec7ae3f0d524d8ef2f1 /vespajlib | |
parent | 2bc3dac20f6ec32f319bc850b6c1a8f28ecdfeed (diff) |
Less grammar, less indirection
Diffstat (limited to 'vespajlib')
4 files changed, 36 insertions, 62 deletions
diff --git a/vespajlib/src/main/java/ai/vespa/validation/Hostname.java b/vespajlib/src/main/java/ai/vespa/validation/Hostname.java index 87ef837492c..6a22aa1ab40 100644 --- a/vespajlib/src/main/java/ai/vespa/validation/Hostname.java +++ b/vespajlib/src/main/java/ai/vespa/validation/Hostname.java @@ -4,7 +4,6 @@ package ai.vespa.validation; import java.util.regex.Pattern; import static ai.vespa.validation.Validation.requireMatch; -import static ai.vespa.validation.Validation.validate; /** * A valid hostname. @@ -21,11 +20,11 @@ public class Hostname extends StringWrapper<Hostname> { } public static Hostname of(String hostname) { - return new Hostname(validate(hostname, "hostname", requireMatch(hostnamePattern))); - } + return new Hostname(requireMatch(hostname, "hostname", hostnamePattern)); + } public static String requireLabel(String label) { - return validate(label, "hostname label", requireMatch(hostnameLabel)); + return requireMatch(label, "hostname label", hostnameLabel); } } diff --git a/vespajlib/src/main/java/ai/vespa/validation/Name.java b/vespajlib/src/main/java/ai/vespa/validation/Name.java index 1869ba734b3..8a66d76c911 100644 --- a/vespajlib/src/main/java/ai/vespa/validation/Name.java +++ b/vespajlib/src/main/java/ai/vespa/validation/Name.java @@ -4,7 +4,6 @@ package ai.vespa.validation; import java.util.regex.Pattern; import static ai.vespa.validation.Validation.requireMatch; -import static ai.vespa.validation.Validation.validate; /** * A name is a non-null, non-blank {@link String} which starts with a letter, and has up to @@ -27,7 +26,7 @@ public class Name extends StringWrapper<Name> { }; public static Name of(String value, String description) { - return new Name(validate(value, description, requireMatch(namePattern))); + return new Name(requireMatch(value, description, namePattern)); }; } diff --git a/vespajlib/src/main/java/ai/vespa/validation/Validation.java b/vespajlib/src/main/java/ai/vespa/validation/Validation.java index f02096c478b..b494b3634f7 100644 --- a/vespajlib/src/main/java/ai/vespa/validation/Validation.java +++ b/vespajlib/src/main/java/ai/vespa/validation/Validation.java @@ -20,69 +20,48 @@ public class Validation { private Validation() { } /** Parses then given string, and then validates that each of the requirements are true for the parsed value. */ - @SafeVarargs - @SuppressWarnings("varargs") - public static <T> T validate(String value, Function<String, T> parser, String description, Predicate<? super T>... requirements) { - T parsed; + public static <T> T parse(String value, Function<String, T> parser, String description) { try { - parsed = parser.apply(requireNonNull(value, description + " cannot be null")); + return parser.apply(requireNonNull(value, description + " cannot be null")); } catch (RuntimeException e) { throw new IllegalArgumentException("failed parsing " + description + " '" + value + "': " + Exceptions.toMessageString(e)); } - return validate(parsed, description, requirements); - } - - /** Validates that each of the requirements are true for the given argument. */ - @SafeVarargs - @SuppressWarnings("varargs") - public static <T> T validate(T value, String description, Predicate<? super T>... requirements) { - for (Predicate<? super T> requirement : requirements) - if ( ! requirement.test(value)) - throw new IllegalArgumentException(description + " " + requirement + ", but got: '" + value + "'"); - - return value; } /** Requires arguments to match the given pattern. */ - public static Predicate<String> requireMatch(Pattern pattern) { - return require(s -> pattern.matcher(s).matches(), "must match '" + pattern + "'"); + public static String requireMatch(String value, String description, Pattern pattern) { + return require(pattern.matcher(value).matches(), value, description, "must match '" + pattern + "'"); } /** Requires arguments to be non-blank. */ - public static Predicate<String> requireNonBlank() { - return require(s -> ! s.isBlank(), "cannot be blank"); + public static String requireNonBlank(String value, String description) { + return require( ! value.isBlank(), value, description, "cannot be blank"); } /** Requires arguments to be at least the lower bound. */ - public static <T extends Comparable<? super T>> Predicate<T> requireAtLeast(T lower) { - requireNonNull(lower, "lower bound cannot be null"); - return require(c -> lower.compareTo(c) <= 0, "must be at least '" + lower + "'"); + public static <T extends Comparable<? super T>> T requireAtLeast(T value, String description, T lower) { + return require(lower.compareTo(value) <= 0, value, description, "must be at least '" + lower + "'"); } /** Requires arguments to be at most the upper bound. */ - public static <T extends Comparable<? super T>> Predicate<T> requireAtMost(T upper) { - requireNonNull(upper, "upper bound cannot be null"); - return require(c -> upper.compareTo(c) >= 0, "must be at most '" + upper + "'"); + public static <T extends Comparable<? super T>> T requireAtMost(T value, String description, T upper) { + return require(upper.compareTo(value) >= 0, value, description, "must be at most '" + upper + "'"); } /** Requires arguments to be at least the lower bound, and at most the upper bound. */ - public static <T extends Comparable<? super T>> Predicate<T> requireInRange(T lower, T upper) { - requireNonNull(lower, "lower bound cannot be null"); - requireNonNull(upper, "upper bound cannot be null"); + public static <T extends Comparable<? super T>> T requireInRange(T value, String description, T lower, T upper) { if (lower.compareTo(upper) > 0) throw new IllegalArgumentException("lower bound cannot be greater than upper bound, " + "but got '" + lower + "' > '" + upper + "'"); - return require(c -> lower.compareTo(c) <= 0 && upper.compareTo(c) >= 0, + return require(lower.compareTo(value) <= 0 && upper.compareTo(value) >= 0, value, description, "must be at least '" + lower + "' and at most '" + upper + "'"); } /** Wraps a predicate with a message describing it. */ - public static <T> Predicate<T> require(Predicate<T> predicate, String message) { - return new Predicate<T>() { - @Override public boolean test(T t) { return predicate.test(t); } - @Override public String toString() { return message; } - }; + public static <T> T require(boolean condition, T value, String description, String message) { + if (condition) return value; + throw new IllegalArgumentException(description + " " + message + ", but got: '" + value + "'"); } }
\ No newline at end of file diff --git a/vespajlib/src/test/java/ai/vespa/validation/ValidationTest.java b/vespajlib/src/test/java/ai/vespa/validation/ValidationTest.java index c3b0730a5ee..0d3fe81ead6 100644 --- a/vespajlib/src/test/java/ai/vespa/validation/ValidationTest.java +++ b/vespajlib/src/test/java/ai/vespa/validation/ValidationTest.java @@ -1,15 +1,13 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package ai.vespa.validation; -import com.google.common.base.Preconditions; import org.junit.jupiter.api.Test; -import static ai.vespa.validation.Validation.require; +import static ai.vespa.validation.Validation.parse; import static ai.vespa.validation.Validation.requireAtLeast; import static ai.vespa.validation.Validation.requireAtMost; import static ai.vespa.validation.Validation.requireInRange; import static ai.vespa.validation.Validation.requireNonBlank; -import static ai.vespa.validation.Validation.validate; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -20,54 +18,53 @@ class ValidationTest { @Test void testNumberComparisons() { - assertEquals(3.14, validate("3.14", Double::parseDouble, "pi", - requireInRange(3.14, 3.14), - requireInRange(0.0, Double.POSITIVE_INFINITY), - requireInRange(Double.NEGATIVE_INFINITY, 3.14), - requireAtLeast(3.14), - requireAtMost(3.14))); + assertEquals(3.14, parse("3.14", Double::parseDouble, "pi")); + requireInRange(3.14, "pi", 3.14, 3.14); + requireInRange(3.14, "pi", 0.0, Double.POSITIVE_INFINITY); + requireInRange(3.14, "pi", Double.NEGATIVE_INFINITY, 3.14); + requireAtLeast(3.14, "pi", 3.14); + requireAtMost(3.14, "pi", 3.14); assertEquals("lower bound cannot be greater than upper bound, but got '1.0' > '0.1'", assertThrows(IllegalArgumentException.class, - () -> validate(3.14, "pi", requireInRange(1.0, 0.1))) + () -> requireInRange(3.14, "pi", 1.0, 0.1)) .getMessage()); assertEquals("pi must be at least '0.0' and at most '0.0', but got: '3.14'", assertThrows(IllegalArgumentException.class, - () -> validate(3.14, "pi", requireInRange(0.0, 0.0))) + () -> requireInRange(3.14, "pi", 0.0, 0.0)) .getMessage()); assertEquals("pi must be at least '4.0' and at most '4.0', but got: '3.14'", assertThrows(IllegalArgumentException.class, - () -> validate(3.14, "pi", requireInRange(4.0, 4.0))) + () -> requireInRange(3.14, "pi", 4.0, 4.0)) .getMessage()); assertEquals("pi must be at least '4.0', but got: '3.14'", assertThrows(IllegalArgumentException.class, - () -> validate(3.14, "pi", requireAtLeast(4.0))) + () -> requireAtLeast(3.14, "pi", 4.0)) .getMessage()); assertEquals("pi must be at most '3.0', but got: '3.14'", assertThrows(IllegalArgumentException.class, - () -> validate(3.14, "pi", requireAtMost(3.0))) + () -> requireAtMost(3.14, "pi", 3.0)) .getMessage()); } @Test void testStringComparisons() { - assertEquals("hei", validate("hei", "word", - requireNonBlank(), - require(__ -> true, "nothing"), - requireInRange("hai", "hoi"))); + assertEquals("hei", requireNonBlank("hei", "word")); + + requireInRange("hei", "word", "hai", "hoi"); assertEquals("word cannot be blank, but got: ''", assertThrows(IllegalArgumentException.class, - () -> validate("", "word", requireNonBlank())) + () -> requireNonBlank("", "word")) .getMessage()); assertEquals("lower bound cannot be greater than upper bound, but got 'hoi' > 'hai'", assertThrows(IllegalArgumentException.class, - () -> validate("hei", "word", requireInRange("hoi", "hai"))) + () -> requireInRange("hei", "word", "hoi", "hai")) .getMessage()); } |