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

org.hamcrest.text.IsEmptyString Maven / Gradle / Ivy

There is a newer version: 3.0
Show newest version

package org.hamcrest.text;

import static org.hamcrest.core.AnyOf.anyOf;
import static org.hamcrest.core.IsNull.nullValue;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;

/**
 * Matches empty Strings (and null).
 */
public final class IsEmptyString extends BaseMatcher {
    private static final IsEmptyString INSTANCE = new IsEmptyString();
    @SuppressWarnings("unchecked")
    private static final Matcher NULL_OR_EMPTY_INSTANCE = anyOf(nullValue(), INSTANCE);

    @Override
    public boolean matches(Object item) {
        return item != null && item instanceof String && ((String) item).equals("");
    }

    @Override
    public void describeTo(Description description) {
        description.appendText("an empty string");
    }

    /**
     * Creates a matcher of {@link String} that matches when the examined string has zero length.
     * 

* For example: *

assertThat("", isEmptyString())
* */ @Factory public static Matcher isEmptyString() { return INSTANCE; } /** * Creates a matcher of {@link String} that matches when the examined string is null, or * has zero length. *

* For example: *

assertThat(((String)null), isEmptyString())
* */ @Factory public static Matcher isEmptyOrNullString() { return NULL_OR_EMPTY_INSTANCE; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy