1 2package org.hamcrest.text; 3 4import org.hamcrest.Description; 5import org.hamcrest.Matcher; 6import org.hamcrest.TypeSafeMatcher; 7 8import static org.hamcrest.core.AnyOf.anyOf; 9import static org.hamcrest.core.IsNull.nullValue; 10 11/** 12 * Matches empty Strings (and null). 13 */ 14public final class IsEmptyString extends TypeSafeMatcher<String> { 15 private static final IsEmptyString INSTANCE = new IsEmptyString(); 16 @SuppressWarnings("unchecked") 17 private static final Matcher<String> NULL_OR_EMPTY_INSTANCE = anyOf(nullValue(), INSTANCE); 18 19 private IsEmptyString() { } 20 21 @Override 22 public boolean matchesSafely(String item) { 23 return item.equals(""); 24 } 25 26 @Override 27 public void describeTo(Description description) { 28 description.appendText("an empty string"); 29 } 30 31 /** 32 * Creates a matcher of {@link String} that matches when the examined string has zero length. 33 * For example: 34 * <pre>assertThat("", isEmptyString())</pre> 35 * 36 * @deprecated use is(emptyString()) instead 37 */ 38 @Deprecated 39 public static Matcher<String> isEmptyString() { 40 return emptyString(); 41 } 42 43 /** 44 * Creates a matcher of {@link String} that matches when the examined string has zero length. 45 * For example: 46 * <pre>assertThat("", is(emptyString()))</pre> 47 * 48 */ 49 public static Matcher<String> emptyString() { 50 return INSTANCE; 51 } 52 53 /** 54 * Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or 55 * has zero length. 56 * For example: 57 * <pre>assertThat(((String)null), isEmptyOrNullString())</pre> 58 * 59 * @deprecated use is(emptyOrNullString()) instead 60 * 61 */ 62 @Deprecated 63 public static Matcher<String> isEmptyOrNullString() { 64 return emptyOrNullString(); 65 } 66 67 /** 68 * Creates a matcher of {@link String} that matches when the examined string is <code>null</code>, or 69 * has zero length. 70 * For example: 71 * <pre>assertThat(((String)null), is(emptyOrNullString()))</pre> 72 * 73 */ 74 public static Matcher<String> emptyOrNullString() { 75 return NULL_OR_EMPTY_INSTANCE; 76 } 77} 78