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

it.ozimov.cirneco.hamcrest.base.IsEquivalent Maven / Gradle / Ivy

package it.ozimov.cirneco.hamcrest.base;

import com.google.common.base.Equivalence;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

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


/**
 * Is the value equivalent to another value as specified by the provided {@linkplain Equivalence}?
 *
 * @since version 0.1 for JDK7
 */
public class IsEquivalent extends TypeSafeMatcher {

    private final T comparison;

    private final Equivalence equivalence;

    /**
     * Creates an instance of the class. comparison can be null,
     * while equivalence cannot.
     */
    public IsEquivalent(final T comparison, final Equivalence equivalence) {
        checkNotNull(equivalence);

        this.comparison = comparison;
        this.equivalence = equivalence;
    }

    /**
     * Creates a matcher that matches when the examined object of type T
     * is equivalent to the specified comparison object according to
     * the provided {@linkplain Equivalence}.
     * 

*

Observe that the {@linkplain Equivalence} can deal with nulls.

*/ public static Matcher equivalentTo(final T expected, final Equivalence equivalence) { return new IsEquivalent<>(expected, equivalence); } @Override protected boolean matchesSafely(final T actual) { return equivalence.equivalent(actual, comparison); } @Override protected void describeMismatchSafely(final T item, final Description mismatchDescription) { mismatchDescription.appendValue(item) .appendText(" is not equivalent to "); if (comparison == null) { mismatchDescription.appendText("null value"); } else { mismatchDescription.appendValue(comparison); } mismatchDescription.appendText(" according with Equivalence ") .appendValue(equivalence); } @Override public void describeTo(final Description description) { description .appendText("a value equivalent to ") .appendValue(comparison) .appendText(" according with Equivalence ") .appendValue(equivalence); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy