    001    package org.hamcrest.core;
    002
    003    import org.hamcrest.Description;
    004    import org.hamcrest.TypeSafeMatcher;
    005
    006    public abstract class SubstringMatcher extends TypeSafeMatcher<String> {
    007
    008        // TODO: Replace String with CharSequence to allow for easy interopability between
    009        //       String, StringBuffer, StringBuilder, CharBuffer, etc (joe).
    010
    011        protected final String substring;
    012
    013        protected SubstringMatcher(final String substring) {
    014            this.substring = substring;
    015        }
    016
    017        @Override
    018        public boolean matchesSafely(String item) {
    019            return evalSubstringOf(item);
    020        }
    021        @Override
    022        public void describeMismatchSafely(String item, Description mismatchDescription) {
    023          mismatchDescription.appendText("was \"").appendText(item).appendText("\"");
    024        }
    025
    026        @Override
    027        public void describeTo(Description description) {
    028            description.appendText("a string ")
    029                    .appendText(relationship())
    030                    .appendText(" ")
    031                    .appendValue(substring);
    032        }
    033
    034        protected abstract boolean evalSubstringOf(String string);
    035
    036        protected abstract String relationship();
    037    }