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..68836412 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 @@ -6,7 +6,6 @@ import com.github.cowwoc.requirements10.java.ValidationFailure; import com.github.cowwoc.requirements10.java.internal.Configuration; -import com.github.cowwoc.requirements10.java.internal.message.CollectionMessages; import com.github.cowwoc.requirements10.java.internal.scope.ApplicationScope; import com.github.cowwoc.requirements10.java.internal.util.Arrays; import com.github.cowwoc.requirements10.java.internal.util.ValidationTarget; @@ -70,22 +69,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