All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.codesseur.validation.MapVerifier Maven / Gradle / Ivy

There is a newer version: 0.0.5
Show newest version
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 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 matcher) {
    return noneMatch((k, v) -> matcher.test(k));
  }

  public MapVerifier noneValuesMatch(Predicate matcher) {
    return noneMatch((k, v) -> matcher.test(v));
  }

  public MapVerifier noneMatch(BiPredicate matcher) {
    return satisfies(c -> c.entrySet().stream().noneMatch(e -> matcher.test(e.getKey(), e.getValue())),
        () -> Failure.of("MATCH"));
  }

  public MapVerifier allKeysMatch(Predicate matcher) {
    return allMatch((k, v) -> matcher.test(k));
  }

  public MapVerifier allValuesMatch(Predicate matcher) {
    return allMatch((k, v) -> matcher.test(v));
  }

  public MapVerifier allMatch(BiPredicate matcher) {
    return satisfies(c -> c.entrySet().stream().allMatch(e -> matcher.test(e.getKey(), e.getValue())),
        () -> Failure.of("NOT_MATCH"));
  }

  public MapVerifier anyKeyMatches(Predicate matcher) {
    return anyMatches((k, v) -> matcher.test(k));
  }

  public MapVerifier anyValueMatches(Predicate matcher) {
    return anyMatches((k, v) -> matcher.test(v));
  }

  public MapVerifier anyMatches(BiPredicate 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 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 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 keys) {
    Set set = Streamed.of(keys).toSet();
    return noneKeysMatch(set::contains);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy