Skip to content

Commit f017260

Browse files
committed
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.
1 parent fc85601 commit f017260

File tree

8 files changed

+2
-115
lines changed

8 files changed

+2
-115
lines changed

docs/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ See https://github.com/cowwoc/requirements.java/commits/master for a full list.
2121
* Replaced `Validator.elseGetException()` with `Validator.getFailures().getException()`.
2222
* Renamed `Validator.isSameReferenceAs()` to `isReferenceEqualTo()`, `Validator.isNotSameReferenceAs()` to
2323
`isReferenceNotEqualTo()`.
24+
* Removed `CollectionValidator.containsSameNullity()` yet again due to poor usability.
2425
* New features:
2526
* Added `GenericTypeValidator.isPrimitive()`.
2627
* Added `validationFailed()` and `getValueOrDefault()` to all validators.

java/src/main/java/com/github/cowwoc/requirements10/java/internal/message/CollectionMessages.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,4 @@ public static MessageBuilder isSortedFailed(AbstractObjectValidator<?, ?> valida
415415
messageBuilder.withContext(sorted, "expected");
416416
return messageBuilder;
417417
}
418-
419-
/**
420-
* @param validator the validator
421-
* @return a message for the validation failure
422-
*/
423-
public static MessageBuilder containsSameNullityFailed(AbstractObjectValidator<?, ?> validator)
424-
{
425-
String name = validator.getName();
426-
MessageBuilder messageBuilder = new MessageBuilder(validator,
427-
quoteName(name) + " must contain all nulls, or no nulls.");
428-
validator.value.nullToInvalid().ifValid(v -> messageBuilder.withContext(v, name));
429-
return messageBuilder;
430-
}
431418
}

java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/AbstractCollectionValidator.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -399,35 +399,6 @@ public S doesNotContainDuplicates()
399399
return self();
400400
}
401401

402-
@Override
403-
public S containsSameNullity()
404-
{
405-
if (containsSameNullityValidationFailed())
406-
{
407-
failOnNull();
408-
addIllegalArgumentException(
409-
CollectionMessages.containsSameNullityFailed(this).toString());
410-
}
411-
return self();
412-
}
413-
414-
/**
415-
* @return {@code true} if the elements in the value are all {@code null} or all not {@code null}, or
416-
* {@code false} if the value is undefined, {@code null}, or contains a mix of {@code null} and not
417-
* {@code null} elements
418-
*/
419-
private boolean containsSameNullityValidationFailed()
420-
{
421-
T invalidToNull = getValueOrDefault(null);
422-
if (invalidToNull == null)
423-
return true;
424-
int numberOfNulls = 0;
425-
for (E element : invalidToNull)
426-
if (element == null)
427-
++numberOfNulls;
428-
return numberOfNulls != invalidToNull.size();
429-
}
430-
431402
@Override
432403
public PrimitiveUnsignedIntegerValidator size()
433404
{

java/src/main/java/com/github/cowwoc/requirements10/java/internal/validator/ObjectArrayValidatorImpl.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,4 @@ protected Set<E> getDuplicates(E[] value)
7070
}
7171
return duplicates;
7272
}
73-
74-
@Override
75-
public ObjectArrayValidator<E[], E> containsSameNullity()
76-
{
77-
if (value.validationFailed(v ->
78-
{
79-
int numberOfNulls = 0;
80-
for (E element : v)
81-
if (element == null)
82-
++numberOfNulls;
83-
return numberOfNulls == getLength(v);
84-
}))
85-
{
86-
addIllegalArgumentException(
87-
CollectionMessages.containsSameNullityFailed(this).toString());
88-
}
89-
return this;
90-
}
9173
}

java/src/main/java/com/github/cowwoc/requirements10/java/validator/ObjectArrayValidator.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,4 @@ public interface ObjectArrayValidator<T, E> extends
1919
ObjectComponent<ObjectArrayValidator<T, E>, T>,
2020
ArrayComponent<ObjectArrayValidator<T, E>, T, E>
2121
{
22-
/**
23-
* Ensures that the array contains only null values or only non-null values.
24-
*
25-
* @return this
26-
* @throws NullPointerException if the value is null
27-
* @throws IllegalArgumentException if the array contains a mix of null and non-null values
28-
*/
29-
ObjectArrayValidator<T, E> containsSameNullity();
3022
}

java/src/main/java/com/github/cowwoc/requirements10/java/validator/component/CollectionComponent.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,6 @@ public interface CollectionComponent<S, E>
493493
*/
494494
S doesNotContainAll(Collection<E> unwanted, String name);
495495

496-
/**
497-
* Ensures that the collection contains only null values, or only non-null values.
498-
*
499-
* @return this
500-
* @throws NullPointerException if the value is {@code null}
501-
* @throws IllegalArgumentException if the collection contains a mix of {@code null} and non-{@code null}
502-
* values
503-
*/
504-
S containsSameNullity();
505-
506496
/**
507497
* Ensures that the collection does not contain any duplicate elements.
508498
*

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
<dependency>
146146
<groupId>com.github.cowwoc.pouch</groupId>
147147
<artifactId>core</artifactId>
148-
<version>4.4</version>
148+
<version>4.6-SNAPSHOT</version>
149149
</dependency>
150150
<dependency>
151151
<groupId>org.twdata.maven</groupId>

test/src/test/java/com/github/cowwoc/requirements10/test/java/CollectionTest.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,40 +1180,4 @@ public void collectionToString()
11801180
new TestValidatorsImpl(scope).requireThat(actualMessages, "actualMessages").isEqualTo(expectedMessages);
11811181
}
11821182
}
1183-
1184-
public void doesNotContainMixedNullity_IsNull()
1185-
{
1186-
try (ApplicationScope scope = new TestApplicationScope(NONE))
1187-
{
1188-
TestValidators validators = new TestValidatorsImpl(scope);
1189-
Map<String, Object> nameToValue = new HashMap<>();
1190-
nameToValue.put("first", null);
1191-
nameToValue.put("second", null);
1192-
validators.requireThat(nameToValue, "nameToValue").values().containsSameNullity();
1193-
}
1194-
}
1195-
1196-
public void doesNotContainMixedNullity_IsNotNull()
1197-
{
1198-
try (ApplicationScope scope = new TestApplicationScope(NONE))
1199-
{
1200-
TestValidators validators = new TestValidatorsImpl(scope);
1201-
Map<String, Object> nameToValue = Map.of("first", 1,
1202-
"second", 10);
1203-
validators.requireThat(nameToValue, "nameToValue").values().containsSameNullity();
1204-
}
1205-
}
1206-
1207-
@Test(expectedExceptions = IllegalArgumentException.class)
1208-
public void doesNotContainMixedNullity_False()
1209-
{
1210-
try (ApplicationScope scope = new TestApplicationScope(NONE))
1211-
{
1212-
TestValidators validators = new TestValidatorsImpl(scope);
1213-
Map<String, Object> nameToValue = new HashMap<>();
1214-
nameToValue.put("first", 1);
1215-
nameToValue.put("second", null);
1216-
validators.requireThat(nameToValue, "nameToValue").values().containsSameNullity();
1217-
}
1218-
}
12191183
}

0 commit comments

Comments
 (0)