From f017260001f4df57d68703b619b637db7f5e91e0 Mon Sep 17 00:00:00 2001 From: Gili Tzabari Date: Mon, 16 Sep 2024 07:09:30 -0400 Subject: [PATCH] Removed containsSameNullity() due to poor usability. \-> Inputs cannot use List.of() because it forbids nulls. \-> Name cannot contain spaces. \-> Output does not treat each element as a separate context value. --- docs/Changelog.md | 1 + .../internal/message/CollectionMessages.java | 13 ------- .../AbstractCollectionValidator.java | 29 --------------- .../validator/ObjectArrayValidatorImpl.java | 18 ---------- .../java/validator/ObjectArrayValidator.java | 8 ----- .../component/CollectionComponent.java | 10 ------ pom.xml | 2 +- .../test/java/CollectionTest.java | 36 ------------------- 8 files changed, 2 insertions(+), 115 deletions(-) diff --git a/docs/Changelog.md b/docs/Changelog.md index a8bbd3b3..231f25c2 100644 --- a/docs/Changelog.md +++ b/docs/Changelog.md @@ -21,6 +21,7 @@ See https://github.com/cowwoc/requirements.java/commits/master for a full list. * Replaced `Validator.elseGetException()` with `Validator.getFailures().getException()`. * Renamed `Validator.isSameReferenceAs()` to `isReferenceEqualTo()`, `Validator.isNotSameReferenceAs()` to `isReferenceNotEqualTo()`. + * Removed `CollectionValidator.containsSameNullity()` yet again due to poor usability. * New features: * Added `GenericTypeValidator.isPrimitive()`. * Added `validationFailed()` and `getValueOrDefault()` to all validators. diff --git a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/CollectionMessages.java b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/CollectionMessages.java index dd23da00..1b75bc6a 100644 --- a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/CollectionMessages.java +++ b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/CollectionMessages.java @@ -415,17 +415,4 @@ public static MessageBuilder isSortedFailed(AbstractObjectValidator valida messageBuilder.withContext(sorted, "expected"); return messageBuilder; } - - /** - * @param validator the validator - * @return a message for the validation failure - */ - public static MessageBuilder containsSameNullityFailed(AbstractObjectValidator validator) - { - String name = validator.getName(); - MessageBuilder messageBuilder = new MessageBuilder(validator, - quoteName(name) + " must contain all nulls, or no nulls."); - validator.value.nullToInvalid().ifValid(v -> messageBuilder.withContext(v, name)); - return messageBuilder; - } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/AbstractCollectionValidator.java b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/AbstractCollectionValidator.java index f1866fe2..0dbed393 100644 --- a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/AbstractCollectionValidator.java +++ b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/AbstractCollectionValidator.java @@ -399,35 +399,6 @@ public S doesNotContainDuplicates() return self(); } - @Override - public S containsSameNullity() - { - if (containsSameNullityValidationFailed()) - { - failOnNull(); - addIllegalArgumentException( - CollectionMessages.containsSameNullityFailed(this).toString()); - } - return self(); - } - - /** - * @return {@code true} if the elements in the value are all {@code null} or all not {@code null}, or - * {@code false} if the value is undefined, {@code null}, or contains a mix of {@code null} and not - * {@code null} elements - */ - private boolean containsSameNullityValidationFailed() - { - T invalidToNull = getValueOrDefault(null); - if (invalidToNull == null) - return true; - int numberOfNulls = 0; - for (E element : invalidToNull) - if (element == null) - ++numberOfNulls; - return numberOfNulls != invalidToNull.size(); - } - @Override public PrimitiveUnsignedIntegerValidator size() { diff --git a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/ObjectArrayValidatorImpl.java b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/ObjectArrayValidatorImpl.java index b891adc3..c5b5db7c 100644 --- a/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/ObjectArrayValidatorImpl.java +++ b/java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/ObjectArrayValidatorImpl.java @@ -70,22 +70,4 @@ protected Set getDuplicates(E[] value) } return duplicates; } - - @Override - public ObjectArrayValidator containsSameNullity() - { - if (value.validationFailed(v -> - { - int numberOfNulls = 0; - for (E element : v) - if (element == null) - ++numberOfNulls; - return numberOfNulls == getLength(v); - })) - { - addIllegalArgumentException( - CollectionMessages.containsSameNullityFailed(this).toString()); - } - return this; - } } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements10/java/validator/ObjectArrayValidator.java b/java/src/main/java/com/github/cowwoc/requirements10/java/validator/ObjectArrayValidator.java index 2d8f2996..d143504d 100644 --- a/java/src/main/java/com/github/cowwoc/requirements10/java/validator/ObjectArrayValidator.java +++ b/java/src/main/java/com/github/cowwoc/requirements10/java/validator/ObjectArrayValidator.java @@ -19,12 +19,4 @@ public interface ObjectArrayValidator extends ObjectComponent, T>, ArrayComponent, T, E> { - /** - * Ensures that the array contains only null values or only non-null values. - * - * @return this - * @throws NullPointerException if the value is null - * @throws IllegalArgumentException if the array contains a mix of null and non-null values - */ - ObjectArrayValidator containsSameNullity(); } \ No newline at end of file diff --git a/java/src/main/java/com/github/cowwoc/requirements10/java/validator/component/CollectionComponent.java b/java/src/main/java/com/github/cowwoc/requirements10/java/validator/component/CollectionComponent.java index 2991ab8c..6ea15479 100644 --- a/java/src/main/java/com/github/cowwoc/requirements10/java/validator/component/CollectionComponent.java +++ b/java/src/main/java/com/github/cowwoc/requirements10/java/validator/component/CollectionComponent.java @@ -493,16 +493,6 @@ public interface CollectionComponent */ S doesNotContainAll(Collection unwanted, String name); - /** - * Ensures that the collection contains only null values, or only non-null values. - * - * @return this - * @throws NullPointerException if the value is {@code null} - * @throws IllegalArgumentException if the collection contains a mix of {@code null} and non-{@code null} - * values - */ - S containsSameNullity(); - /** * Ensures that the collection does not contain any duplicate elements. * diff --git a/pom.xml b/pom.xml index c4dc1634..30923a89 100644 --- a/pom.xml +++ b/pom.xml @@ -145,7 +145,7 @@ com.github.cowwoc.pouch core - 4.4 + 4.6-SNAPSHOT org.twdata.maven diff --git a/test/src/test/java/com/github/cowwoc/requirements10/test/java/CollectionTest.java b/test/src/test/java/com/github/cowwoc/requirements10/test/java/CollectionTest.java index 39b02369..9e443fff 100644 --- a/test/src/test/java/com/github/cowwoc/requirements10/test/java/CollectionTest.java +++ b/test/src/test/java/com/github/cowwoc/requirements10/test/java/CollectionTest.java @@ -1180,40 +1180,4 @@ public void collectionToString() new TestValidatorsImpl(scope).requireThat(actualMessages, "actualMessages").isEqualTo(expectedMessages); } } - - public void doesNotContainMixedNullity_IsNull() - { - try (ApplicationScope scope = new TestApplicationScope(NONE)) - { - TestValidators validators = new TestValidatorsImpl(scope); - Map nameToValue = new HashMap<>(); - nameToValue.put("first", null); - nameToValue.put("second", null); - validators.requireThat(nameToValue, "nameToValue").values().containsSameNullity(); - } - } - - public void doesNotContainMixedNullity_IsNotNull() - { - try (ApplicationScope scope = new TestApplicationScope(NONE)) - { - TestValidators validators = new TestValidatorsImpl(scope); - Map nameToValue = Map.of("first", 1, - "second", 10); - validators.requireThat(nameToValue, "nameToValue").values().containsSameNullity(); - } - } - - @Test(expectedExceptions = IllegalArgumentException.class) - public void doesNotContainMixedNullity_False() - { - try (ApplicationScope scope = new TestApplicationScope(NONE)) - { - TestValidators validators = new TestValidatorsImpl(scope); - Map nameToValue = new HashMap<>(); - nameToValue.put("first", 1); - nameToValue.put("second", null); - validators.requireThat(nameToValue, "nameToValue").values().containsSameNullity(); - } - } } \ No newline at end of file