Skip to content

Commit

Permalink
Removed containsSameNullity() due to poor usability.
Browse files Browse the repository at this point in the history
\-> Inputs cannot use List.of() because it forbids nulls.
\-> Name cannot contain spaces.
\-> Output does not treat each element as a separate context value.
  • Loading branch information
cowwoc committed Sep 16, 2024
1 parent fc85601 commit d75cda7
Show file tree
Hide file tree
Showing 8 changed files with 2 additions and 116 deletions.
1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,22 +69,4 @@ protected Set<E> getDuplicates(E[] value)
}
return duplicates;
}

@Override
public ObjectArrayValidator<E[], E> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,4 @@ public interface ObjectArrayValidator<T, E> extends
ObjectComponent<ObjectArrayValidator<T, E>, T>,
ArrayComponent<ObjectArrayValidator<T, E>, 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<T, E> containsSameNullity();
}
Original file line number Diff line number Diff line change
Expand Up @@ -493,16 +493,6 @@ public interface CollectionComponent<S, E>
*/
S doesNotContainAll(Collection<E> 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.
*
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<dependency>
<groupId>com.github.cowwoc.pouch</groupId>
<artifactId>core</artifactId>
<version>4.4</version>
<version>4.6-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.twdata.maven</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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<String, Object> 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<String, Object> nameToValue = new HashMap<>();
nameToValue.put("first", 1);
nameToValue.put("second", null);
validators.requireThat(nameToValue, "nameToValue").values().containsSameNullity();
}
}
}

0 comments on commit d75cda7

Please sign in to comment.