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

it.ozimov.cirneco.hamcrest.map.IsMultimapWithKeySet 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.Set;

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


public class IsMultimapWithKeySet extends TypeSafeMatcher> {

    private final Set comparisonSet;

    public IsMultimapWithKeySet(final Set comparisonSet) {
        checkNotNull(comparisonSet);

        this.comparisonSet = comparisonSet;
    }

    /**
     * Creates a matcher for {@linkplain  Multimap} matching when the examined object has exactly
     * the same key set of the given comparison {@code Multimap}.
     */
    public static  Matcher> hasSameKeySet(final Multimap comparison) {
        return hasSameKeySet(comparison.keySet());
    }

    /**
     * Creates a matcher for {@linkplain Multimap} matching when the examined object has a key set exactly
     * equals to the given {@linkplain Set}.
     */
    public static  Matcher> hasSameKeySet(final Set comparison) {
        return new IsMultimapWithKeySet(comparison);
    }

    @Override
    public boolean matchesSafely(final Multimap actual) {
        return actual.keySet().equals(comparisonSet);
    }

    @Override
    public void describeMismatchSafely(final Multimap multimap, final Description mismatchDescription) {
        mismatchDescription.appendText("Multimap key set was ")
                .appendValueList("[", ", ", "]", multimap.keySet())
                .appendText(" while expected key set was ")
                .appendValueList("[", ", ", "]", comparisonSet);
    }

    @Override
    public void describeTo(final Description description) {
        description.appendText("a Multimap containing same key set as ")
                .appendValueList("[", ", ", "]", comparisonSet);
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy