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

com.github.dakusui.crest.matcherbuilders.ListMatcherBuilder Maven / Gradle / Ivy

package com.github.dakusui.crest.matcherbuilders;

import com.github.dakusui.crest.core.InternalUtils;
import com.github.dakusui.crest.core.Printable;
import com.github.dakusui.crest.functions.TransformingPredicate;
import com.github.dakusui.faultsource.printable.Predicates;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class ListMatcherBuilder> extends ObjectMatcherBuilder, SELF> {
  protected ListMatcherBuilder(Function> function) {
    super(function);
  }

  public SELF containsAll(Collection collection) {
    return this.check(
        new TransformingPredicate, Collection>(
            String.format("containsAll%s", InternalUtils.summarize(collection)),
            Predicates.isEmpty(),
            Printable.function(
                String.format("notCovered%s", InternalUtils.summarize(collection)),
                objects -> collection.stream(
                ).filter(
                    each -> !objects.contains(each)
                ).collect(
                    toList()
                ))
        )
    );
  }

  public SELF containsOnly(Collection collection) {
    return this.check(
        new TransformingPredicate, Collection>(
            String.format("containsOnly%s", InternalUtils.summarize(collection)),
            Predicates.isEmpty(),
            Printable.function(
                String.format("extra%s", InternalUtils.summarize(collection)),
                objects -> objects.stream(
                ).filter(
                    each -> !collection.contains(each)
                ).collect(
                    toList()
                ))
        )
    );
  }

  public SELF containsExactly(Collection collection) {
    return this.check(
        new TransformingPredicate, Collection>(
            String.format("containsExactly%s", InternalUtils.summarize(collection)),
            Predicates.isEmpty(),
            Printable.function(
                String.format("difference%s", InternalUtils.summarize(collection)),
                objects -> Stream.concat(
                    objects.stream(), collection.stream()
                ).filter(
                    each -> !(collection.contains(each) && objects.contains(each))
                ).collect(
                    toList()
                ))
        )
    );
  }

  public SELF containsNone(Collection collection) {
    return this.check(
        new TransformingPredicate, Collection>(
            String.format("containsNone%s", InternalUtils.summarize(collection)),
            Predicates.isEmpty(),
            Printable.function(
                String.format("contained%s", InternalUtils.summarize(collection)),
                objects -> objects.stream(
                ).filter(
                    collection::contains
                ).collect(
                    toList()
                ))
        )
    );
  }

  public SELF contains(Object entry) {
    return this.check(Predicates.contains(entry));
  }

  public SELF isEmpty() {
    return this.check(Predicates.isEmpty());
  }

  public SELF allMatch(Predicate predicate) {
    return this.check(
        Printable.predicate(
            String.format("allMatch[%s]", predicate),
            entries -> entries.stream().allMatch(predicate)
        ));
  }

  public SELF anyMatch(Predicate predicate) {
    return this.check(
        Printable.predicate(
            String.format("anyMatch[%s]", predicate),
            entries -> entries.stream().anyMatch(predicate)
        ));
  }

  public SELF noneMatch(Predicate predicate) {
    return this.check(
        Printable.predicate(
            String.format("noneMatch[%s]", predicate),
            entries -> entries.stream().noneMatch(predicate)
        ));
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy