Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/am/ik/yavi/constraint/CollectionConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static am.ik.yavi.core.NullAs.VALID;
import static am.ik.yavi.core.ViolationMessage.Default.COLLECTION_CONTAINS;
import static am.ik.yavi.core.ViolationMessage.Default.COLLECTION_UNIQUE;
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_ALL_OF;

public class CollectionConstraint<T, L extends Collection<E>, E>
extends ContainerConstraintBase<T, L, CollectionConstraint<T, L, E>> {
Expand Down Expand Up @@ -69,6 +70,22 @@ public CollectionConstraint<T, L, E> unique() {
return this;
}

public CollectionConstraint<T, L, E> allOf(Collection<? extends E> values) {
Comment thread
making marked this conversation as resolved.
Outdated
this.predicates().add(ConstraintPredicate.withViolatedValue(collection -> {
final Set<E> missingValues = new HashSet<>(values);

if (collection instanceof Set)
Comment thread
making marked this conversation as resolved.
Outdated
missingValues.removeAll(collection);
else
missingValues.removeAll(new HashSet<>(collection));
Comment thread
making marked this conversation as resolved.

return missingValues.isEmpty() ? Optional.empty() : Optional.of(new ViolatedValue(missingValues));
}, OBJECT_ALL_OF, () -> new Object[]{values.toString()}, VALID));

return this;
}


@Override
protected ToIntFunction<L> size() {
return Collection::size;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/am/ik/yavi/core/ViolationMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ enum Default implements ViolationMessage {
OBJECT_EQUAL_TO("object.equalTo", "\"{0}\" must be equal to {1}"), //
OBJECT_ONE_OF("object.oneOf", "\"{0}\" must be one of the following values: {1}"), //
OBJECT_NOT_ONE_OF("object.notOneOf", "\"{0}\" must not be one of the following values: {1}"), //
OBJECT_ALL_OF("object.allOf", "\"{0}\" must be have of all of the following values: {1}, missing values: {2}"), //

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object. prefix is not correct in this case

CONTAINER_NOT_EMPTY("container.notEmpty", "\"{0}\" must not be empty"), //
CONTAINER_LESS_THAN("container.lessThan", "The size of \"{0}\" must be less than {1}. The given size is {2}"), //
CONTAINER_LESS_THAN_OR_EQUAL("container.lessThanOrEqual",
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/am/ik/yavi/constraint/CollectionConstraintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ void unique() {
assertThat(predicate.test(Arrays.asList("a", "b", "c", "b"))).isFalse();
}

@Test
void allOf() {
Predicate<List<String>> predicate = retrievePredicate(c -> c.allOf(Arrays.asList("a", "b", "c")));
assertThat(predicate.test(Arrays.asList("a", "b", "c", "d"))).isTrue();
assertThat(predicate.test(Arrays.asList("a", "b", "c"))).isTrue();
assertThat(predicate.test(Arrays.asList("a", "b"))).isFalse();
assertThat(predicate.test(Arrays.asList("a"))).isFalse();
assertThat(predicate.test(Arrays.asList("a", "b", "d"))).isFalse();
assertThat(predicate.test(Arrays.asList("x", "y"))).isFalse();
assertThat(predicate.test(Arrays.asList())).isFalse();
}

private static Predicate<List<String>> retrievePredicate(
Function<CollectionConstraint<List<String>, List<String>, String>, CollectionConstraint<List<String>, List<String>, String>> constraint) {
return constraint.apply(new CollectionConstraint<>()).predicates().peekFirst().predicate();
Expand Down