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

it.ozimov.cirneco.hamcrest.map.IsMultimapKeyWithCollectionSize Maven / Gradle / Ivy

package it.ozimov.cirneco.hamcrest.map;

import com.google.common.collect.Multimap;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

import java.util.Collection;

import static com.google.common.base.Preconditions.checkArgument;


public class IsMultimapKeyWithCollectionSize extends TypeSafeMatcher> {

    private final K comparison;
    private final int size;

    public IsMultimapKeyWithCollectionSize(final K comparison, final int size) {
        checkArgument(size >= 0, "size cannot be negative");

        this.comparison = comparison;
        this.size = size;
    }

    /**
     * Creates a matcher for {@linkplain Multimap} matching when the examined object K in the key set
     * has size elements in the retained {@linkplain Collection}.
     */
    public static  Matcher> keyWithSize(final K element, final int size) {
        return new IsMultimapKeyWithCollectionSize(element, size);
    }

    @Override
    public boolean matchesSafely(final Multimap actual) {
        return actual.containsKey(comparison) && actual.get(comparison).size() == size;
    }

    @Override
    public void describeMismatchSafely(final Multimap multimap, final Description mismatchDescription) {
        if (multimap.containsKey(comparison)) {
            mismatchDescription
                    .appendText("Multimap was not containing element").appendValue(comparison);
        } else {
            mismatchDescription
                    .appendText("Multimap had element ")
                    .appendValue(comparison)
                    .appendText(" with collection size ")
                    .appendValue(multimap.get(comparison).size());
        }
    }

    @Override
    public void describeTo(final Description description) {
        description.appendText("a Multimap with element ")
                .appendValue(comparison)
                .appendText(" with collection size ")
                .appendValue(size);
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy