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

com.annimon.stream.test.hamcrest.OptionalIntMatcher Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package com.annimon.stream.test.hamcrest;

import com.annimon.stream.OptionalInt;
import static org.hamcrest.CoreMatchers.is;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class OptionalIntMatcher {

    private OptionalIntMatcher() { }

    public static Matcher isPresent() {
        return new IsPresentMatcher();
    }

    public static Matcher isEmpty() {
        return new IsEmptyMatcher();
    }

    public static  Matcher hasValue(int value) {
        return hasValueThat(is(value));
    }

    public static Matcher hasValueThat(Matcher matcher) {
        return new HasValueMatcher(matcher);
    }

    public static class IsPresentMatcher extends TypeSafeDiagnosingMatcher {

        @Override
        protected boolean matchesSafely(OptionalInt optional, Description mismatchDescription) {
            mismatchDescription.appendText("OptionalInt was empty");
            return optional.isPresent();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("OptionalInt value should be present");
        }
    }

    public static class IsEmptyMatcher extends TypeSafeDiagnosingMatcher {

        @Override
        protected boolean matchesSafely(OptionalInt optional, Description mismatchDescription) {
            mismatchDescription.appendText("OptionalInt was present");
            return !optional.isPresent();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("OptionalInt value should be empty");
        }
    }

    public static class HasValueMatcher extends TypeSafeDiagnosingMatcher {

        private final Matcher matcher;

        public HasValueMatcher(Matcher matcher) {
            this.matcher = matcher;
        }

        @Override
        protected boolean matchesSafely(OptionalInt optional, Description mismatchDescription) {
            if (!optional.isPresent()) {
                mismatchDescription.appendText("OptionalInt was empty");
                return false;
            }
            final Integer value = optional.getAsInt();
            mismatchDescription.appendText("OptionalInt value ");
            matcher.describeMismatch(value, mismatchDescription);
            return matcher.matches(value);
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("OptionalInt value ").appendDescriptionOf(matcher);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy