diff --git a/README.md b/README.md index 21b312509..6ef44812f 100644 --- a/README.md +++ b/README.md @@ -30,14 +30,24 @@ To get started, add this Maven dependency: Designed for discovery using your favorite IDE's auto-complete feature. The main entry points are: -* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/DefaultJavaValidators.html#requireThat(T,java.lang.String)) for preconditions -* [assumeThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/DefaultJavaValidators.html#assumeThat(T,java.lang.String)) for postconditions and class invariants -* [checkIfThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/DefaultJavaValidators.html#checkIf(T,java.lang.String)) for multiple failures and everything else -* [JavaValidators](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/JavaValidators.html) for custom configurations +* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/java/DefaultJavaValidators.html#requireThat(T,java.lang.String)) for method preconditions. +* [assumeThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/java/DefaultJavaValidators.html#assumeThat(T,java.lang.String)) for class invariants, method postconditions and private methods. +* [checkIfThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/java/DefaultJavaValidators.html#checkIf(T,java.lang.String)) for multiple failures and customized error handling. +* [JavaValidators](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/java/JavaValidators.html) for custom configurations. The first three methods use a shared configuration, while `JavaValidators` allows you to create an independent configuration. +`requireThat()` and `assumeThat()` throw an exception on the first validation failure, +while `checkIf()` collects multiple validation failures before throwing an exception at the end. +`checkIf()` is more flexible than the others, but its syntax is more verbose. + +Exceptions that are thrown in response to invalid method arguments (e.g. `isGreaterThan(null, value)`) are +thrown by all validators and cannot be configured. Exceptions that are thrown in response to the value +failing a validation check, e.g. `isGreaterThan(5)` on a value of 0, are thrown by `requireThat()` and +`assumeThat()` but are recorded by `checkIf()` without being thrown. The type of thrown exceptions is +configurable using [ConfigurationUpdater#exceptionTransformer(Function)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/java/ConfigurationUpdater.html#exceptionTransformer(java.util.function.Function)). + See the [API documentation](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/) for more details. ## Usage Example @@ -105,31 +115,13 @@ This library offers the following features: * [String diff](docs/Features.md#string-diff) that shows the differences between two strings * [Performant and robust](docs/Performance.md) -## Getting Started - -The best way to learn about the API is using your IDE's auto-complete engine. -The main entry points you should be aware of are: - -* [requireThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/DefaultJavaValidators.html#requireThat(T,java.lang.String)) -* [assumeThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/DefaultJavaValidators.html#assumeThat(T,java.lang.String)) -* [checkIfThat(value, name)](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/DefaultJavaValidators.html#checkIf(T,java.lang.String)) - -The three static methods share the same configuration. -To create an independent configuration, use [JavaValidators](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/com.github.cowwoc.requirements/com/github/cowwoc/requirements/JavaValidators.html). - -See the [API documentation](https://cowwoc.github.io/requirements.java/9.0.0/docs/api/) for more details. +## Tips -## Best practices - -* Use `requireThat()` to verify the preconditions of public APIs. -* Use `assert assumeThat()` to verify class invariants, method post-conditions, and the preconditions of - private methods. - The JVM will remove these checks if assertions are disabled. -* Use `checkIf()` to return multiple failures at once. -* Use `checkIf().elseGetMessages()` to return failures without throwing an exception. - This is the best-performing approach, ideal for web services. -* To enhance the clarity of failure messages, you should provide parameter names, even though they are - optional. +* Use `assert` with `assumeThat().elseThrow()` for sanity checks. When assertions are disabled, they will get + disabled. +* Use `checkIf().elseGetMessages()` to return failure messages without throwing an exception. + This is the fastest validation approach, ideal for web services. +* To enhance the clarity of failure messages, you should provide parameter names, even when they are optional. ## Third-party libraries and tools diff --git a/docs/Changelog.md b/docs/Changelog.md index aba5e1d9c..a7dca1a41 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -28,6 +28,7 @@ See https://github.com/cowwoc/requirements.java/commits/master for a full list. 14. Dropped the `isOneOf()` and `isNotOneOf()` functionality yet again. I haven't figured out a good design for this yet. 15. Added `ObjectValidator.isX()` methods to downcast to known types. + 16. Replaced thread context with `Validators.getContext()`, `putContext()`, `removeContext()`. * Bugfixes: * `StringValidator/Verifier.asShort()`, `asInteger()` and `asLong()` were not handling the case where a string could not be converted to a number. diff --git a/guava/src/main/java/com/github/cowwoc/requirements/guava/DefaultGuavaValidators.java b/guava/src/main/java/com/github/cowwoc/requirements/guava/DefaultGuavaValidators.java index 0bdff7c87..13444aaa6 100644 --- a/guava/src/main/java/com/github/cowwoc/requirements/guava/DefaultGuavaValidators.java +++ b/guava/src/main/java/com/github/cowwoc/requirements/guava/DefaultGuavaValidators.java @@ -9,11 +9,13 @@ import com.github.cowwoc.requirements.java.Configuration; import com.github.cowwoc.requirements.java.ConfigurationUpdater; import com.github.cowwoc.requirements.java.GlobalConfiguration; -import com.github.cowwoc.requirements.java.ScopedContext; import com.github.cowwoc.requirements.java.internal.scope.MainApplicationScope; +import com.github.cowwoc.requirements.java.internal.util.CloseableLock; +import com.github.cowwoc.requirements.java.internal.util.ReentrantStampedLock; import com.github.cowwoc.requirements.java.type.part.Validator; import com.google.common.collect.Multimap; +import java.util.Map; import java.util.function.Function; /** @@ -36,6 +38,8 @@ * of 0, are thrown by {@code requireThat()} and {@code assumeThat()} but are recorded by {@code checkIf()} * without being thrown. The type of thrown exceptions is configurable using * {@link ConfigurationUpdater#exceptionTransformer(Function)}. + *

+ * Thread Safety: This class is thread-safe. * * @see GuavaValidators#newInstance() Creating a new instance with an independent configuration */ @@ -43,6 +47,7 @@ public final class DefaultGuavaValidators { private static final GuavaValidatorsImpl delegate = new GuavaValidatorsImpl(MainApplicationScope.INSTANCE, Configuration.DEFAULT); + private static final ReentrantStampedLock contextLock = new ReentrantStampedLock(); /** * Validates the state of a {@code Multimap}. Any exceptions thrown due to validation failure are @@ -54,7 +59,7 @@ public final class DefaultGuavaValidators * @param value the value * @param name the name of the value * @return a validator for the value - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty */ public static > MultimapValidator assumeThat(T value, String name) @@ -86,7 +91,7 @@ public static > MultimapValidator assume * @param value the value * @param name the name of the value * @return a validator for the value - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty */ public static > MultimapValidator checkIf(T value, String name) @@ -117,7 +122,7 @@ public static > MultimapValidator checkI * @param value the value * @param name the name of the value * @return a validator for the value - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty */ public static > MultimapValidator requireThat(T value, String name) @@ -150,26 +155,56 @@ public static ConfigurationUpdater updateConfiguration() } /** - * Returns the contextual information for validations performed by this thread using any validator. The - * contextual information is a map of key-value pairs that can provide more details about validation - * failures. For example, if the message is "Password may not be empty" and the map contains the key-value - * pair {@code {"username": "john.smith"}}, the exception message would be: + * Returns the contextual information for validators created out by this factory. The contextual information + * is a map of key-value pairs that can provide more details about validation failures. For example, if the + * message is "Password may not be empty" and the map contains the key-value pair + * {@code {"username": "john.smith"}}, the exception message would be: *

* {@snippet lang = output: * Password may not be empty * username: john.smith} + * + * @return an unmodifiable map from each entry's name to its value + */ + public static Map getContext() + { + return contextLock.optimisticRead(delegate::getContext); + } + + /** + * Sets the contextual information for validators created by this factory. *

- * Values set by this method may be overridden by {@link Validator#putContext(Object, String)}}. - *

- * NOTE: This method affects existing and new validators used by current thread. Changes are - * reversed once {@link ScopedContext#close()} is invoked. + * This method adds contextual information to exception messages. The contextual information is stored as + * key-value pairs in a map. Values set by this method may be overridden by + * {@link Validator#putContext(Object, String)}}. * - * @return the thread context updater + * @param value the value of the entry + * @param name the name of an entry + * @return the underlying validator factory + * @throws NullPointerException if {@code name} is null */ - @CheckReturnValue - public static ScopedContext threadContext() + public static GuavaValidators putContext(Object value, String name) + { + try (CloseableLock unused = contextLock.write()) + { + return delegate.putContext(value, name); + } + } + + /** + * Removes the contextual information of validators created by this factory. + * + * @param name the parameter name + * @return the underlying validator factory + * @throws NullPointerException if {@code name} is null + * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty + */ + public static GuavaValidators removeContext(String name) { - return delegate.threadContext(); + try (CloseableLock unused = contextLock.write()) + { + return delegate.removeContext(name); + } } /** diff --git a/guava/src/main/java/com/github/cowwoc/requirements/guava/GuavaValidators.java b/guava/src/main/java/com/github/cowwoc/requirements/guava/GuavaValidators.java index 9a45a2e8d..8427199b6 100644 --- a/guava/src/main/java/com/github/cowwoc/requirements/guava/GuavaValidators.java +++ b/guava/src/main/java/com/github/cowwoc/requirements/guava/GuavaValidators.java @@ -4,6 +4,7 @@ */ package com.github.cowwoc.requirements.guava; +import com.github.cowwoc.requirements.annotation.CheckReturnValue; import com.github.cowwoc.requirements.guava.internal.implementation.GuavaValidatorsImpl; import com.github.cowwoc.requirements.java.Configuration; import com.github.cowwoc.requirements.java.ConfigurationUpdater; @@ -27,13 +28,14 @@ * {@code checkIf()} is more flexible than the others, but its syntax is more verbose. *

* Exceptions that are thrown in response to invalid method arguments (e.g. - * {@code isGreaterThan(null, value)} are thrown by all validators and cannot be configured. Exceptions that + * {@code isGreaterThan(null, value)}) are thrown by all validators and cannot be configured. Exceptions that * are thrown in response to the value failing a validation check, e.g. {@code isGreaterThan(5)} on a value * of 0, are thrown by {@code requireThat()} and {@code assumeThat()} but are recorded by {@code checkIf()} * without being thrown. The type of thrown exceptions is configurable using * {@link ConfigurationUpdater#exceptionTransformer(Function)}. */ -public interface GuavaValidators extends Validators, GuavaRequireThat, GuavaAssumeThat, GuavaCheckIf +public interface GuavaValidators + extends Validators, GuavaRequireThat, GuavaAssumeThat, GuavaCheckIf { /** * Creates a new instance using the default configuration. @@ -57,4 +59,20 @@ static GuavaValidators newInstance(Configuration configuration) { return new GuavaValidatorsImpl(MainApplicationScope.INSTANCE, configuration); } + + /** + * Returns a new factory instance with an independent configuration. This method is commonly used to inherit + * and update contextual information from the original factory before passing it into a nested operation. + * For example: + *

+ * {@snippet : + * GuavaValidators copy = validators.copy(); + * copy.context().put(json.toString(), "json"); + * nestedOperation(copy); + *} + * + * @return a copy of this factory + */ + @CheckReturnValue + GuavaValidators copy(); } \ No newline at end of file diff --git a/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/GuavaValidatorsImpl.java b/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/GuavaValidatorsImpl.java index d364d4934..027d3a0ab 100644 --- a/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/GuavaValidatorsImpl.java +++ b/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/GuavaValidatorsImpl.java @@ -3,12 +3,18 @@ import com.github.cowwoc.requirements.guava.GuavaValidators; import com.github.cowwoc.requirements.guava.MultimapValidator; import com.github.cowwoc.requirements.java.Configuration; +import com.github.cowwoc.requirements.java.ValidationFailure; import com.github.cowwoc.requirements.java.internal.implementation.AbstractValidator; import com.github.cowwoc.requirements.java.internal.implementation.AbstractValidators; import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.google.common.collect.Multimap; -public class GuavaValidatorsImpl extends AbstractValidators +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class GuavaValidatorsImpl extends AbstractValidators implements GuavaValidators { /** @@ -23,10 +29,22 @@ public GuavaValidatorsImpl(ApplicationScope scope, Configuration configuration) super(scope, configuration); } + /** + * Creates a copy of an existing validator factory. + * + * @param other the factory to copy + * @throws NullPointerException if {@code other} is null + */ + public GuavaValidatorsImpl(GuavaValidatorsImpl other) + { + this(other.scope, other.configuration()); + this.context.putAll(other.context); + } + @Override public > MultimapValidator requireThat(T value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override @@ -56,6 +74,39 @@ public > MultimapValidator checkIf(T val private > MultimapValidator newInstance(T value, String name, Configuration configuration) { - return new MultimapValidatorImpl<>(scope, configuration, name, value); + return new MultimapValidatorImpl<>(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); + } + + private Map newValidatorContext() + { + HashMap context = HashMap.newHashMap(this.context.size() + 2); + context.putAll(this.context); + return context; + } + + private List newValidatorFailures() + { + return new ArrayList<>(2); + } + + @Override + public GuavaValidatorsImpl copy() + { + return new GuavaValidatorsImpl(this); + } + + @Override + public GuavaValidatorsImpl putContext(Object value, String name) + { + context.put(name, value); + return this; + } + + @Override + public GuavaValidatorsImpl removeContext(String name) + { + context.remove(name); + return this; } } \ No newline at end of file diff --git a/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/MultimapValidatorImpl.java b/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/MultimapValidatorImpl.java index b5f15aa73..425a37eae 100644 --- a/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/MultimapValidatorImpl.java +++ b/guava/src/main/java/com/github/cowwoc/requirements/guava/internal/implementation/MultimapValidatorImpl.java @@ -8,7 +8,6 @@ import com.github.cowwoc.requirements.java.Configuration; import com.github.cowwoc.requirements.java.ValidationFailure; import com.github.cowwoc.requirements.java.internal.implementation.AbstractObjectValidator; -import com.github.cowwoc.requirements.java.internal.implementation.AbstractValidator; import com.github.cowwoc.requirements.java.internal.implementation.CollectionValidatorImpl; import com.github.cowwoc.requirements.java.internal.implementation.PrimitiveUnsignedIntegerValidatorImpl; import com.github.cowwoc.requirements.java.internal.implementation.message.CollectionMessages; @@ -19,9 +18,7 @@ import com.github.cowwoc.requirements.java.type.PrimitiveUnsignedIntegerValidator; import com.google.common.collect.Multimap; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -35,41 +32,6 @@ public final class MultimapValidatorImpl> extends AbstractObjectValidator, T> implements MultimapValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public MultimapValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, T value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public MultimapValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value) - { - this(scope, configuration, name, value, new HashMap<>(), new ArrayList<>()); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -77,13 +39,13 @@ public MultimapValidatorImpl(ApplicationScope scope, Configuration configuration * @param value (optional) the value * @param context the contextual information set by the user * @param failures the list of validation failures - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private MultimapValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value, Map context, List failures) + public MultimapValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } @@ -145,18 +107,18 @@ public PrimitiveUnsignedIntegerValidator size() { if (hasFailed()) { - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".size()", 0, - null, Pluralizer.ENTRY); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", 0, + name, null, Pluralizer.ENTRY, context, failures); } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".size()", 0, - null, Pluralizer.ENTRY); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", 0, + name, null, Pluralizer.ENTRY, context, failures); } PrimitiveUnsignedIntegerValidatorImpl newValidator = new PrimitiveUnsignedIntegerValidatorImpl(scope, - this, name + ".size()", value.size(), value, Pluralizer.ENTRY); + configuration, name + ".size()", value.size(), name, value, Pluralizer.ENTRY, context, failures); newValidator.putContext(value, name); return newValidator; } @@ -165,18 +127,24 @@ public PrimitiveUnsignedIntegerValidator size() public CollectionValidator> keySet() { if (hasFailed() || value == null) - return new CollectionValidatorImpl<>(scope, this, name + ".keySet()", null, Pluralizer.KEY); + { + return new CollectionValidatorImpl<>(scope, configuration, + name + ".keySet()", null, Pluralizer.KEY, context, failures); + } return new CollectionValidatorImpl<>(scope, configuration, name + ".keySet()", value.keySet(), - Pluralizer.KEY); + Pluralizer.KEY, context, failures); } @Override public CollectionValidator> values() { if (hasFailed() || value == null) - return new CollectionValidatorImpl<>(scope, configuration, name + ".values()", null, Pluralizer.VALUE); + { + return new CollectionValidatorImpl<>(scope, configuration, + name + ".values()", null, Pluralizer.VALUE, context, failures); + } return new CollectionValidatorImpl<>(scope, configuration, name + ".values()", value.values(), - Pluralizer.VALUE); + Pluralizer.VALUE, context, failures); } @Override @@ -185,9 +153,9 @@ public CollectionValidator, Collection>> entries() if (hasFailed() || value == null) { return new CollectionValidatorImpl<>(scope, configuration, name + ".entries()", null, - Pluralizer.ENTRY); + Pluralizer.ENTRY, context, failures); } return new CollectionValidatorImpl<>(scope, configuration, name + ".entries()", value.entries(), - Pluralizer.ENTRY); + Pluralizer.ENTRY, context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/DefaultJavaValidators.java b/java/src/main/java/com/github/cowwoc/requirements/java/DefaultJavaValidators.java index 53bcd481c..e87cf7f5d 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/DefaultJavaValidators.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/DefaultJavaValidators.java @@ -7,6 +7,8 @@ import com.github.cowwoc.requirements.annotation.CheckReturnValue; import com.github.cowwoc.requirements.java.internal.implementation.JavaValidatorsImpl; import com.github.cowwoc.requirements.java.internal.scope.MainApplicationScope; +import com.github.cowwoc.requirements.java.internal.util.CloseableLock; +import com.github.cowwoc.requirements.java.internal.util.ReentrantStampedLock; import com.github.cowwoc.requirements.java.type.BigDecimalValidator; import com.github.cowwoc.requirements.java.type.BigIntegerValidator; import com.github.cowwoc.requirements.java.type.BooleanValidator; @@ -75,11 +77,13 @@ * {@code checkIf()} is more flexible than the others, but its syntax is more verbose. *

* Exceptions that are thrown in response to invalid method arguments (e.g. - * {@code isGreaterThan(null, value)} are thrown by all validators and cannot be configured. Exceptions that + * {@code isGreaterThan(null, value)}) are thrown by all validators and cannot be configured. Exceptions that * are thrown in response to the value failing a validation check, e.g. {@code isGreaterThan(5)} on a value * of 0, are thrown by {@code requireThat()} and {@code assumeThat()} but are recorded by {@code checkIf()} * without being thrown. The type of thrown exceptions is configurable using * {@link ConfigurationUpdater#exceptionTransformer(Function)}. + *

+ * Thread Safety: This class is thread-safe. * * @see JavaValidators#newInstance() Creating an independent configuration */ @@ -87,6 +91,7 @@ public final class DefaultJavaValidators { private static final JavaValidatorsImpl delegate = new JavaValidatorsImpl(MainApplicationScope.INSTANCE, Configuration.DEFAULT); + private static final ReentrantStampedLock contextLock = new ReentrantStampedLock(); private DefaultJavaValidators() { @@ -2748,26 +2753,56 @@ public static ConfigurationUpdater updateConfiguration() } /** - * Returns the contextual information for validations performed by this thread using any validator. The - * contextual information is a map of key-value pairs that can provide more details about validation - * failures. For example, if the message is "Password may not be empty" and the map contains the key-value - * pair {@code {"username": "john.smith"}}, the exception message would be: + * Returns the contextual information for validators created out by this factory. The contextual information + * is a map of key-value pairs that can provide more details about validation failures. For example, if the + * message is "Password may not be empty" and the map contains the key-value pair + * {@code {"username": "john.smith"}}, the exception message would be: *

* {@snippet lang = output: * Password may not be empty * username: john.smith} + * + * @return an unmodifiable map from each entry's name to its value + */ + public static Map getContext() + { + return contextLock.optimisticRead(delegate::getContext); + } + + /** + * Sets the contextual information for validators created by this factory. *

- * Values set by this method may be overridden by {@link Validator#putContext(Object, String)}}. - *

- * NOTE: This method affects existing and new validators used by current thread. Changes are - * reversed once {@link ScopedContext#close()} is invoked. + * This method adds contextual information to exception messages. The contextual information is stored as + * key-value pairs in a map. Values set by this method may be overridden by + * {@link Validator#putContext(Object, String)}}. * - * @return the thread context updater + * @param value the value of the entry + * @param name the name of an entry + * @return the underlying validator factory + * @throws NullPointerException if {@code name} is null */ - @CheckReturnValue - public static ScopedContext threadContext() + public static JavaValidators putContext(Object value, String name) + { + try (CloseableLock unused = contextLock.write()) + { + return delegate.putContext(value, name); + } + } + + /** + * Removes the contextual information of validators created by this factory. + * + * @param name the parameter name + * @return the underlying validator factory + * @throws NullPointerException if {@code name} is null + * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty + */ + public static JavaValidators removeContext(String name) { - return delegate.threadContext(); + try (CloseableLock unused = contextLock.write()) + { + return delegate.removeContext(name); + } } /** diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/JavaValidators.java b/java/src/main/java/com/github/cowwoc/requirements/java/JavaValidators.java index 2acf9ace9..7b621d032 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/JavaValidators.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/JavaValidators.java @@ -4,6 +4,7 @@ */ package com.github.cowwoc.requirements.java; +import com.github.cowwoc.requirements.annotation.CheckReturnValue; import com.github.cowwoc.requirements.java.internal.implementation.JavaValidatorsImpl; import com.github.cowwoc.requirements.java.internal.scope.MainApplicationScope; @@ -24,13 +25,14 @@ * {@code checkIf()} is more flexible than the others, but its syntax is more verbose. *

* Exceptions that are thrown in response to invalid method arguments (e.g. - * {@code isGreaterThan(null, value)} are thrown by all validators and cannot be configured. Exceptions that + * {@code isGreaterThan(null, value)}) are thrown by all validators and cannot be configured. Exceptions that * are thrown in response to the value failing a validation check, e.g. {@code isGreaterThan(5)} on a value * of 0, are thrown by {@code requireThat()} and {@code assumeThat()} but are recorded by {@code checkIf()} * without being thrown. The type of thrown exceptions is configurable using * {@link ConfigurationUpdater#exceptionTransformer(Function)}. */ -public interface JavaValidators extends Validators, JavaRequireThat, JavaAssumeThat, JavaCheckIf +public interface JavaValidators + extends Validators, JavaRequireThat, JavaAssumeThat, JavaCheckIf { /** * Creates a new instance using the default configuration. @@ -54,4 +56,20 @@ static JavaValidators newInstance(Configuration configuration) { return new JavaValidatorsImpl(MainApplicationScope.INSTANCE, configuration); } + + /** + * Returns a new factory instance with an independent configuration. This method is commonly used to inherit + * and update contextual information from the original factory before passing it into a nested operation. + * For example: + *

+ * {@snippet : + * JavaValidators copy = validators.copy(); + * copy.context().put(json.toString(), "json"); + * nestedOperation(copy); + *} + * + * @return a copy of this factory + */ + @CheckReturnValue + JavaValidators copy(); } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/ScopedContext.java b/java/src/main/java/com/github/cowwoc/requirements/java/ScopedContext.java deleted file mode 100644 index c92ef7dc2..000000000 --- a/java/src/main/java/com/github/cowwoc/requirements/java/ScopedContext.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.github.cowwoc.requirements.java; - -/** - * Contextual information that is accessible only within the scope's duration, from creation to - * {@link #close()}. - */ -public interface ScopedContext extends AutoCloseable -{ - /** - * Looks up contextual information by its name. - *

- * To check if a parameter exists or has a {@code null} value, use {@link #containsName(String)}. - * - * @param name the parameter name - * @return the value of the parameter or {@code null} if no match was found - */ - Object get(String name); - - /** - * Returns {@code true} if a parameter exists. - * - * @param name the parameter name - * @return {@code true} if the parameter exists - */ - boolean containsName(String name); - - /** - * Adds or updates contextual information. - * - * @param value the parameter value - * @param name the parameter name - * @return this - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - */ - ScopedContext put(Object value, String name); - - /** - * Removes contextual information. - * - * @param name the parameter name - * @return this - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - */ - ScopedContext remove(String name); - - /** - * Reverses any changes applied to the context. - */ - @Override - void close(); -} \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/Validators.java b/java/src/main/java/com/github/cowwoc/requirements/java/Validators.java index fce915bca..c4f85b849 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/Validators.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/Validators.java @@ -3,6 +3,7 @@ import com.github.cowwoc.requirements.annotation.CheckReturnValue; import com.github.cowwoc.requirements.java.type.part.Validator; +import java.util.Map; import java.util.function.Function; /** @@ -20,13 +21,15 @@ * {@code checkIf()} is more flexible than the others, but its syntax is more verbose. *

* Exceptions that are thrown in response to invalid method arguments (e.g. - * {@code isGreaterThan(null, value)} are thrown by all validators and cannot be configured. Exceptions that + * {@code isGreaterThan(null, value)}) are thrown by all validators and cannot be configured. Exceptions that * are thrown in response to the value failing a validation check, e.g. {@code isGreaterThan(5)} on a value * of 0, are thrown by {@code requireThat()} and {@code assumeThat()} but are recorded by {@code checkIf()} * without being thrown. The type of thrown exceptions is configurable using * {@link ConfigurationUpdater#exceptionTransformer(Function)}. + * + * @param the type of the factory */ -public interface Validators +public interface Validators { /** * Returns the configuration used by new validators. @@ -47,24 +50,58 @@ public interface Validators ConfigurationUpdater updateConfiguration(); /** - * Returns the contextual information for validations performed by this thread using any validator. The - * contextual information is a map of key-value pairs that can provide more details about validation - * failures. For example, if the message is "Password may not be empty" and the map contains the key-value - * pair {@code {"username": "john.smith"}}, the exception message would be: + * Returns a new factory instance with an independent configuration. This method is commonly used to inherit + * and update contextual information from the original factory before passing it into a nested operation. + * For example: + *

+ * {@snippet : + * JavaValidators copy = validators.copy(); + * copy.context().put(json.toString(), "json"); + * nestedOperation(copy); + *} + * + * @return a copy of this factory + */ + @CheckReturnValue + S copy(); + + /** + * Returns the contextual information inherited by validators created out by this factory. The contextual + * information is a map of key-value pairs that can provide more details about validation failures. For + * example, if the message is "Password may not be empty" and the map contains the key-value pair + * {@code {"username": "john.smith"}}, the exception message would be: *

* {@snippet lang = output: * Password may not be empty * username: john.smith} + * + * @return an unmodifiable map from each entry's name to its value + */ + Map getContext(); + + /** + * Sets the contextual information for validators created by this factory. *

- * Values set by this method may be overridden by {@link Validator#putContext(Object, String)}}. - *

- * NOTE: This method affects existing and new validators used by current thread. Changes are - * reversed once {@link ScopedContext#close()} is invoked. + * This method adds contextual information to exception messages. The contextual information is stored as + * key-value pairs in a map. Values set by this method may be overridden by + * {@link Validator#putContext(Object, String)}}. * - * @return the thread context updater + * @param value the value of the entry + * @param name the name of an entry + * @return this + * @throws NullPointerException if {@code name} is null */ - @CheckReturnValue - ScopedContext threadContext(); + S putContext(Object value, String name); + + /** + * Removes the contextual information of validators created by this factory. + * + * @param name the parameter name + * @return this + * @throws NullPointerException if {@code name} is null + * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty + */ + S removeContext(String name); /** * Returns the global configuration shared by all validators. diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractArrayValidator.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractArrayValidator.java index 1376efa82..c61c78353 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractArrayValidator.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractArrayValidator.java @@ -572,18 +572,18 @@ public PrimitiveUnsignedIntegerValidatorImpl length() { if (hasFailed()) { - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".length", 0, - value, PLURALIZER); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".length", 0, + name, value, PLURALIZER, context, failures); } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".length", 0, - value, PLURALIZER); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".length", 0, + name, value, PLURALIZER, context, failures); } PrimitiveUnsignedIntegerValidatorImpl newValidator = new PrimitiveUnsignedIntegerValidatorImpl(scope, - this, name + ".length", getLength(value), value, PLURALIZER); + configuration, name + ".length", getLength(value), name, value, PLURALIZER, context, failures); newValidator.putContext(value, name); return newValidator; } @@ -592,15 +592,23 @@ public PrimitiveUnsignedIntegerValidatorImpl length() public CollectionValidatorImpl> asCollection() { if (!failures.isEmpty()) - return new CollectionValidatorImpl<>(scope, this, name, null, Pluralizer.ELEMENT); - return new CollectionValidatorImpl<>(scope, this, name, asList(value), Pluralizer.ELEMENT); + { + return new CollectionValidatorImpl<>(scope, configuration, name, null, Pluralizer.ELEMENT, context, + failures); + } + return new CollectionValidatorImpl<>(scope, configuration, name, asList(value), Pluralizer.ELEMENT, + context, failures); } @Override public ListValidatorImpl> asList() { if (!failures.isEmpty()) - return new ListValidatorImpl<>(scope, this, name, null, Pluralizer.ELEMENT); - return new ListValidatorImpl<>(scope, this, name, asList(value), Pluralizer.ELEMENT); + { + return new ListValidatorImpl<>(scope, configuration, name, null, Pluralizer.ELEMENT, context, + failures); + } + return new ListValidatorImpl<>(scope, configuration, name, asList(value), Pluralizer.ELEMENT, context, + failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractCollectionValidator.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractCollectionValidator.java index 072607db9..c2a7c7e0e 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractCollectionValidator.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractCollectionValidator.java @@ -47,7 +47,7 @@ public abstract class AbstractCollectionValidator * @param pluralizer the type of items in the collection * @param context the contextual information set by the user * @param failures the list of validation failures - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. @@ -613,47 +613,56 @@ public PrimitiveUnsignedIntegerValidatorImpl size() { if (hasFailed()) { - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".size()", 0, - null, pluralizer); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", 0, + name, null, pluralizer, context, failures); } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".size()", 0, - null, pluralizer); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", 0, + name, null, pluralizer, context, failures); } - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".size()", value.size(), - value, pluralizer); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", value.size(), + name, value, pluralizer, context, failures); } @Override public ObjectArrayValidator asArray(Class type) { if (hasFailed()) - return new ObjectArrayValidatorImpl<>(scope, this, name + ".asArray()", null); + { + return new ObjectArrayValidatorImpl<>(scope, configuration, name + ".asArray()", null, context, + failures); + } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new ObjectArrayValidatorImpl<>(scope, this, name + ".asArray()", null); + return new ObjectArrayValidatorImpl<>(scope, configuration, name + ".asArray()", null, context, + failures); } @SuppressWarnings("unchecked") E[] array = (E[]) Array.newInstance(type, value.size()); - return new ObjectArrayValidatorImpl<>(scope, this, name, value.toArray(array)); + return new ObjectArrayValidatorImpl<>(scope, configuration, name, value.toArray(array), context, failures); } @Override public ListValidator> asList() { if (hasFailed()) - return new ListValidatorImpl<>(scope, this, name + ".asList()", null, pluralizer); + { + return new ListValidatorImpl<>(scope, configuration, name + ".asList()", null, pluralizer, context, + failures); + } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new ListValidatorImpl<>(scope, this, name + ".asList()", null, pluralizer); + return new ListValidatorImpl<>(scope, configuration, name + ".asList()", null, pluralizer, context, + failures); } - return new ListValidatorImpl<>(scope, this, name, new ArrayList<>(value), pluralizer); + return new ListValidatorImpl<>(scope, configuration, name, new ArrayList<>(value), pluralizer, context, + failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractObjectValidator.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractObjectValidator.java index c0a72238b..360ab70b4 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractObjectValidator.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractObjectValidator.java @@ -201,6 +201,7 @@ private BiFunction getEqualityFunction() @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidator.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidator.java index 20e22bfba..fe3fd1cdc 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidator.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidator.java @@ -49,7 +49,7 @@ public abstract class AbstractValidator implements Validator * @param name the name of the value * @param context the contextual information set by the user * @param failures the list of validation failures - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidators.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidators.java index 83a50653c..e706cbb7c 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidators.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/AbstractValidators.java @@ -1,30 +1,38 @@ package com.github.cowwoc.requirements.java.internal.implementation; +import com.github.cowwoc.requirements.annotation.CheckReturnValue; import com.github.cowwoc.requirements.java.Configuration; import com.github.cowwoc.requirements.java.ConfigurationUpdater; import com.github.cowwoc.requirements.java.EqualityMethod; import com.github.cowwoc.requirements.java.GlobalConfiguration; import com.github.cowwoc.requirements.java.MutableStringMappers; -import com.github.cowwoc.requirements.java.ScopedContext; import com.github.cowwoc.requirements.java.StringMappers; import com.github.cowwoc.requirements.java.Validators; import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.internal.util.CloseableLock; import com.github.cowwoc.requirements.java.internal.util.ReentrantStampedLock; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; import java.util.function.Function; +import static com.github.cowwoc.requirements.java.DefaultJavaValidators.assumeThat; + /** - * @param the type of validator that the methods should return + * @param the type of the validator factory */ -public abstract class AbstractValidators implements Validators +public abstract class AbstractValidators implements Validators { protected final ApplicationScope scope; + private final ReentrantStampedLock requireThatLock = new ReentrantStampedLock(); private final ReentrantStampedLock assumeThatLock = new ReentrantStampedLock(); private final ReentrantStampedLock checkIfLock = new ReentrantStampedLock(); - private Configuration configuration; + private Configuration requireThatConfiguration; private Configuration assumeThatConfiguration; private Configuration checkIfConfiguration; + protected final Map context = new HashMap<>(); /** * Creates a new instance. @@ -40,7 +48,15 @@ protected AbstractValidators(ApplicationScope scope, Configuration configuration if (configuration == null) throw new NullPointerException("configuration may not be null"); this.scope = scope; - this.configuration = configuration; + setConfiguration(configuration); + } + + /** + * @return the application configuration + */ + public ApplicationScope getScope() + { + return scope; } /** @@ -72,88 +88,91 @@ protected S self() @Override public Configuration configuration() { - return configuration; + return requireThatLock.optimisticRead(() -> this.requireThatConfiguration); } - @Override - public ConfigurationUpdater updateConfiguration() + /** + * Returns the configuration for {@code assumeThat()} factory methods. + * + * @return the configuration for {@code assumeThat()} factory methods + */ + protected Configuration getAssumeThatConfiguration() { - return new UpdatableConfigurationImpl(); + return assumeThatLock.optimisticRead(() -> this.assumeThatConfiguration); } /** - * Set the configuration used by new validators. + * Returns the configuration for {@code checkIf()} factory methods. * - * @param configuration the updated configuration - * @throws NullPointerException if {@code configuration} is null + * @return the configuration for {@code checkIf()} factory methods */ - public void setConfiguration(Configuration configuration) + protected Configuration getCheckIfConfiguration() { - if (configuration == null) - throw new NullPointerException("configuration may not be null"); - this.configuration = configuration; - try (CloseableLock unused = assumeThatLock.write()) - { - assumeThatConfiguration = null; - } - try (CloseableLock unused = checkIfLock.write()) - { - checkIfConfiguration = null; - } + return checkIfLock.optimisticRead(() -> this.checkIfConfiguration); + } + + @Override + public ConfigurationUpdater updateConfiguration() + { + return updateConfiguration(this::setConfiguration); } /** - * Returns the configuration for {@code requireThat()} factory methods. + * Updates the configuration used by new validators. + *

+ * NOTE: Changes are only applied when {@link ConfigurationUpdater#close()} is invoked. * - * @return the configuration for {@code requireThat()} factory methods + * @param setConfiguration a method that sets the validator factory's configuration + * @return the configuration updater + * @throws NullPointerException if {@code setConfiguration} is null */ - protected Configuration getRequireThatConfiguration() + @CheckReturnValue + public ConfigurationUpdater updateConfiguration(Consumer setConfiguration) { - return configuration(); + return new UpdatableConfigurationImpl(setConfiguration); } /** - * Returns the configuration for {@code assumeThat()} factory methods. + * Set the configuration used by new validators. * - * @return the configuration for {@code assumeThat()} factory methods + * @param configuration the updated configuration + * @throws NullPointerException if {@code configuration} is null */ - protected Configuration getAssumeThatConfiguration() + public final void setConfiguration(Configuration configuration) { - Configuration assumeThatConfiguration = assumeThatLock.optimisticRead(() -> - this.assumeThatConfiguration); - if (assumeThatConfiguration != null) - return assumeThatConfiguration; + if (configuration == null) + throw new NullPointerException("configuration may not be null"); + try (CloseableLock unused = requireThatLock.write()) + { + this.requireThatConfiguration = configuration; + } try (CloseableLock unused = assumeThatLock.write()) { - this.assumeThatConfiguration = assumeThatConfiguration = MutableConfiguration.from(configuration()). + this.assumeThatConfiguration = MutableConfiguration.from(configuration). exceptionTransformer(convertToAssertionError()).toImmutable(); } - return assumeThatConfiguration; - } - - /** - * Returns the configuration for {@code checkIf()} factory methods. - * - * @return the configuration for {@code checkIf()} factory methods - */ - protected Configuration getCheckIfConfiguration() - { - Configuration checkIfConfiguration = checkIfLock.optimisticRead(() -> - this.checkIfConfiguration); - if (checkIfConfiguration != null) - return checkIfConfiguration; try (CloseableLock unused = checkIfLock.write()) { - this.checkIfConfiguration = checkIfConfiguration = MutableConfiguration.from(configuration()). + this.checkIfConfiguration = MutableConfiguration.from(configuration). throwOnFailure(false).toImmutable(); } - return checkIfConfiguration; } - @Override - public ScopedContext threadContext() + /** + * Returns the contextual information for validators created out by this factory. The contextual information + * is a map of key-value pairs that can provide more details about validation failures. For example, if the + * message is "Password may not be empty" and the map contains the key-value pair + * {@code {"username": "john.smith"}}, the exception message would be: + *

+ * {@snippet lang = output: + * Password may not be empty + * username: john.smith} + * + * @return an unmodifiable map from each entry's name to its value + */ + public Map getContext() { - return new ScopedContextImpl(scope); + return Collections.unmodifiableMap(context); } @Override @@ -169,6 +188,7 @@ public final class UpdatableConfigurationImpl implements ConfigurationUpdater { // REMINDER: Per https://shipilev.net/blog/2014/safe-public-construction/ section // "A final field was written" objects are safe for publication if they contain at least one final field. + private final Consumer setConfiguration; private final MutableStringMappers mutableStringMappers; private boolean cleanStackTrace; private boolean includeDiff; @@ -180,9 +200,15 @@ public final class UpdatableConfigurationImpl implements ConfigurationUpdater /** * Creates a new configuration updater. + * + * @param setConfiguration a method that sets the validator factory's configuration + * @throws NullPointerException if {@code setConfiguration} is null */ - private UpdatableConfigurationImpl() + private UpdatableConfigurationImpl(Consumer setConfiguration) { + assert assumeThat(setConfiguration, "setConfiguration").isNotNull().elseThrow(); + this.setConfiguration = setConfiguration; + Configuration configuration = AbstractValidators.this.configuration(); this.cleanStackTrace = configuration.cleanStackTrace(); this.includeDiff = configuration.includeDiff(); @@ -353,7 +379,7 @@ public void close() changed |= !immutableStringMappers.equals(oldConfiguration.stringMappers()); if (!changed) return; - setConfiguration(new Configuration(cleanStackTrace, includeDiff, + this.setConfiguration.accept(new Configuration(cleanStackTrace, includeDiff, equalityMethod, immutableStringMappers, lazyExceptions, oldConfiguration.throwOnFailure(), exceptionTransformer)); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigDecimalValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigDecimalValidatorImpl.java index 1ac9779bc..fe0e8807c 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigDecimalValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigDecimalValidatorImpl.java @@ -13,50 +13,12 @@ import com.github.cowwoc.requirements.java.type.BigDecimalValidator; import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class BigDecimalValidatorImpl extends AbstractObjectValidator implements BigDecimalValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public BigDecimalValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - BigDecimal value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public BigDecimalValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - BigDecimal value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -69,7 +31,7 @@ public BigDecimalValidatorImpl(ApplicationScope scope, Configuration configurati * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private BigDecimalValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + public BigDecimalValidatorImpl(ApplicationScope scope, Configuration configuration, String name, BigDecimal value, Map context, List failures) { super(scope, configuration, name, value, context, failures); @@ -531,28 +493,37 @@ private boolean maximumFailed(BigDecimal maximum, boolean maximumInclusive) public PrimitiveUnsignedIntegerValidatorImpl precision() { if (hasFailed()) - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".precision()", 0, value, null); + { + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".precision()", + 0, name, value, null, context, failures); + } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".precision()", 0, value, null); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".precision()", + 0, name, value, null, context, failures); } - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, - name + ".precision()", value.precision(), value, null); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".precision()", + value.precision(), name, value, null, context, failures); } @Override public PrimitiveIntegerValidatorImpl scale() { if (hasFailed()) - return new PrimitiveIntegerValidatorImpl(scope, this, name + ".scale()", 0); + { + return new PrimitiveIntegerValidatorImpl(scope, configuration, name + ".scale()", 0, context, + failures); + } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new PrimitiveIntegerValidatorImpl(scope, this, name + ".scale()", 0); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name + ".scale()", 0, context, + failures); } - return new PrimitiveIntegerValidatorImpl(scope, this, name + ".scale()", value.scale()); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name + ".scale()", value.scale(), + context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigIntegerValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigIntegerValidatorImpl.java index fc7412f96..28c4b1297 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigIntegerValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BigIntegerValidatorImpl.java @@ -13,50 +13,12 @@ import com.github.cowwoc.requirements.java.type.BigIntegerValidator; import java.math.BigInteger; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class BigIntegerValidatorImpl extends AbstractObjectValidator implements BigIntegerValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public BigIntegerValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - BigInteger value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public BigIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - BigInteger value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -69,7 +31,7 @@ public BigIntegerValidatorImpl(ApplicationScope scope, Configuration configurati * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private BigIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + public BigIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String name, BigInteger value, Map context, List failures) { super(scope, configuration, name, value, context, failures); diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BooleanValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BooleanValidatorImpl.java index 7a72863d9..d15f9c2be 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BooleanValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/BooleanValidatorImpl.java @@ -7,50 +7,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.BooleanValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class BooleanValidatorImpl extends AbstractObjectValidator implements BooleanValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public BooleanValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - Boolean value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public BooleanValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Boolean value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -63,7 +25,7 @@ public BooleanValidatorImpl(ApplicationScope scope, Configuration configuration, * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private BooleanValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + public BooleanValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Boolean value, Map context, List failures) { super(scope, configuration, name, value, context, failures); diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ByteValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ByteValidatorImpl.java index c45b7ad84..f3bef56fb 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ByteValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ByteValidatorImpl.java @@ -8,50 +8,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.ByteValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class ByteValidatorImpl extends AbstractObjectValidator implements ByteValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ByteValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - Byte value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ByteValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Byte value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -64,8 +26,8 @@ public ByteValidatorImpl(ApplicationScope scope, Configuration configuration, St * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private ByteValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Byte value, Map context, List failures) + public ByteValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Byte value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CharacterValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CharacterValidatorImpl.java index e7a3f64d3..10ead9a73 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CharacterValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CharacterValidatorImpl.java @@ -7,50 +7,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.CharacterValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class CharacterValidatorImpl extends AbstractObjectValidator implements CharacterValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public CharacterValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - Character value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public CharacterValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Character value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -63,7 +25,7 @@ public CharacterValidatorImpl(ApplicationScope scope, Configuration configuratio * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private CharacterValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + public CharacterValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Character value, Map context, List failures) { super(scope, configuration, name, value, context, failures); diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ClassValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ClassValidatorImpl.java index bb3e8e0b9..cbd7cc90e 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ClassValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ClassValidatorImpl.java @@ -10,8 +10,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.ClassValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -21,42 +19,6 @@ public final class ClassValidatorImpl extends AbstractObjectValidator, Class> implements ClassValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ClassValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - Class value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ClassValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Class value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -69,8 +31,8 @@ public ClassValidatorImpl(ApplicationScope scope, Configuration configuration, S * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private ClassValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Class value, Map context, List failures) + public ClassValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Class value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CollectionValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CollectionValidatorImpl.java index d34cd30a9..b100afff8 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CollectionValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/CollectionValidatorImpl.java @@ -10,9 +10,7 @@ import com.github.cowwoc.requirements.java.internal.util.Pluralizer; import com.github.cowwoc.requirements.java.type.CollectionValidator; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -24,44 +22,6 @@ public final class CollectionValidatorImpl> extends AbstractCollectionValidator, E, T> implements CollectionValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @param pluralizer the type of items in the collection - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public CollectionValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - T value, Pluralizer pluralizer) - { - this(scope, validator.configuration(), name, value, pluralizer, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @param pluralizer the type of items in the collection - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public CollectionValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value, Pluralizer pluralizer) - { - this(scope, configuration, name, value, pluralizer, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -75,8 +35,8 @@ public CollectionValidatorImpl(ApplicationScope scope, Configuration configurati * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private CollectionValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value, Pluralizer pluralizer, Map context, List failures) + public CollectionValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value, + Pluralizer pluralizer, Map context, List failures) { super(scope, configuration, name, value, pluralizer, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ComparableValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ComparableValidatorImpl.java index 472c24611..22737082c 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ComparableValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ComparableValidatorImpl.java @@ -11,8 +11,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.ComparableValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,42 +21,6 @@ public final class ComparableValidatorImpl> extends AbstractObjectValidator, T> implements ComparableValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ComparableValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - T value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ComparableValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -71,8 +33,8 @@ public ComparableValidatorImpl(ApplicationScope scope, Configuration configurati * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - public ComparableValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value, Map context, List failures) + public ComparableValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/DoubleValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/DoubleValidatorImpl.java index 76160eab2..bc9a24d43 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/DoubleValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/DoubleValidatorImpl.java @@ -12,50 +12,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.DoubleValidator; - import java.util.ArrayList; - import java.util.HashMap; import java.util.List; import java.util.Map; public final class DoubleValidatorImpl extends AbstractObjectValidator implements DoubleValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public DoubleValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - Double value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public DoubleValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Double value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,7 +30,7 @@ public DoubleValidatorImpl(ApplicationScope scope, Configuration configuration, * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private DoubleValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + public DoubleValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Double value, Map context, List failures) { super(scope, configuration, name, value, context, failures); diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/FloatValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/FloatValidatorImpl.java index b2f9a1c17..42bb77bad 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/FloatValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/FloatValidatorImpl.java @@ -12,50 +12,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.FloatValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class FloatValidatorImpl extends AbstractObjectValidator implements FloatValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public FloatValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - Float value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public FloatValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Float value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public FloatValidatorImpl(ApplicationScope scope, Configuration configuration, S * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private FloatValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Float value, Map context, List failures) + public FloatValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Float value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/InetAddressValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/InetAddressValidatorImpl.java index 187b0dbe8..ba357391c 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/InetAddressValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/InetAddressValidatorImpl.java @@ -14,50 +14,12 @@ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class InetAddressValidatorImpl extends AbstractObjectValidator implements InetAddressValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public InetAddressValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - InetAddress value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public InetAddressValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - InetAddress value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -70,7 +32,7 @@ public InetAddressValidatorImpl(ApplicationScope scope, Configuration configurat * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private InetAddressValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + public InetAddressValidatorImpl(ApplicationScope scope, Configuration configuration, String name, InetAddress value, Map context, List failures) { super(scope, configuration, name, value, context, failures); @@ -129,6 +91,6 @@ public StringValidatorImpl asString() // InetAddress.getByName(String). Instead, we use InetAddress.getHostName() which returns the desired // format. String hostName = value.getHostName(); - return new StringValidatorImpl(scope, this, name, hostName); + return new StringValidatorImpl(scope, configuration, name, hostName, context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/IntegerValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/IntegerValidatorImpl.java index 1e918b0c9..e3324c03f 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/IntegerValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/IntegerValidatorImpl.java @@ -8,50 +8,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.IntegerValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class IntegerValidatorImpl extends AbstractObjectValidator implements IntegerValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public IntegerValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - Integer value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public IntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Integer value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -64,8 +26,8 @@ public IntegerValidatorImpl(ApplicationScope scope, Configuration configuration, * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private IntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Integer value, Map context, List failures) + public IntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Integer value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/JavaValidatorsImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/JavaValidatorsImpl.java index c1dc2d80e..1138ee7c8 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/JavaValidatorsImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/JavaValidatorsImpl.java @@ -2,6 +2,7 @@ import com.github.cowwoc.requirements.java.Configuration; import com.github.cowwoc.requirements.java.JavaValidators; +import com.github.cowwoc.requirements.java.ValidationFailure; import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.internal.util.Pluralizer; import com.github.cowwoc.requirements.java.type.BigDecimalValidator; @@ -50,12 +51,14 @@ import java.net.URI; import java.net.URL; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -public final class JavaValidatorsImpl extends AbstractValidators +public final class JavaValidatorsImpl extends AbstractValidators implements JavaValidators { /** @@ -70,238 +73,250 @@ public JavaValidatorsImpl(ApplicationScope scope, Configuration configuration) super(scope, configuration); } + /** + * Creates a copy of an existing validator factory. + * + * @param other the factory to copy + * @throws NullPointerException if {@code other} is null + */ + public JavaValidatorsImpl(JavaValidatorsImpl other) + { + this(other.scope, other.configuration()); + this.context.putAll(other.context); + } + @Override public PrimitiveByteValidator requireThat(byte value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public ByteValidator requireThat(Byte value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveShortValidator requireThat(short value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public ShortValidator requireThat(Short value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveIntegerValidator requireThat(int value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public IntegerValidator requireThat(Integer value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveLongValidator requireThat(long value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public LongValidator requireThat(Long value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveFloatValidator requireThat(float value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public FloatValidator requireThat(Float value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveDoubleValidator requireThat(double value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public DoubleValidator requireThat(Double value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveBooleanValidator requireThat(boolean value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public BooleanValidator requireThat(Boolean value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveCharacterValidator requireThat(char value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public CharacterValidator requireThat(Character value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public BigIntegerValidator requireThat(BigInteger value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public BigDecimalValidator requireThat(BigDecimal value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public > ComparableValidator requireThat(T value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public ObjectValidator requireThat(T value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public > CollectionValidator requireThat(T value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public > ListValidator requireThat(T value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveByteArrayValidator requireThat(byte[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveShortArrayValidator requireThat(short[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveIntegerArrayValidator requireThat(int[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveLongArrayValidator requireThat(long[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveFloatArrayValidator requireThat(float[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveDoubleArrayValidator requireThat(double[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveBooleanArrayValidator requireThat(boolean[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PrimitiveCharacterArrayValidator requireThat(char[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public ObjectArrayValidator requireThat(E[] value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public > MapValidator requireThat(T value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public PathValidator requireThat(Path value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public StringValidator requireThat(String value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public UriValidator requireThat(URI value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public UrlValidator requireThat(URL value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public ClassValidator requireThat(Class value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public OptionalValidator requireThat(Optional value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override public InetAddressValidator requireThat(InetAddress value, String name) { - return newInstance(value, name, getRequireThatConfiguration()); + return newInstance(value, name, configuration()); } @Override @@ -1242,211 +1257,272 @@ public InetAddressValidator checkIf(InetAddress value) private PrimitiveByteValidator newInstance(byte value, String name, Configuration configuration) { - return new PrimitiveByteValidatorImpl(scope, configuration, name, value); + return new PrimitiveByteValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); + } + + private Map newValidatorContext() + { + HashMap context = HashMap.newHashMap(this.context.size() + 2); + context.putAll(this.context); + return context; + } + + private List newValidatorFailures() + { + return new ArrayList<>(2); } private ByteValidator newInstance(Byte value, String name, Configuration configuration) { - return new ByteValidatorImpl(scope, configuration, name, value); + return new ByteValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private PrimitiveShortValidator newInstance(short value, String name, Configuration configuration) { - return new PrimitiveShortValidatorImpl(scope, configuration, name, value); + return new PrimitiveShortValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private ShortValidator newInstance(Short value, String name, Configuration configuration) { - return new ShortValidatorImpl(scope, configuration, name, value); + return new ShortValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private PrimitiveIntegerValidator newInstance(int value, String name, Configuration configuration) { - return new PrimitiveIntegerValidatorImpl(scope, configuration, name, value); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private IntegerValidator newInstance(Integer value, String name, Configuration configuration) { - return new IntegerValidatorImpl(scope, configuration, name, value); + return new IntegerValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private PrimitiveLongValidator newInstance(long value, String name, Configuration configuration) { - return new PrimitiveLongValidatorImpl(scope, configuration, name, value); + return new PrimitiveLongValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private LongValidator newInstance(Long value, String name, Configuration configuration) { - return new LongValidatorImpl(scope, configuration, name, value); + return new LongValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private PrimitiveFloatValidator newInstance(float value, String name, Configuration configuration) { - return new PrimitiveFloatValidatorImpl(scope, configuration, name, value); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private FloatValidator newInstance(Float value, String name, Configuration configuration) { - return new FloatValidatorImpl(scope, configuration, name, value); + return new FloatValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public PrimitiveDoubleValidator newInstance(double value, String name, Configuration configuration) { - return new PrimitiveDoubleValidatorImpl(scope, configuration, name, value); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } private DoubleValidator newInstance(Double value, String name, Configuration configuration) { - return new DoubleValidatorImpl(scope, configuration, name, value); + return new DoubleValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveBooleanValidator newInstance(boolean value, String name, - Configuration configuration) + public PrimitiveBooleanValidator newInstance(boolean value, String name, Configuration configuration) { - return new PrimitiveBooleanValidatorImpl(scope, configuration, name, value); + return new PrimitiveBooleanValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public BooleanValidator newInstance(Boolean value, String name, Configuration configuration) { - return new BooleanValidatorImpl(scope, configuration, name, value); + return new BooleanValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public PrimitiveCharacterValidator newInstance(char value, String name, Configuration configuration) { - return new PrimitiveCharacterValidatorImpl(scope, configuration, name, value); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public CharacterValidator newInstance(Character value, String name, Configuration configuration) { - return new CharacterValidatorImpl(scope, configuration, name, value); + return new CharacterValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public BigIntegerValidator newInstance(BigInteger value, String name, Configuration configuration) { - return new BigIntegerValidatorImpl(scope, configuration, name, value); + return new BigIntegerValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public BigDecimalValidator newInstance(BigDecimal value, String name, Configuration configuration) { - return new BigDecimalValidatorImpl(scope, configuration, name, value); + return new BigDecimalValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public > ComparableValidator newInstance(T value, String name, Configuration configuration) { - return new ComparableValidatorImpl<>(scope, configuration, name, value); + return new ComparableValidatorImpl<>(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public ObjectValidator newInstance(T value, String name, Configuration configuration) { - return new ObjectValidatorImpl<>(scope, configuration, name, value); + return new ObjectValidatorImpl<>(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public > CollectionValidator newInstance(T value, String name, Configuration configuration) { - return new CollectionValidatorImpl<>(scope, configuration, name, value, Pluralizer.ELEMENT); + return new CollectionValidatorImpl<>(scope, configuration, name, value, Pluralizer.ELEMENT, + newValidatorContext(), newValidatorFailures()); } public > ListValidator newInstance(T value, String name, Configuration configuration) { - return new ListValidatorImpl<>(scope, configuration, name, value, Pluralizer.ELEMENT); + return new ListValidatorImpl<>(scope, configuration, name, value, Pluralizer.ELEMENT, + newValidatorContext(), newValidatorFailures()); } - public PrimitiveByteArrayValidator newInstance(byte[] value, String name, - Configuration configuration) + public PrimitiveByteArrayValidator newInstance(byte[] value, String name, Configuration configuration) { - return new PrimitiveByteArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveByteArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveShortArrayValidator newInstance(short[] value, String name, - Configuration configuration) + public PrimitiveShortArrayValidator newInstance(short[] value, String name, Configuration configuration) { - return new PrimitiveShortArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveShortArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveIntegerArrayValidator newInstance(int[] value, String name, - Configuration configuration) + public PrimitiveIntegerArrayValidator newInstance(int[] value, String name, Configuration configuration) { - return new PrimitiveIntegerArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveIntegerArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveLongArrayValidator newInstance(long[] value, String name, - Configuration configuration) + public PrimitiveLongArrayValidator newInstance(long[] value, String name, Configuration configuration) { - return new PrimitiveLongArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveLongArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveFloatArrayValidator newInstance(float[] value, String name, - Configuration configuration) + public PrimitiveFloatArrayValidator newInstance(float[] value, String name, Configuration configuration) { - return new PrimitiveFloatArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveFloatArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveDoubleArrayValidator newInstance(double[] value, String name, - Configuration configuration) + public PrimitiveDoubleArrayValidator newInstance(double[] value, String name, Configuration configuration) { - return new PrimitiveDoubleArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveDoubleArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveBooleanArrayValidator newInstance(boolean[] value, String name, - Configuration configuration) + public PrimitiveBooleanArrayValidator newInstance(boolean[] value, String name, Configuration configuration) { - return new PrimitiveBooleanArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveBooleanArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public PrimitiveCharacterArrayValidator newInstance(char[] value, String name, - Configuration configuration) + public PrimitiveCharacterArrayValidator newInstance(char[] value, String name, Configuration configuration) { - return new PrimitiveCharacterArrayValidatorImpl(scope, configuration, name, value); + return new PrimitiveCharacterArrayValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } - public ObjectArrayValidator newInstance(E[] value, String name, - Configuration configuration) + public ObjectArrayValidator newInstance(E[] value, String name, Configuration configuration) { - return new ObjectArrayValidatorImpl<>(scope, configuration, name, value); + return new ObjectArrayValidatorImpl<>(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public > MapValidator newInstance(T value, String name, Configuration configuration) { - return new MapValidatorImpl<>(scope, configuration, name, value); + return new MapValidatorImpl<>(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public PathValidator newInstance(Path value, String name, Configuration configuration) { - return new PathValidatorImpl(scope, configuration, name, value); + return new PathValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public StringValidator newInstance(String value, String name, Configuration configuration) { - return new StringValidatorImpl(scope, configuration, name, value); + return new StringValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public UriValidator newInstance(URI value, String name, Configuration configuration) { - return new UriValidatorImpl(scope, configuration, name, value); + return new UriValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public UrlValidator newInstance(URL value, String name, Configuration configuration) { - return new UrlValidatorImpl(scope, configuration, name, value); + return new UrlValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public ClassValidator newInstance(Class value, String name, Configuration configuration) { - return new ClassValidatorImpl<>(scope, configuration, name, value); + return new ClassValidatorImpl<>(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } @SuppressWarnings("OptionalUsedAsFieldOrParameterType") public OptionalValidator newInstance(Optional value, String name, Configuration configuration) { - return new OptionalValidatorImpl<>(scope, configuration, name, value); + return new OptionalValidatorImpl<>(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); } public InetAddressValidator newInstance(InetAddress value, String name, Configuration configuration) { - return new InetAddressValidatorImpl(scope, configuration, name, value); + return new InetAddressValidatorImpl(scope, configuration, name, value, newValidatorContext(), + newValidatorFailures()); + } + + @Override + public JavaValidatorsImpl copy() + { + return new JavaValidatorsImpl(this); + } + + @Override + public JavaValidatorsImpl putContext(Object value, String name) + { + context.put(name, value); + return this; + } + + @Override + public JavaValidatorsImpl removeContext(String name) + { + context.remove(name); + return this; } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ListValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ListValidatorImpl.java index bf33229bf..829cc1553 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ListValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ListValidatorImpl.java @@ -12,9 +12,7 @@ import com.github.cowwoc.requirements.java.internal.util.Pluralizer; import com.github.cowwoc.requirements.java.type.ListValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -26,44 +24,6 @@ public final class ListValidatorImpl> extends AbstractCollectionValidator, E, T> implements ListValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @param pluralizer (optional) the type of items in the collection - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ListValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - T value, Pluralizer pluralizer) - { - this(scope, validator.configuration(), name, value, pluralizer, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @param pluralizer (optional) the type of items in the collection - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ListValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value, Pluralizer pluralizer) - { - this(scope, configuration, name, value, pluralizer, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -77,8 +37,8 @@ public ListValidatorImpl(ApplicationScope scope, Configuration configuration, St * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private ListValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value, Pluralizer pluralizer, Map context, List failures) + public ListValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value, + Pluralizer pluralizer, Map context, List failures) { super(scope, configuration, name, value, pluralizer, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/LongValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/LongValidatorImpl.java index 0373aeaed..94fc76cbd 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/LongValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/LongValidatorImpl.java @@ -8,49 +8,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.LongValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class LongValidatorImpl extends AbstractObjectValidator implements LongValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public LongValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, Long value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public LongValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Long value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -63,8 +26,8 @@ public LongValidatorImpl(ApplicationScope scope, Configuration configuration, St * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private LongValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Long value, Map context, List failures) + public LongValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Long value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/MapValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/MapValidatorImpl.java index 2b04e981e..6d380592a 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/MapValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/MapValidatorImpl.java @@ -14,9 +14,7 @@ import com.github.cowwoc.requirements.java.type.MapValidator; import com.github.cowwoc.requirements.java.type.PrimitiveUnsignedIntegerValidator; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -31,40 +29,6 @@ public final class MapValidatorImpl> extends AbstractObjectValidator, T> implements MapValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public MapValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, T value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public MapValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -72,12 +36,12 @@ public MapValidatorImpl(ApplicationScope scope, Configuration configuration, Str * @param value (optional) the value * @param context the contextual information set by the user * @param failures the list of validation failures - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private MapValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value, + public MapValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value, Map context, List failures) { super(scope, configuration, name, value, context, failures); @@ -140,18 +104,18 @@ public PrimitiveUnsignedIntegerValidator size() { if (hasFailed()) { - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".size()", 0, - null, Pluralizer.ENTRY); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", 0, + name, null, Pluralizer.ENTRY, context, failures); } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".size()", 0, - null, Pluralizer.ENTRY); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", 0, + name, null, Pluralizer.ENTRY, context, failures); } - return new PrimitiveUnsignedIntegerValidatorImpl(scope, - this, name + ".size()", value.size(), value, Pluralizer.ENTRY). + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".size()", value.size(), + name, value, Pluralizer.ENTRY, context, failures). putContext(value, name); } @@ -159,28 +123,38 @@ public PrimitiveUnsignedIntegerValidator size() public CollectionValidator> keySet() { if (hasFailed()) - return new CollectionValidatorImpl<>(scope, this, name + ".keySet()", null, Pluralizer.KEY); + { + return new CollectionValidatorImpl<>(scope, configuration, name + ".keySet()", null, Pluralizer.KEY, + context, failures); + } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new CollectionValidatorImpl<>(scope, this, name + ".keySet()", null, Pluralizer.KEY); + return new CollectionValidatorImpl<>(scope, configuration, name + ".keySet()", null, Pluralizer.KEY, + context, failures); } - return new CollectionValidatorImpl<>(scope, this, name + ".keySet()", value.keySet(), Pluralizer.KEY); + return new CollectionValidatorImpl<>(scope, configuration, name + ".keySet()", value.keySet(), + Pluralizer.KEY, context, failures); } @Override public CollectionValidator> values() { if (hasFailed()) - return new CollectionValidatorImpl<>(scope, this, name + ".values()", null, Pluralizer.VALUE); + { + return new CollectionValidatorImpl<>(scope, configuration, name + ".values()", null, Pluralizer.VALUE, + context, failures); + } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new CollectionValidatorImpl<>(scope, this, name + ".values()", null, Pluralizer.VALUE); + return new CollectionValidatorImpl<>(scope, configuration, name + ".values()", null, Pluralizer.VALUE, + context, failures); } - return new CollectionValidatorImpl<>(scope, this, name + ".values()", value.values(), Pluralizer.VALUE); + return new CollectionValidatorImpl<>(scope, configuration, name + ".values()", value.values(), + Pluralizer.VALUE, context, failures); } @Override @@ -188,15 +162,17 @@ public CollectionValidator, Set>> entrySet() { if (hasFailed()) { - return new CollectionValidatorImpl<>(scope, this, name + ".entrySet()", null, Pluralizer.ENTRY); + return new CollectionValidatorImpl<>(scope, configuration, name + ".entrySet()", null, + Pluralizer.ENTRY, context, failures); } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new CollectionValidatorImpl<>(scope, this, name + ".entrySet()", null, Pluralizer.ENTRY); + return new CollectionValidatorImpl<>(scope, configuration, name + ".entrySet()", null, + Pluralizer.ENTRY, context, failures); } - return new CollectionValidatorImpl<>(scope, this, name + ".entrySet()", value.entrySet(), - Pluralizer.ENTRY); + return new CollectionValidatorImpl<>(scope, configuration, name + ".entrySet()", value.entrySet(), + Pluralizer.ENTRY, context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectArrayValidatorImpl.java index c408d63cb..fb5aefb0e 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectArrayValidatorImpl.java @@ -14,9 +14,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.ObjectArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -28,42 +26,6 @@ public final class ObjectArrayValidatorImpl extends AbstractArrayValidator, E, E[]> implements ObjectArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ObjectArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, E[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ObjectArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, E[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -76,8 +38,8 @@ public ObjectArrayValidatorImpl(ApplicationScope scope, Configuration configurat * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private ObjectArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - E[] value, Map context, List failures) + public ObjectArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, E[] value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectValidatorImpl.java index a59398e22..eff52469a 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ObjectValidatorImpl.java @@ -31,9 +31,7 @@ import java.net.URI; import java.net.URL; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -44,41 +42,6 @@ public final class ObjectValidatorImpl extends AbstractObjectValidator, T> implements ObjectValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ObjectValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, T value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ObjectValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -91,8 +54,8 @@ public ObjectValidatorImpl(ApplicationScope scope, Configuration configuration, * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private ObjectValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - T value, Map context, List failures) + public ObjectValidatorImpl(ApplicationScope scope, Configuration configuration, String name, T value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } @@ -107,7 +70,7 @@ public T getValue() public PrimitiveByteValidatorImpl isByte() { if (hasFailed()) - return new PrimitiveByteValidatorImpl(scope, this, name, (byte) 0); + return new PrimitiveByteValidatorImpl(scope, configuration, name, (byte) 0, context, failures); else if (value == null) { addNullPointerException( @@ -118,16 +81,16 @@ else if (!(value instanceof Byte)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Byte.class). toString()); - return new PrimitiveByteValidatorImpl(scope, this, name, (byte) 0); + return new PrimitiveByteValidatorImpl(scope, configuration, name, (byte) 0, context, failures); } - return new PrimitiveByteValidatorImpl(scope, this, name, (byte) value); + return new PrimitiveByteValidatorImpl(scope, configuration, name, (byte) value, context, failures); } @Override public PrimitiveShortValidatorImpl isShort() { if (hasFailed()) - return new PrimitiveShortValidatorImpl(scope, this, name, (short) 0); + return new PrimitiveShortValidatorImpl(scope, configuration, name, (short) 0, context, failures); else if (value == null) { addNullPointerException( @@ -138,16 +101,16 @@ else if (!(value instanceof Short)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Short.class). toString()); - return new PrimitiveShortValidatorImpl(scope, this, name, (short) 0); + return new PrimitiveShortValidatorImpl(scope, configuration, name, (short) 0, context, failures); } - return new PrimitiveShortValidatorImpl(scope, this, name, (short) value); + return new PrimitiveShortValidatorImpl(scope, configuration, name, (short) value, context, failures); } @Override public PrimitiveIntegerValidatorImpl isInt() { if (hasFailed()) - return new PrimitiveIntegerValidatorImpl(scope, this, name, 0); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, 0, context, failures); else if (value == null) { addNullPointerException( @@ -158,16 +121,16 @@ else if (!(value instanceof Integer)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Integer.class). toString()); - return new PrimitiveIntegerValidatorImpl(scope, this, name, 0); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, 0, context, failures); } - return new PrimitiveIntegerValidatorImpl(scope, this, name, (int) value); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, (int) value, context, failures); } @Override public PrimitiveLongValidatorImpl isLong() { if (hasFailed()) - return new PrimitiveLongValidatorImpl(scope, this, name, 0L); + return new PrimitiveLongValidatorImpl(scope, configuration, name, 0L, context, failures); else if (value == null) { addNullPointerException( @@ -178,16 +141,16 @@ else if (!(value instanceof Long)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Long.class). toString()); - return new PrimitiveLongValidatorImpl(scope, this, name, 0L); + return new PrimitiveLongValidatorImpl(scope, configuration, name, 0L, context, failures); } - return new PrimitiveLongValidatorImpl(scope, this, name, (long) value); + return new PrimitiveLongValidatorImpl(scope, configuration, name, (long) value, context, failures); } @Override public PrimitiveFloatValidatorImpl isFloat() { if (hasFailed()) - return new PrimitiveFloatValidatorImpl(scope, this, name, 0.0f); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, 0.0f, context, failures); else if (value == null) { addNullPointerException( @@ -198,16 +161,16 @@ else if (!(value instanceof Float)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Float.class). toString()); - return new PrimitiveFloatValidatorImpl(scope, this, name, 0.0f); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, 0.0f, context, failures); } - return new PrimitiveFloatValidatorImpl(scope, this, name, (float) value); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, (float) value, context, failures); } @Override public PrimitiveDoubleValidatorImpl isDouble() { if (hasFailed()) - return new PrimitiveDoubleValidatorImpl(scope, this, name, 0.0); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, 0.0, context, failures); else if (value == null) { addNullPointerException( @@ -218,16 +181,16 @@ else if (!(value instanceof Double)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Double.class). toString()); - return new PrimitiveDoubleValidatorImpl(scope, this, name, 0.0); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, 0.0, context, failures); } - return new PrimitiveDoubleValidatorImpl(scope, this, name, (double) value); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, (double) value, context, failures); } @Override public PrimitiveBooleanValidatorImpl isBoolean() { if (hasFailed()) - return new PrimitiveBooleanValidatorImpl(scope, this, name, false); + return new PrimitiveBooleanValidatorImpl(scope, configuration, name, false, context, failures); else if (value == null) { addNullPointerException( @@ -238,16 +201,16 @@ else if (!(value instanceof Boolean)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Boolean.class). toString()); - return new PrimitiveBooleanValidatorImpl(scope, this, name, false); + return new PrimitiveBooleanValidatorImpl(scope, configuration, name, false, context, failures); } - return new PrimitiveBooleanValidatorImpl(scope, this, name, (boolean) value); + return new PrimitiveBooleanValidatorImpl(scope, configuration, name, (boolean) value, context, failures); } @Override public PrimitiveCharacterValidatorImpl isChar() { if (hasFailed()) - return new PrimitiveCharacterValidatorImpl(scope, this, name, '-'); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, '-', context, failures); else if (value == null) { addNullPointerException( @@ -258,16 +221,16 @@ else if (!(value instanceof Character)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Character.class). toString()); - return new PrimitiveCharacterValidatorImpl(scope, this, name, '-'); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, '-', context, failures); } - return new PrimitiveCharacterValidatorImpl(scope, this, name, (char) value); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, (char) value, context, failures); } @Override public BigIntegerValidatorImpl isBigInteger() { if (hasFailed()) - return new BigIntegerValidatorImpl(scope, this, name, null); + return new BigIntegerValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -278,16 +241,16 @@ else if (!(value instanceof BigInteger)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, BigInteger.class). toString()); - return new BigIntegerValidatorImpl(scope, this, name, null); + return new BigIntegerValidatorImpl(scope, configuration, name, null, context, failures); } - return new BigIntegerValidatorImpl(scope, this, name, (BigInteger) value); + return new BigIntegerValidatorImpl(scope, configuration, name, (BigInteger) value, context, failures); } @Override public BigDecimalValidatorImpl isBigDecimal() { if (hasFailed()) - return new BigDecimalValidatorImpl(scope, this, name, null); + return new BigDecimalValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -298,16 +261,16 @@ else if (!(value instanceof BigDecimal)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, BigDecimal.class). toString()); - return new BigDecimalValidatorImpl(scope, this, name, null); + return new BigDecimalValidatorImpl(scope, configuration, name, null, context, failures); } - return new BigDecimalValidatorImpl(scope, this, name, (BigDecimal) value); + return new BigDecimalValidatorImpl(scope, configuration, name, (BigDecimal) value, context, failures); } @Override public ComparableValidatorImpl isComparable() { if (hasFailed()) - return new ComparableValidatorImpl<>(scope, this, name, null); + return new ComparableValidatorImpl<>(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -318,16 +281,19 @@ else if (!(value instanceof Comparable)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Comparable.class). toString()); - return new ComparableValidatorImpl<>(scope, this, name, null); + return new ComparableValidatorImpl<>(scope, configuration, name, null, context, failures); } - return new ComparableValidatorImpl<>(scope, this, name, (BigDecimal) value); + return new ComparableValidatorImpl<>(scope, configuration, name, (BigDecimal) value, context, failures); } @Override public CollectionValidator> isCollection() { if (hasFailed()) - return new CollectionValidatorImpl<>(scope, this, name, null, Pluralizer.ELEMENT); + { + return new CollectionValidatorImpl<>(scope, configuration, name, null, Pluralizer.ELEMENT, context, + failures); + } else if (value == null) { addNullPointerException( @@ -338,17 +304,21 @@ else if (!(value instanceof Collection)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Collection.class). toString()); - return new CollectionValidatorImpl<>(scope, this, name, null, Pluralizer.ELEMENT); + return new CollectionValidatorImpl<>(scope, configuration, name, null, Pluralizer.ELEMENT, context, + failures); } - return new CollectionValidatorImpl<>(scope, this, name, (Collection) value, - Pluralizer.ELEMENT); + return new CollectionValidatorImpl<>(scope, configuration, name, (Collection) value, + Pluralizer.ELEMENT, context, failures); } @Override public ListValidator> isList() { if (hasFailed()) - return new ListValidatorImpl<>(scope, this, name, null, Pluralizer.ELEMENT); + { + return new ListValidatorImpl<>(scope, configuration, name, null, Pluralizer.ELEMENT, context, + failures); + } else if (value == null) { addNullPointerException( @@ -359,16 +329,18 @@ else if (!(value instanceof List)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, List.class). toString()); - return new ListValidatorImpl<>(scope, this, name, null, Pluralizer.ELEMENT); + return new ListValidatorImpl<>(scope, configuration, name, null, Pluralizer.ELEMENT, context, + failures); } - return new ListValidatorImpl<>(scope, this, name, (List) value, Pluralizer.ELEMENT); + return new ListValidatorImpl<>(scope, configuration, name, (List) value, Pluralizer.ELEMENT, context, + failures); } @Override public PrimitiveByteArrayValidator isByteArray() { if (hasFailed()) - return new PrimitiveByteArrayValidatorImpl(scope, this, name, null); + return new PrimitiveByteArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -379,16 +351,16 @@ else if (!(value instanceof byte[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, byte[].class). toString()); - return new PrimitiveByteArrayValidatorImpl(scope, this, name, null); + return new PrimitiveByteArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveByteArrayValidatorImpl(scope, this, name, (byte[]) value); + return new PrimitiveByteArrayValidatorImpl(scope, configuration, name, (byte[]) value, context, failures); } @Override public PrimitiveShortArrayValidator isShortArray() { if (hasFailed()) - return new PrimitiveShortArrayValidatorImpl(scope, this, name, null); + return new PrimitiveShortArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -399,16 +371,16 @@ else if (!(value instanceof short[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, short[].class). toString()); - return new PrimitiveShortArrayValidatorImpl(scope, this, name, null); + return new PrimitiveShortArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveShortArrayValidatorImpl(scope, this, name, (short[]) value); + return new PrimitiveShortArrayValidatorImpl(scope, configuration, name, (short[]) value, context, failures); } @Override public PrimitiveIntegerArrayValidator isIntArray() { if (hasFailed()) - return new PrimitiveIntegerArrayValidatorImpl(scope, this, name, null); + return new PrimitiveIntegerArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -419,16 +391,17 @@ else if (!(value instanceof int[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, int[].class). toString()); - return new PrimitiveIntegerArrayValidatorImpl(scope, this, name, null); + return new PrimitiveIntegerArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveIntegerArrayValidatorImpl(scope, this, name, (int[]) value); + return new PrimitiveIntegerArrayValidatorImpl(scope, configuration, name, (int[]) value, context, + failures); } @Override public PrimitiveLongArrayValidator isLongArray() { if (hasFailed()) - return new PrimitiveLongArrayValidatorImpl(scope, this, name, null); + return new PrimitiveLongArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -439,16 +412,16 @@ else if (!(value instanceof long[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, long[].class). toString()); - return new PrimitiveLongArrayValidatorImpl(scope, this, name, null); + return new PrimitiveLongArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveLongArrayValidatorImpl(scope, this, name, (long[]) value); + return new PrimitiveLongArrayValidatorImpl(scope, configuration, name, (long[]) value, context, failures); } @Override public PrimitiveFloatArrayValidator isFloatArray() { if (hasFailed()) - return new PrimitiveFloatArrayValidatorImpl(scope, this, name, null); + return new PrimitiveFloatArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -459,16 +432,17 @@ else if (!(value instanceof float[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, float[].class). toString()); - return new PrimitiveFloatArrayValidatorImpl(scope, this, name, null); + return new PrimitiveFloatArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveFloatArrayValidatorImpl(scope, this, name, (float[]) value); + return new PrimitiveFloatArrayValidatorImpl(scope, configuration, name, (float[]) value, context, + failures); } @Override public PrimitiveDoubleArrayValidator isDoubleArray() { if (hasFailed()) - return new PrimitiveDoubleArrayValidatorImpl(scope, this, name, null); + return new PrimitiveDoubleArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -479,16 +453,17 @@ else if (!(value instanceof double[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, double[].class). toString()); - return new PrimitiveDoubleArrayValidatorImpl(scope, this, name, null); + return new PrimitiveDoubleArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveDoubleArrayValidatorImpl(scope, this, name, (double[]) value); + return new PrimitiveDoubleArrayValidatorImpl(scope, configuration, name, (double[]) value, context, + failures); } @Override public PrimitiveBooleanArrayValidator isBooleanArray() { if (hasFailed()) - return new PrimitiveBooleanArrayValidatorImpl(scope, this, name, null); + return new PrimitiveBooleanArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -499,16 +474,17 @@ else if (!(value instanceof boolean[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, boolean[].class). toString()); - return new PrimitiveBooleanArrayValidatorImpl(scope, this, name, null); + return new PrimitiveBooleanArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveBooleanArrayValidatorImpl(scope, this, name, (boolean[]) value); + return new PrimitiveBooleanArrayValidatorImpl(scope, configuration, name, (boolean[]) value, context, + failures); } @Override public PrimitiveCharacterArrayValidator isCharArray() { if (hasFailed()) - return new PrimitiveCharacterArrayValidatorImpl(scope, this, name, null); + return new PrimitiveCharacterArrayValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -519,16 +495,17 @@ else if (!(value instanceof char[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, char[].class). toString()); - return new PrimitiveCharacterArrayValidatorImpl(scope, this, name, null); + return new PrimitiveCharacterArrayValidatorImpl(scope, configuration, name, null, context, failures); } - return new PrimitiveCharacterArrayValidatorImpl(scope, this, name, (char[]) value); + return new PrimitiveCharacterArrayValidatorImpl(scope, configuration, name, (char[]) value, context, + failures); } @Override public ObjectArrayValidator isObjectArray() { if (hasFailed()) - return new ObjectArrayValidatorImpl<>(scope, this, name, null); + return new ObjectArrayValidatorImpl<>(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -539,16 +516,16 @@ else if (!(value instanceof Object[])) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Object[].class). toString()); - return new ObjectArrayValidatorImpl<>(scope, this, name, null); + return new ObjectArrayValidatorImpl<>(scope, configuration, name, null, context, failures); } - return new ObjectArrayValidatorImpl<>(scope, this, name, (Object[]) value); + return new ObjectArrayValidatorImpl<>(scope, configuration, name, (Object[]) value, context, failures); } @Override public MapValidator> isMap() { if (hasFailed()) - return new MapValidatorImpl<>(scope, this, name, null); + return new MapValidatorImpl<>(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -559,16 +536,16 @@ else if (!(value instanceof Map)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Map.class). toString()); - return new MapValidatorImpl<>(scope, this, name, null); + return new MapValidatorImpl<>(scope, configuration, name, null, context, failures); } - return new MapValidatorImpl<>(scope, this, name, (Map) value); + return new MapValidatorImpl<>(scope, configuration, name, (Map) value, context, failures); } @Override public PathValidator isPath() { if (hasFailed()) - return new PathValidatorImpl(scope, this, name, null); + return new PathValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -579,16 +556,16 @@ else if (!(value instanceof Path)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Path.class). toString()); - return new PathValidatorImpl(scope, this, name, null); + return new PathValidatorImpl(scope, configuration, name, null, context, failures); } - return new PathValidatorImpl(scope, this, name, (Path) value); + return new PathValidatorImpl(scope, configuration, name, (Path) value, context, failures); } @Override public StringValidatorImpl isString() { if (hasFailed()) - return new StringValidatorImpl(scope, this, name, null); + return new StringValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -599,16 +576,16 @@ else if (!(value instanceof String)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, String.class). toString()); - return new StringValidatorImpl(scope, this, name, null); + return new StringValidatorImpl(scope, configuration, name, null, context, failures); } - return new StringValidatorImpl(scope, this, name, (String) value); + return new StringValidatorImpl(scope, configuration, name, (String) value, context, failures); } @Override public UriValidatorImpl isUri() { if (hasFailed()) - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -619,16 +596,16 @@ else if (!(value instanceof URI)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, URI.class). toString()); - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); } - return new UriValidatorImpl(scope, this, name, (URI) value); + return new UriValidatorImpl(scope, configuration, name, (URI) value, context, failures); } @Override public UrlValidatorImpl isUrl() { if (hasFailed()) - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -639,16 +616,16 @@ else if (!(value instanceof URL)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, URL.class). toString()); - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); } - return new UrlValidatorImpl(scope, this, name, (URL) value); + return new UrlValidatorImpl(scope, configuration, name, (URL) value, context, failures); } @Override public ClassValidatorImpl isClass() { if (hasFailed()) - return new ClassValidatorImpl<>(scope, this, name, null); + return new ClassValidatorImpl<>(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -659,16 +636,16 @@ else if (!(value instanceof Class)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Class.class). toString()); - return new ClassValidatorImpl<>(scope, this, name, null); + return new ClassValidatorImpl<>(scope, configuration, name, null, context, failures); } - return new ClassValidatorImpl<>(scope, this, name, (Class) value); + return new ClassValidatorImpl<>(scope, configuration, name, (Class) value, context, failures); } @Override public OptionalValidator isOptional() { if (hasFailed()) - return new OptionalValidatorImpl<>(scope, this, name, null); + return new OptionalValidatorImpl<>(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -679,16 +656,16 @@ else if (!(value instanceof Optional)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, Optional.class). toString()); - return new OptionalValidatorImpl<>(scope, this, name, null); + return new OptionalValidatorImpl<>(scope, configuration, name, null, context, failures); } - return new OptionalValidatorImpl<>(scope, this, name, (Optional) value); + return new OptionalValidatorImpl<>(scope, configuration, name, (Optional) value, context, failures); } @Override public InetAddressValidatorImpl isInetAddress() { if (hasFailed()) - return new InetAddressValidatorImpl(scope, this, name, null); + return new InetAddressValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( @@ -699,8 +676,8 @@ else if (!(value instanceof InetAddress)) addIllegalArgumentException( ObjectMessages.isInstanceOf(scope, this, this.name, value, true, InetAddress.class). toString()); - return new InetAddressValidatorImpl(scope, this, name, null); + return new InetAddressValidatorImpl(scope, configuration, name, null, context, failures); } - return new InetAddressValidatorImpl(scope, this, name, (InetAddress) value); + return new InetAddressValidatorImpl(scope, configuration, name, (InetAddress) value, context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/OptionalValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/OptionalValidatorImpl.java index 72a41bd4d..b4a465baf 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/OptionalValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/OptionalValidatorImpl.java @@ -12,8 +12,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.OptionalValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -22,42 +20,6 @@ public final class OptionalValidatorImpl extends AbstractObjectValidator, Optional> implements OptionalValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public OptionalValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, Optional value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public OptionalValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, Optional value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -70,7 +32,7 @@ public OptionalValidatorImpl(ApplicationScope scope, Configuration configuration * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private OptionalValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + public OptionalValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Optional value, Map context, List failures) { super(scope, configuration, name, value, context, failures); diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PathValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PathValidatorImpl.java index 7c321f19b..cde7c35e1 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PathValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PathValidatorImpl.java @@ -17,50 +17,12 @@ import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class PathValidatorImpl extends AbstractObjectValidator implements PathValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PathValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, Path value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PathValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, Path value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -73,8 +35,8 @@ public PathValidatorImpl(ApplicationScope scope, Configuration configuration, * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PathValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Path value, Map context, List failures) + public PathValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Path value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanArrayValidatorImpl.java index 62e19809e..14c4cfece 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveBooleanArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,42 +18,6 @@ public final class PrimitiveBooleanArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveBooleanArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveBooleanArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, boolean[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveBooleanArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, boolean[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public PrimitiveBooleanArrayValidatorImpl(ApplicationScope scope, Configuration * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveBooleanArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, boolean[] value, Map context, List failures) + public PrimitiveBooleanArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + boolean[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanValidatorImpl.java index 248a64ddf..0c3b4def4 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveBooleanValidatorImpl.java @@ -7,8 +7,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveBooleanValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,42 +15,6 @@ public final class PrimitiveBooleanValidatorImpl extends AbstractValidator validator, String name, - boolean value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveBooleanValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, boolean value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -65,8 +27,8 @@ public PrimitiveBooleanValidatorImpl(ApplicationScope scope, Configuration confi * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveBooleanValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, boolean value, Map context, List failures) + public PrimitiveBooleanValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + boolean value, Map context, List failures) { super(scope, configuration, name, context, failures); this.value = value; @@ -164,6 +126,7 @@ private PrimitiveBooleanValidator isNotEqualToImpl(boolean unwanted, String name @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteArrayValidatorImpl.java index 346282a2c..ed67dc463 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveByteArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,42 +18,6 @@ public final class PrimitiveByteArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveByteArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveByteArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - byte[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveByteArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, byte[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public PrimitiveByteArrayValidatorImpl(ApplicationScope scope, Configuration con * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveByteArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, byte[] value, Map context, List failures) + public PrimitiveByteArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + byte[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteValidatorImpl.java index 5dd79d8d5..fc1c059ca 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveByteValidatorImpl.java @@ -8,8 +8,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveByteValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,42 +16,6 @@ public final class PrimitiveByteValidatorImpl extends AbstractValidator validator, String name, - byte value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveByteValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - byte value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -66,7 +28,7 @@ public PrimitiveByteValidatorImpl(ApplicationScope scope, Configuration configur * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveByteValidatorImpl(ApplicationScope scope, Configuration configuration, + public PrimitiveByteValidatorImpl(ApplicationScope scope, Configuration configuration, String name, byte value, Map context, List failures) { super(scope, configuration, name, context, failures); @@ -470,6 +432,7 @@ private boolean maximumFailed(byte maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterArrayValidatorImpl.java index 58cb1fed8..cd38da319 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveCharacterArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,43 +18,6 @@ public final class PrimitiveCharacterArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveCharacterArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveCharacterArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, - char[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveCharacterArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, char[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -69,7 +30,7 @@ public PrimitiveCharacterArrayValidatorImpl(ApplicationScope scope, Configuratio * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveCharacterArrayValidatorImpl(ApplicationScope scope, Configuration configuration, + public PrimitiveCharacterArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, char[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterValidatorImpl.java index cf5f17469..06d87a991 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveCharacterValidatorImpl.java @@ -7,8 +7,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveCharacterValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -17,42 +15,6 @@ public final class PrimitiveCharacterValidatorImpl extends AbstractValidator validator, String name, - char value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveCharacterValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, char value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -65,8 +27,8 @@ public PrimitiveCharacterValidatorImpl(ApplicationScope scope, Configuration con * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveCharacterValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, char value, Map context, List failures) + public PrimitiveCharacterValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + char value, Map context, List failures) { super(scope, configuration, name, context, failures); this.value = value; @@ -305,6 +267,7 @@ private boolean maximumFailed(char maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleArrayValidatorImpl.java index 304a9922f..485ffc518 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveDoubleArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,42 +18,6 @@ public final class PrimitiveDoubleArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveDoubleArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveDoubleArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, double[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveDoubleArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, double[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public PrimitiveDoubleArrayValidatorImpl(ApplicationScope scope, Configuration c * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveDoubleArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, double[] value, Map context, List failures) + public PrimitiveDoubleArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + double[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleValidatorImpl.java index 75ade1828..7c2f7073f 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveDoubleValidatorImpl.java @@ -8,8 +8,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveDoubleValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,42 +16,6 @@ public final class PrimitiveDoubleValidatorImpl extends AbstractValidator validator, String name, - double value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveDoubleValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, double value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -66,8 +28,8 @@ public PrimitiveDoubleValidatorImpl(ApplicationScope scope, Configuration config * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveDoubleValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, double value, Map context, List failures) + public PrimitiveDoubleValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + double value, Map context, List failures) { super(scope, configuration, name, context, failures); this.value = value; @@ -577,6 +539,7 @@ private boolean maximumFailed(double maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatArrayValidatorImpl.java index 2fa802a24..648fbcd03 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveFloatArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,42 +18,6 @@ public final class PrimitiveFloatArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveFloatArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveFloatArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, float[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveFloatArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, float[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public PrimitiveFloatArrayValidatorImpl(ApplicationScope scope, Configuration co * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveFloatArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, float[] value, Map context, List failures) + public PrimitiveFloatArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + float[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatValidatorImpl.java index 41a1a5f9b..0168ad3ad 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveFloatValidatorImpl.java @@ -8,8 +8,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveFloatValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,42 +16,6 @@ public final class PrimitiveFloatValidatorImpl extends AbstractValidator validator, String name, - float value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveFloatValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, float value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -66,8 +28,8 @@ public PrimitiveFloatValidatorImpl(ApplicationScope scope, Configuration configu * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveFloatValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, float value, Map context, List failures) + public PrimitiveFloatValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + float value, Map context, List failures) { super(scope, configuration, name, context, failures); this.value = value; @@ -578,6 +540,7 @@ private boolean maximumFailed(float maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerArrayValidatorImpl.java index 0f2f84e14..89272b4a1 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveIntegerArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,42 +18,6 @@ public final class PrimitiveIntegerArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveIntegerArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveIntegerArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, int[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveIntegerArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, int[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public PrimitiveIntegerArrayValidatorImpl(ApplicationScope scope, Configuration * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveIntegerArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, int[] value, Map context, List failures) + public PrimitiveIntegerArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + int[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerValidatorImpl.java index fdd9e107b..f9de831f0 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveIntegerValidatorImpl.java @@ -8,8 +8,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveIntegerValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,42 +16,6 @@ public final class PrimitiveIntegerValidatorImpl extends AbstractValidator validator, String name, - int value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, int value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -66,8 +28,8 @@ public PrimitiveIntegerValidatorImpl(ApplicationScope scope, Configuration confi * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, int value, Map context, List failures) + public PrimitiveIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + int value, Map context, List failures) { super(scope, configuration, name, context, failures); this.value = value; @@ -472,6 +434,7 @@ private boolean maximumFailed(int maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongArrayValidatorImpl.java index e7755a685..0686feab5 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveLongArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,42 +18,6 @@ public final class PrimitiveLongArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveLongArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveLongArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, long[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveLongArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, long[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public PrimitiveLongArrayValidatorImpl(ApplicationScope scope, Configuration con * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveLongArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, long[] value, Map context, List failures) + public PrimitiveLongArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + long[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongValidatorImpl.java index 46116e885..13aa2e432 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveLongValidatorImpl.java @@ -8,8 +8,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveLongValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,42 +16,6 @@ public final class PrimitiveLongValidatorImpl extends AbstractValidator validator, String name, - long value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveLongValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, long value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -66,8 +28,7 @@ public PrimitiveLongValidatorImpl(ApplicationScope scope, Configuration configur * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveLongValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, + public PrimitiveLongValidatorImpl(ApplicationScope scope, Configuration configuration, String name, long value, Map context, List failures) { super(scope, configuration, name, context, failures); @@ -462,6 +423,7 @@ private boolean maximumFailed(long maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortArrayValidatorImpl.java index 77e5d8234..6e2f72e7e 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortArrayValidatorImpl.java @@ -8,9 +8,7 @@ import com.github.cowwoc.requirements.java.internal.util.Arrays; import com.github.cowwoc.requirements.java.type.PrimitiveShortArrayValidator; -import java.util.ArrayList; import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -20,42 +18,6 @@ public final class PrimitiveShortArrayValidatorImpl extends AbstractArrayValidator implements PrimitiveShortArrayValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveShortArrayValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, - short[] value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveShortArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, short[] value) - { - this(scope, configuration, name, value, HashMap.newHashMap(4), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -68,8 +30,8 @@ public PrimitiveShortArrayValidatorImpl(ApplicationScope scope, Configuration co * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveShortArrayValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, short[] value, Map context, List failures) + public PrimitiveShortArrayValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + short[] value, Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortValidatorImpl.java index 42fd25e35..b15bb5af8 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveShortValidatorImpl.java @@ -8,8 +8,6 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.PrimitiveShortValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,42 +16,6 @@ public final class PrimitiveShortValidatorImpl extends AbstractValidator validator, String name, - short value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveShortValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, short value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -66,8 +28,8 @@ public PrimitiveShortValidatorImpl(ApplicationScope scope, Configuration configu * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveShortValidatorImpl(ApplicationScope scope, Configuration configuration, - String name, short value, Map context, List failures) + public PrimitiveShortValidatorImpl(ApplicationScope scope, Configuration configuration, String name, + short value, Map context, List failures) { super(scope, configuration, name, context, failures); this.value = value; @@ -459,6 +421,7 @@ private boolean maximumFailed(short maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, "String.valueOf(" + name + ")", String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, "String.valueOf(" + name + ")", + String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveUnsignedIntegerValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveUnsignedIntegerValidatorImpl.java index e8f8555cc..9e382997e 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveUnsignedIntegerValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/PrimitiveUnsignedIntegerValidatorImpl.java @@ -19,27 +19,6 @@ public final class PrimitiveUnsignedIntegerValidatorImpl extends AbstractValidat private final Object parent; private final Pluralizer pluralizer; - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @param parent the object that contains this value - * @param pluralizer (optional) the type of items in the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public PrimitiveUnsignedIntegerValidatorImpl(ApplicationScope scope, AbstractValidator validator, - String name, int value, Object parent, Pluralizer pluralizer) - { - this(scope, validator.configuration(), name, value, validator.name, parent, pluralizer, validator.context, - validator.failures); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -50,12 +29,12 @@ public PrimitiveUnsignedIntegerValidatorImpl(ApplicationScope scope, AbstractVal * @param pluralizer (optional) the type of items in the value * @param context the contextual information set by the user * @param failures the list of validation failures - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private PrimitiveUnsignedIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, + public PrimitiveUnsignedIntegerValidatorImpl(ApplicationScope scope, Configuration configuration, String nameOfValue, int value, String nameOfParent, Object parent, Pluralizer pluralizer, Map context, List failures) { @@ -615,6 +594,6 @@ private boolean maximumFailed(int maximum, boolean maximumInclusive) @Override public StringValidatorImpl asString() { - return new StringValidatorImpl(scope, this, name, String.valueOf(value)); + return new StringValidatorImpl(scope, configuration, name, String.valueOf(value), context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ScopedContextImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ScopedContextImpl.java deleted file mode 100644 index 9df34d571..000000000 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ScopedContextImpl.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.github.cowwoc.requirements.java.internal.implementation; - -import com.github.cowwoc.requirements.java.ScopedContext; -import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public final class ScopedContextImpl implements ScopedContext -{ - private final ApplicationScope scope; - private final Set updatedEntries = new HashSet<>(); - private final Map threadContext; - private final Map nameToOldValue; - - public ScopedContextImpl(ApplicationScope scope) - { - assert scope != null; - this.scope = scope; - this.threadContext = scope.getThreadContext().get(); - this.nameToOldValue = HashMap.newHashMap(threadContext.size()); - } - - @Override - public Object get(String name) - { - return threadContext.get(name); - } - - @Override - public boolean containsName(String name) - { - return threadContext.containsKey(name); - } - - @Override - public ScopedContext put(Object value, String name) - { - scope.getInternalValidator().requireThat(name, "name").isStripped(); - if (threadContext.containsKey(name)) - { - Object oldValue = threadContext.put(name, value); - if (updatedEntries.add(name)) - nameToOldValue.put(name, oldValue); - } - else - updatedEntries.add(name); - threadContext.put(name, value); - return this; - } - - @Override - public ScopedContext remove(String name) - { - scope.getInternalValidator().requireThat(name, "name").isStripped(); - if (threadContext.containsKey(name)) - { - Object oldValue = threadContext.remove(name); - if (updatedEntries.add(name)) - nameToOldValue.put(name, oldValue); - } - return this; - } - - public void close() - { - for (String name : updatedEntries) - { - if (nameToOldValue.containsKey(name)) - { - Object oldValue = nameToOldValue.get(name); - threadContext.put(name, oldValue); - } - else - threadContext.remove(name); - } - updatedEntries.clear(); - nameToOldValue.clear(); - } -} \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ShortValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ShortValidatorImpl.java index da3a78844..e273d56d4 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ShortValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ShortValidatorImpl.java @@ -8,49 +8,12 @@ import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.ShortValidator; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class ShortValidatorImpl extends AbstractObjectValidator implements ShortValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ShortValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, Short value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public ShortValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Short value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -63,8 +26,8 @@ public ShortValidatorImpl(ApplicationScope scope, Configuration configuration, S * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private ShortValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - Short value, Map context, List failures) + public ShortValidatorImpl(ApplicationScope scope, Configuration configuration, String name, Short value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/StringValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/StringValidatorImpl.java index 1a72866f3..2e2d99afd 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/StringValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/StringValidatorImpl.java @@ -39,8 +39,6 @@ import java.net.UnknownHostException; import java.nio.file.InvalidPathException; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,42 +47,6 @@ public final class StringValidatorImpl extends AbstractObjectValidator validator, String name, - String value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public StringValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - String value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -92,13 +54,13 @@ public StringValidatorImpl(ApplicationScope scope, Configuration configuration, * @param value (optional) the value * @param context the contextual information set by the user * @param failures the list of validation failures - * @throws NullPointerException if {@code name} is null + * @throws NullPointerException if any of the mandatory arguments are null * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private StringValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - String value, Map context, List failures) + public StringValidatorImpl(ApplicationScope scope, Configuration configuration, String name, String value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } @@ -300,13 +262,13 @@ public PrimitiveByteValidator asByte() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a byte."). toString()); - return new PrimitiveByteValidatorImpl(scope, this, name, (byte) 0); + return new PrimitiveByteValidatorImpl(scope, configuration, name, (byte) 0, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveByteValidatorImpl(scope, this, name, (byte) 0); + return new PrimitiveByteValidatorImpl(scope, configuration, name, (byte) 0, context, failures); } byte valueAsByte; @@ -318,9 +280,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a byte."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new PrimitiveByteValidatorImpl(scope, this, name, (byte) 0); + return new PrimitiveByteValidatorImpl(scope, configuration, name, (byte) 0, context, failures); } - return new PrimitiveByteValidatorImpl(scope, this, name, valueAsByte); + return new PrimitiveByteValidatorImpl(scope, configuration, name, valueAsByte, context, failures); } @Override @@ -331,13 +293,13 @@ public PrimitiveShortValidator asShort() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a short."). toString()); - return new PrimitiveShortValidatorImpl(scope, this, name, (short) 0); + return new PrimitiveShortValidatorImpl(scope, configuration, name, (short) 0, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveShortValidatorImpl(scope, this, name, (short) 0); + return new PrimitiveShortValidatorImpl(scope, configuration, name, (short) 0, context, failures); } short valueAsShort; @@ -349,9 +311,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a short."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new PrimitiveShortValidatorImpl(scope, this, name, (short) 0); + return new PrimitiveShortValidatorImpl(scope, configuration, name, (short) 0, context, failures); } - return new PrimitiveShortValidatorImpl(scope, this, name, valueAsShort); + return new PrimitiveShortValidatorImpl(scope, configuration, name, valueAsShort, context, failures); } @Override @@ -362,13 +324,13 @@ public PrimitiveIntegerValidator asInt() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a int."). toString()); - return new PrimitiveIntegerValidatorImpl(scope, this, name, 0); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, 0, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveIntegerValidatorImpl(scope, this, name, 0); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, 0, context, failures); } int valueAsInt; @@ -380,9 +342,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a int."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new PrimitiveIntegerValidatorImpl(scope, this, name, 0); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, 0, context, failures); } - return new PrimitiveIntegerValidatorImpl(scope, this, name, valueAsInt); + return new PrimitiveIntegerValidatorImpl(scope, configuration, name, valueAsInt, context, failures); } @Override @@ -393,13 +355,13 @@ public PrimitiveLongValidator asLong() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a long."). toString()); - return new PrimitiveLongValidatorImpl(scope, this, name, 0L); + return new PrimitiveLongValidatorImpl(scope, configuration, name, 0L, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveLongValidatorImpl(scope, this, name, 0L); + return new PrimitiveLongValidatorImpl(scope, configuration, name, 0L, context, failures); } long valueAsLong; @@ -411,9 +373,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a long."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new PrimitiveLongValidatorImpl(scope, this, name, 0L); + return new PrimitiveLongValidatorImpl(scope, configuration, name, 0L, context, failures); } - return new PrimitiveLongValidatorImpl(scope, this, name, valueAsLong); + return new PrimitiveLongValidatorImpl(scope, configuration, name, valueAsLong, context, failures); } @Override @@ -424,13 +386,13 @@ public PrimitiveFloatValidator asFloat() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a float."). toString()); - return new PrimitiveFloatValidatorImpl(scope, this, name, 0.0f); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, 0.0f, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveFloatValidatorImpl(scope, this, name, 0.0f); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, 0.0f, context, failures); } float valueAsFloat; @@ -442,9 +404,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a float."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new PrimitiveFloatValidatorImpl(scope, this, name, 0.0f); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, 0.0f, context, failures); } - return new PrimitiveFloatValidatorImpl(scope, this, name, valueAsFloat); + return new PrimitiveFloatValidatorImpl(scope, configuration, name, valueAsFloat, context, failures); } @Override @@ -455,13 +417,13 @@ public PrimitiveDoubleValidator asDouble() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a double."). toString()); - return new PrimitiveDoubleValidatorImpl(scope, this, name, 0.0f); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, 0.0f, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveDoubleValidatorImpl(scope, this, name, 0.0); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, 0.0, context, failures); } double valueAsDouble; @@ -473,9 +435,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a double."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new PrimitiveDoubleValidatorImpl(scope, this, name, 0.0); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, 0.0, context, failures); } - return new PrimitiveDoubleValidatorImpl(scope, this, name, valueAsDouble); + return new PrimitiveDoubleValidatorImpl(scope, configuration, name, valueAsDouble, context, failures); } @Override @@ -486,17 +448,17 @@ public PrimitiveBooleanValidator asBoolean() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a boolean."). toString()); - return new PrimitiveBooleanValidatorImpl(scope, this, name, false); + return new PrimitiveBooleanValidatorImpl(scope, configuration, name, false, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveBooleanValidatorImpl(scope, this, name, false); + return new PrimitiveBooleanValidatorImpl(scope, configuration, name, false, context, failures); } boolean valueAsBoolean = Boolean.parseBoolean(value); - return new PrimitiveBooleanValidatorImpl(scope, this, name, valueAsBoolean); + return new PrimitiveBooleanValidatorImpl(scope, configuration, name, valueAsBoolean, context, failures); } @Override @@ -507,13 +469,13 @@ public PrimitiveCharacterValidator asChar() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a character."). toString()); - return new PrimitiveCharacterValidatorImpl(scope, this, name, '-'); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, '-', context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveCharacterValidatorImpl(scope, this, name, '-'); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, '-', context, failures); } if (value.length() != 1) @@ -522,9 +484,9 @@ else if (value == null) new MessageBuilder(scope, this, name + " must be convertible to a character."). putContext(value, "Actual"). toString()); - return new PrimitiveCharacterValidatorImpl(scope, this, name, '-'); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, '-', context, failures); } - return new PrimitiveCharacterValidatorImpl(scope, this, name, value.charAt(0)); + return new PrimitiveCharacterValidatorImpl(scope, configuration, name, value.charAt(0), context, failures); } @Override @@ -535,13 +497,13 @@ public BigIntegerValidator asBigInteger() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a BigInteger."). toString()); - return new BigIntegerValidatorImpl(scope, this, name, null); + return new BigIntegerValidatorImpl(scope, configuration, name, null, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new BigIntegerValidatorImpl(scope, this, name, null); + return new BigIntegerValidatorImpl(scope, configuration, name, null, context, failures); } BigInteger valueAsBigInteger; @@ -553,9 +515,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a BigInteger."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new BigIntegerValidatorImpl(scope, this, name, null); + return new BigIntegerValidatorImpl(scope, configuration, name, null, context, failures); } - return new BigIntegerValidatorImpl(scope, this, name, valueAsBigInteger); + return new BigIntegerValidatorImpl(scope, configuration, name, valueAsBigInteger, context, failures); } @Override @@ -566,13 +528,13 @@ public BigDecimalValidator asBigDecimal() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a BigDecimal."). toString()); - return new BigDecimalValidatorImpl(scope, this, name, null); + return new BigDecimalValidatorImpl(scope, configuration, name, null, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new BigDecimalValidatorImpl(scope, this, name, null); + return new BigDecimalValidatorImpl(scope, configuration, name, null, context, failures); } BigDecimal valueAsBigDecimal; @@ -584,9 +546,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a BigDecimal."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new BigDecimalValidatorImpl(scope, this, name, null); + return new BigDecimalValidatorImpl(scope, configuration, name, null, context, failures); } - return new BigDecimalValidatorImpl(scope, this, name, valueAsBigDecimal); + return new BigDecimalValidatorImpl(scope, configuration, name, valueAsBigDecimal, context, failures); } @Override @@ -597,13 +559,13 @@ public PathValidator asPath() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a Path."). toString()); - return new PathValidatorImpl(scope, this, name, null); + return new PathValidatorImpl(scope, configuration, name, null, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PathValidatorImpl(scope, this, name, null); + return new PathValidatorImpl(scope, configuration, name, null, context, failures); } Path valueAsPath; @@ -615,9 +577,9 @@ else if (value == null) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a Path."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new PathValidatorImpl(scope, this, name, null); + return new PathValidatorImpl(scope, configuration, name, null, context, failures); } - return new PathValidatorImpl(scope, this, name, valueAsPath); + return new PathValidatorImpl(scope, configuration, name, valueAsPath, context, failures); } @Override @@ -628,25 +590,25 @@ public UriValidator asUri() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a URI."). toString()); - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); } try { URI uri = URI.create(value); - return new UriValidatorImpl(scope, this, name, uri); + return new UriValidatorImpl(scope, configuration, name, uri, context, failures); } catch (IllegalArgumentException e) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a URI."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); } } @@ -658,25 +620,25 @@ public UrlValidator asUrl() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to a URL."). toString()); - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); } try { URL url = URI.create(value).toURL(); - return new UrlValidatorImpl(scope, this, name, url); + return new UrlValidatorImpl(scope, configuration, name, url, context, failures); } catch (MalformedURLException e) { addFailure(new MessageBuilder(scope, this, name + " must be convertible to a URL."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); } } @@ -688,13 +650,13 @@ public InetAddressValidator asInetAddress() addIllegalArgumentException( new MessageBuilder(scope, this, name + " must be convertible to an InetAddress."). toString()); - return new InetAddressValidatorImpl(scope, this, name, null); + return new InetAddressValidatorImpl(scope, configuration, name, null, context, failures); } else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new InetAddressValidatorImpl(scope, this, name, null); + return new InetAddressValidatorImpl(scope, configuration, name, null, context, failures); } // IPv4 must start with a digit. IPv6 must start with a colon. @@ -702,7 +664,7 @@ else if (value == null) { addIllegalArgumentException( CollectionMessages.isNotEmpty(scope, this, name).toString()); - return new InetAddressValidatorImpl(scope, this, name, null); + return new InetAddressValidatorImpl(scope, configuration, name, null, context, failures); } char firstCharacter = value.charAt(0); @@ -713,7 +675,7 @@ else if (value == null) name + " must contain a valid IP address or hostname format."). putContext(value, "Actual"). toString()); - return new InetAddressValidatorImpl(scope, this, name, null); + return new InetAddressValidatorImpl(scope, configuration, name, null, context, failures); } InetAddress address; @@ -727,9 +689,9 @@ else if (value == null) name + " must contain a valid IP address or hostname format."). putContext(value, "Actual"). toString(), e, IllegalArgumentException::new); - return new InetAddressValidatorImpl(scope, this, name, null); + return new InetAddressValidatorImpl(scope, configuration, name, null, context, failures); } - return new InetAddressValidatorImpl(scope, this, name, address); + return new InetAddressValidatorImpl(scope, configuration, name, address, context, failures); } @Override @@ -905,17 +867,17 @@ public PrimitiveUnsignedIntegerValidator length() { if (hasFailed()) { - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".length()", - 0, null, PLURALIZER); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".length()", + 0, name, null, PLURALIZER, context, failures); } if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, name).toString()); - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".length()", - 0, null, PLURALIZER); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".length()", + 0, name, null, PLURALIZER, context, failures); } - return new PrimitiveUnsignedIntegerValidatorImpl(scope, this, name + ".length()", - value.length(), value, PLURALIZER); + return new PrimitiveUnsignedIntegerValidatorImpl(scope, configuration, name + ".length()", + value.length(), name, value, PLURALIZER, context, failures); } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UriValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UriValidatorImpl.java index c8b1fb8ce..9ce3d2db9 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UriValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UriValidatorImpl.java @@ -15,49 +15,12 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class UriValidatorImpl extends AbstractObjectValidator implements UriValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public UriValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, URI value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public UriValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - URI value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -70,8 +33,8 @@ public UriValidatorImpl(ApplicationScope scope, Configuration configuration, Str * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private UriValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - URI value, Map context, List failures) + public UriValidatorImpl(ApplicationScope scope, Configuration configuration, String name, URI value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } @@ -101,25 +64,25 @@ else if (!value.isAbsolute()) public UrlValidator asUrl() { if (hasFailed()) - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); } else { try { URL url = value.toURL(); - return new UrlValidatorImpl(scope, this, name, url); + return new UrlValidatorImpl(scope, configuration, name, url, context, failures); } catch (MalformedURLException | IllegalArgumentException e) { addFailure(new MessageBuilder(scope, this, name + " is not a valid URL."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new UrlValidatorImpl(scope, this, name, null); + return new UrlValidatorImpl(scope, configuration, name, null, context, failures); } } } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UrlValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UrlValidatorImpl.java index 442d98391..57db072e7 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UrlValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/UrlValidatorImpl.java @@ -15,49 +15,12 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; public final class UrlValidatorImpl extends AbstractObjectValidator implements UrlValidator { - /** - * Creates a new validator as a result of a validation. - * - * @param scope the application configuration - * @param validator the validator - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public UrlValidatorImpl(ApplicationScope scope, AbstractValidator validator, String name, URL value) - { - this(scope, validator.configuration(), name, value, validator.context, validator.failures); - } - - /** - * Creates a new validator. - * - * @param scope the application configuration - * @param configuration the validator configuration - * @param name the name of the value - * @param value (optional) the value - * @throws NullPointerException if {@code name} is null - * @throws IllegalArgumentException if {@code name} contains leading or trailing whitespace, or is empty - * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains - * leading or trailing whitespace, or is empty. - */ - public UrlValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - URL value) - { - this(scope, configuration, name, value, HashMap.newHashMap(2), new ArrayList<>(1)); - } - /** * @param scope the application configuration * @param configuration the validator configuration @@ -70,8 +33,8 @@ public UrlValidatorImpl(ApplicationScope scope, Configuration configuration, Str * @throws AssertionError if any of the mandatory arguments are null. If {@code name} contains * leading or trailing whitespace, or is empty. */ - private UrlValidatorImpl(ApplicationScope scope, Configuration configuration, String name, - URL value, Map context, List failures) + public UrlValidatorImpl(ApplicationScope scope, Configuration configuration, String name, URL value, + Map context, List failures) { super(scope, configuration, name, value, context, failures); } @@ -80,25 +43,25 @@ private UrlValidatorImpl(ApplicationScope scope, Configuration configuration, St public UriValidator asUri() { if (hasFailed()) - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); else if (value == null) { addNullPointerException( ObjectMessages.isNotNull(scope, this, this.name).toString()); - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); } else { try { URI uri = value.toURI(); - return new UriValidatorImpl(scope, this, name, uri); + return new UriValidatorImpl(scope, configuration, name, uri, context, failures); } catch (URISyntaxException e) { addFailure(new MessageBuilder(scope, this, name + " is not a valid URI."). putContext(value, "Actual").toString(), e, IllegalArgumentException::new); - return new UriValidatorImpl(scope, this, name, null); + return new UriValidatorImpl(scope, configuration, name, null, context, failures); } } } diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ValidationFailureImpl.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ValidationFailureImpl.java index 23b207ef8..d74e19f9e 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ValidationFailureImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/ValidationFailureImpl.java @@ -28,8 +28,8 @@ public final class ValidationFailureImpl implements ValidationFailure * @throws AssertionError if any of the arguments are null. If the exception message contains leading or * trailing whitespace, or is empty. */ - public ValidationFailureImpl(Configuration configuration, String message, - Throwable cause, ExceptionBuilder exceptionBuilder) + public ValidationFailureImpl(Configuration configuration, String message, Throwable cause, + ExceptionBuilder exceptionBuilder) { assert (configuration != null); assert (exceptionBuilder != null); diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/message/MessageBuilder.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/message/MessageBuilder.java index b4d53d34b..f184be2f4 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/message/MessageBuilder.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/implementation/message/MessageBuilder.java @@ -90,12 +90,10 @@ public MessageBuilder addDiff(String nameOfValue, Object value, String nameOfExp */ private String getValidatorContext() { - Map threadContext = scope.getThreadContext().get(); Map validatorContext = validator.getContext(); Set existingKeys = new HashSet<>(failureContext.keySet()); Map section = LinkedHashMap.newLinkedHashMap(failureContext.size() + - threadContext.size() + validatorContext.size()); section.putAll(failureContext); @@ -105,10 +103,6 @@ private String getValidatorContext() section.put(entry.getKey(), entry.getValue()); } - for (Entry entry : threadContext.entrySet()) - if (existingKeys.add(entry.getKey())) - section.put(entry.getKey(), entry.getValue()); - StringJoiner joiner = new StringJoiner("\n"); String sectionAsString = contextToString(List.of(section), true); if (!sectionAsString.isEmpty()) diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/AbstractApplicationScope.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/AbstractApplicationScope.java index 8a3ad421c..0d2f824f7 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/AbstractApplicationScope.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/AbstractApplicationScope.java @@ -12,10 +12,6 @@ import com.github.cowwoc.requirements.java.internal.implementation.MutableConfiguration; import com.github.cowwoc.requirements.java.internal.terminal.Terminal; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Supplier; - /** * ApplicationScope for the main and test codebases. */ @@ -26,7 +22,6 @@ public abstract class AbstractApplicationScope implements ApplicationScope * The global configuration. */ private final GlobalConfiguration globalConfiguration; - private final ThreadLocal> threadConfiguration = ThreadLocal.withInitial(HashMap::new); @SuppressWarnings("this-escape") private final Reference internalValidator = ConcurrentLazyReference.create(() -> new JavaValidatorsImpl(this, MutableConfiguration.from(Configuration.DEFAULT).cleanStackTrace(false). @@ -64,10 +59,4 @@ public JavaValidatorsImpl getInternalValidator() { return internalValidator.getValue(); } - - @Override - public Supplier> getThreadContext() - { - return threadConfiguration::get; - } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/ApplicationScope.java b/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/ApplicationScope.java index ff28778ec..d947739a0 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/ApplicationScope.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/internal/scope/ApplicationScope.java @@ -7,9 +7,6 @@ import com.github.cowwoc.requirements.java.GlobalConfiguration; import com.github.cowwoc.requirements.java.internal.implementation.JavaValidatorsImpl; -import java.util.Map; -import java.util.function.Supplier; - /** * The configuration of an application. A JVM may contain multiple applications. */ @@ -20,11 +17,6 @@ public interface ApplicationScope extends JvmScope */ GlobalConfiguration getGlobalConfiguration(); - /** - * @return the contextual information for validations performed by this thread using any validator - */ - Supplier> getThreadContext(); - /** * @return creates validators used to check the arguments of validation methods */ diff --git a/java/src/main/java/com/github/cowwoc/requirements/java/type/part/Validator.java b/java/src/main/java/com/github/cowwoc/requirements/java/type/part/Validator.java index 393767e64..9157e6cfe 100644 --- a/java/src/main/java/com/github/cowwoc/requirements/java/type/part/Validator.java +++ b/java/src/main/java/com/github/cowwoc/requirements/java/type/part/Validator.java @@ -49,8 +49,8 @@ public interface Validator * Password may not be empty * username: john.smith} * - * @return an unmodifiable map with the context information - * @see Validators#threadContext() + * @return an unmodifiable map from each entry's name to its value + * @see Validators#getContext() */ Map getContext(); @@ -59,7 +59,7 @@ public interface Validator *

* This method adds contextual information to exception messages. The contextual information is stored as * key-value pairs in a map. Values set by this method override any values that are set using - * {@link Validators#threadContext()}. + * {@link Validators#putContext(Object, String)}. *

* There is no way to remove contextual information from a validator. Thread-level contextual information is * removed automatically. diff --git a/test/src/test/java/com/github/cowwoc/requirements/test/TestValidators.java b/test/src/test/java/com/github/cowwoc/requirements/test/TestValidators.java index 2335dee76..b4487ae96 100644 --- a/test/src/test/java/com/github/cowwoc/requirements/test/TestValidators.java +++ b/test/src/test/java/com/github/cowwoc/requirements/test/TestValidators.java @@ -24,8 +24,8 @@ * scenarios 1 and 2. For scenario 3, the exception is available via * {@link Validator#elseGetException() validator.elseGetException()}}. */ -public interface TestValidators extends Validators, JavaRequireThat, JavaAssumeThat, JavaCheckIf, - GuavaRequireThat, GuavaAssumeThat, GuavaCheckIf +public interface TestValidators + extends Validators, JavaRequireThat, JavaAssumeThat, JavaCheckIf, GuavaRequireThat, GuavaAssumeThat, GuavaCheckIf { /** * Creates a validator factory with a custom configuration. diff --git a/test/src/test/java/com/github/cowwoc/requirements/test/TestValidatorsImpl.java b/test/src/test/java/com/github/cowwoc/requirements/test/TestValidatorsImpl.java index 7c4612562..326666039 100644 --- a/test/src/test/java/com/github/cowwoc/requirements/test/TestValidatorsImpl.java +++ b/test/src/test/java/com/github/cowwoc/requirements/test/TestValidatorsImpl.java @@ -4,9 +4,8 @@ import com.github.cowwoc.requirements.guava.MultimapValidator; import com.github.cowwoc.requirements.guava.internal.implementation.GuavaValidatorsImpl; import com.github.cowwoc.requirements.java.Configuration; +import com.github.cowwoc.requirements.java.ConfigurationUpdater; import com.github.cowwoc.requirements.java.GlobalConfiguration; -import com.github.cowwoc.requirements.java.ScopedContext; -import com.github.cowwoc.requirements.java.internal.implementation.AbstractValidators; import com.github.cowwoc.requirements.java.internal.implementation.JavaValidatorsImpl; import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.java.type.BigDecimalValidator; @@ -59,10 +58,10 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Optional; -public final class TestValidatorsImpl extends AbstractValidators - implements TestValidators +public final class TestValidatorsImpl implements TestValidators { private final JavaValidatorsImpl javaValidators; private final GuavaValidatorsImpl guavaValidators; @@ -75,15 +74,32 @@ public final class TestValidatorsImpl extends AbstractValidators */ public TestValidatorsImpl(ApplicationScope scope) { - super(scope, Configuration.DEFAULT); this.javaValidators = new JavaValidatorsImpl(scope, Configuration.DEFAULT); this.guavaValidators = new GuavaValidatorsImpl(scope, Configuration.DEFAULT); } - @Override - public void setConfiguration(Configuration configuration) + /** + * Creates a copy of an existing validator factory. + * + * @param other the factory to copy + * @throws NullPointerException if {@code other} is null + */ + public TestValidatorsImpl(TestValidatorsImpl other) + { + this(other.javaValidators.getScope()); + setConfiguration(other.configuration()); + for (Entry entry : other.getContext().entrySet()) + putContext(entry.getValue(), entry.getKey()); + } + + /** + * Set the configuration used by new validators. + * + * @param configuration the updated configuration + * @throws NullPointerException if {@code configuration} is null + */ + private void setConfiguration(Configuration configuration) { - super.setConfiguration(configuration); javaValidators.setConfiguration(configuration); guavaValidators.setConfiguration(configuration); } @@ -1290,15 +1306,48 @@ public > MultimapValidator checkIf(T val @Override @CheckReturnValue - public ScopedContext threadContext() + public GlobalConfiguration globalConfiguration() { - return javaValidators.threadContext(); + return javaValidators.globalConfiguration(); } @Override - @CheckReturnValue - public GlobalConfiguration globalConfiguration() + public Configuration configuration() { - return javaValidators.globalConfiguration(); + return javaValidators.configuration(); + } + + @Override + public ConfigurationUpdater updateConfiguration() + { + return javaValidators.updateConfiguration(this::setConfiguration); + } + + @Override + public TestValidatorsImpl copy() + { + return new TestValidatorsImpl(this); + } + + @Override + public Map getContext() + { + return javaValidators.getContext(); + } + + @Override + public TestValidatorsImpl putContext(Object value, String name) + { + javaValidators.putContext(value, name); + guavaValidators.putContext(value, name); + return this; + } + + @Override + public TestValidatorsImpl removeContext(String name) + { + javaValidators.removeContext(name); + guavaValidators.removeContext(name); + return this; } } \ No newline at end of file diff --git a/test/src/test/java/com/github/cowwoc/requirements/test/java/internal/impl/ConfigurationTest.java b/test/src/test/java/com/github/cowwoc/requirements/test/java/internal/impl/ConfigurationTest.java index 42d67b4c3..48eff0258 100644 --- a/test/src/test/java/com/github/cowwoc/requirements/test/java/internal/impl/ConfigurationTest.java +++ b/test/src/test/java/com/github/cowwoc/requirements/test/java/internal/impl/ConfigurationTest.java @@ -6,12 +6,12 @@ import com.github.cowwoc.requirements.java.Configuration; import com.github.cowwoc.requirements.java.ConfigurationUpdater; -import com.github.cowwoc.requirements.java.ScopedContext; import com.github.cowwoc.requirements.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements.test.TestValidatorsImpl; import com.github.cowwoc.requirements.test.scope.TestApplicationScope; import org.testng.annotations.Test; +import static com.github.cowwoc.requirements.java.DefaultJavaValidators.requireThat; import static com.github.cowwoc.requirements.java.terminal.TerminalEncoding.NONE; public final class ConfigurationTest @@ -68,36 +68,84 @@ public void inheritDefaultConfiguration() } @Test - public void threadConfiguration() + public void factorySeparateContext() { try (ApplicationScope scope = new TestApplicationScope(NONE)) { - TestValidatorsImpl validators = new TestValidatorsImpl(scope); - try (ScopedContext context = validators.threadContext()) - { - context.put("threadValue", "threadName"); - String message = validators.checkIf("value", "name"). - putContext("validatorValue", "validatorName").isNull().elseGetMessages().getFirst(); - validators.requireThat(message, "message").contains("validatorName: \"validatorValue\""). - contains("threadName : \"threadValue\""); - } + TestValidatorsImpl factory1 = new TestValidatorsImpl(scope); + + TestValidatorsImpl factory2 = factory1.copy(); + factory2.putContext("factoryValue", "factoryName"); + + String message1 = factory1.checkIf("value", "name"). + putContext("validatorValue", "validatorName").isNull().elseGetMessages().getFirst(); + String message2 = factory2.checkIf("value", "name"). + putContext("validatorValue", "validatorName").isNull().elseGetMessages().getFirst(); + + requireThat(message1, "message1").contains("validatorName: \"validatorValue\""). + doesNotContain("factoryName : \"factoryValue\""); + requireThat(message2, "message2").contains("validatorName: \"validatorValue\""). + contains("factoryName : \"factoryValue\""); } } @Test - public void validatorOverridesThreadContext() + public void factoryInheritedContext() { try (ApplicationScope scope = new TestApplicationScope(NONE)) { - TestValidatorsImpl validators = new TestValidatorsImpl(scope); - try (ScopedContext context = validators.threadContext()) - { - context.put("threadValue", "collision"); - String message = validators.checkIf("value", "name"). - putContext("validatorValue", "collision").isNull().elseGetMessages().getFirst(); - validators.requireThat(message, "message").contains("collision: \"validatorValue\""). - doesNotContain("collision: \"threadValue\""); - } + TestValidatorsImpl factory1 = new TestValidatorsImpl(scope); + factory1.putContext("factoryValue", "factoryName"); + + TestValidatorsImpl factory2 = factory1.copy(); + String message = factory2.checkIf("value", "name"). + putContext("validatorValue", "validatorName").isNull().elseGetMessages().getFirst(); + + requireThat(message, "message2").contains("validatorName: \"validatorValue\""). + contains("factoryName : \"factoryValue\""); + } + } + + /** + * Ensure that the validator context is separate from the factory context. + */ + @Test + public void validatorContextSeparateFromFactory() + { + try (ApplicationScope scope = new TestApplicationScope(NONE)) + { + TestValidatorsImpl factory = new TestValidatorsImpl(scope); + + String message = factory.checkIf("value", "name"). + putContext("validatorValue", "validatorName").isNull().elseGetMessages().getFirst(); + + // Ensure that this does not affect pre-existing validators + factory.putContext("factoryValue", "factoryName"); + + requireThat(message, "message2").contains("validatorName: \"validatorValue\""). + doesNotContain("factoryName : \"factoryValue\""); + } + } + + @Test + public void validatorOverridesFactoryContext() + { + try (ApplicationScope scope = new TestApplicationScope(NONE)) + { + TestValidatorsImpl factory1 = new TestValidatorsImpl(scope); + factory1.putContext("factoryValue", "collision"); + + TestValidatorsImpl factory2 = factory1.copy(); + + String message1 = factory1.checkIf("value", "name"). + putContext("validatorValue", "collision").isNull().elseGetMessages().getFirst(); + String message2 = factory2.checkIf("value", "name"). + putContext("validatorValue", "collision").isNull().elseGetMessages().getFirst(); + + requireThat(message1, "message1").contains("collision: \"validatorValue\""). + doesNotContain("collision: \"factoryValue\""); + requireThat(message2, "message2").contains("collision: \"validatorValue\""). + doesNotContain("collision: \"factoryValue\""); } } @@ -107,15 +155,13 @@ public void exceptionOverridesValidatorContext() try (ApplicationScope scope = new TestApplicationScope(NONE)) { TestValidatorsImpl validators = new TestValidatorsImpl(scope); - try (ScopedContext context = validators.threadContext()) - { - context.put("threadValue", "name2"); - String message = validators.checkIf("value", "name"). - putContext("validatorValue", "name2").isNull().elseGetMessages().getFirst(); - validators.requireThat(message, "message").contains("Actual: \"value\""). - doesNotContain("name2: \"validatorValue\""). - doesNotContain("name2: \"threadValue\""); - } + validators.putContext("factoryValue", "name2"); + + String message = validators.checkIf("value", "name"). + putContext("validatorValue", "name2").isNull().elseGetMessages().getFirst(); + validators.requireThat(message, "message").contains("Actual: \"value\""). + doesNotContain("name2: \"validatorValue\""). + doesNotContain("name2: \"factoryValue\""); } } } \ No newline at end of file