com.codesseur.validation.MapVerifier Maven / Gradle / Ivy
package com.codesseur.validation;
import com.codesseur.iterate.Streamed;
import io.vavr.Lazy;
import io.vavr.Tuple;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
import org.immutables.value.Value;
@Value.Immutable
public abstract class MapVerifier> extends Verifier> {
public static > MapVerifier me(Lazy extends T> value) {
return ImmutableMapVerifier.builder().value(value).build();
}
public MapVerifier hasSize(int size) {
return isNotNull().then()
.satisfies(e -> e.size() == size, () -> Failure.of("SIZE_MISMATCH", Tuple.of("size", size)));
}
public MapVerifier isNotEmpty() {
return isNotNull().then().satisfies(es -> !es.isEmpty(), () -> Failure.of("EMPTY"));
}
public MapVerifier isEmpty() {
return isNotNull().then().satisfies(Map::isEmpty, () -> Failure.of("NOT_EMPTY"));
}
public MapVerifier noneKeysMatch(Predicate super K> matcher) {
return noneMatch((k, v) -> matcher.test(k));
}
public MapVerifier noneValuesMatch(Predicate super V> matcher) {
return noneMatch((k, v) -> matcher.test(v));
}
public MapVerifier noneMatch(BiPredicate super K, ? super V> matcher) {
return satisfies(c -> c.entrySet().stream().noneMatch(e -> matcher.test(e.getKey(), e.getValue())),
() -> Failure.of("MATCH"));
}
public MapVerifier allKeysMatch(Predicate super K> matcher) {
return allMatch((k, v) -> matcher.test(k));
}
public MapVerifier allValuesMatch(Predicate super V> matcher) {
return allMatch((k, v) -> matcher.test(v));
}
public MapVerifier allMatch(BiPredicate super K, ? super V> matcher) {
return satisfies(c -> c.entrySet().stream().allMatch(e -> matcher.test(e.getKey(), e.getValue())),
() -> Failure.of("NOT_MATCH"));
}
public MapVerifier anyKeyMatches(Predicate super K> matcher) {
return anyMatches((k, v) -> matcher.test(k));
}
public MapVerifier anyValueMatches(Predicate super V> matcher) {
return anyMatches((k, v) -> matcher.test(v));
}
public MapVerifier anyMatches(BiPredicate super K, ? super V> matcher) {
return satisfies(c -> c.entrySet().stream().anyMatch(e -> matcher.test(e.getKey(), e.getValue())),
() -> Failure.of("NOT_MATCH"));
}
public MapVerifier containsKey(K key) {
return satisfies(c -> c.containsKey(key), () -> Failure.of("NOT_CONTAIN", Tuple.of("key", key)));
}
public MapVerifier notContainsKey(K key) {
return satisfies(c -> !c.containsKey(key), () -> Failure.of("CONTAINS", Tuple.of("key", key)));
}
@SafeVarargs
public final MapVerifier containsAllKeys(K... keys) {
return containsAllKeys(Arrays.asList(keys));
}
public MapVerifier containsAllKeys(Iterable extends K> keys) {
Set set = Streamed.of(keys).toSet();
return allKeysMatch(set::contains);
}
@SafeVarargs
public final MapVerifier containsAnyKeys(K... keys) {
return containsAnyKeys(Arrays.asList(keys));
}
public MapVerifier containsAnyKeys(Iterable extends K> keys) {
Set set = Streamed.of(keys).toSet();
return anyKeyMatches(set::contains);
}
@SafeVarargs
public final MapVerifier notContainsAnyKeys(K... keys) {
return notContainsAnyKeys(Arrays.asList(keys));
}
public MapVerifier notContainsAnyKeys(Iterable extends K> keys) {
Set set = Streamed.of(keys).toSet();
return noneKeysMatch(set::contains);
}
}