diff --git a/src/main/java/org/cactoos/Text.java b/src/main/java/org/cactoos/Text.java index bf99564df2..b134281931 100644 --- a/src/main/java/org/cactoos/Text.java +++ b/src/main/java/org/cactoos/Text.java @@ -24,6 +24,7 @@ package org.cactoos; import java.io.IOException; +import org.cactoos.text.UncheckedText; /** * Text. @@ -44,4 +45,47 @@ public interface Text extends Comparable { */ String asString() throws IOException; + /** + * Text check for no nulls. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.11 + */ + final class NoNulls implements Text { + /** + * The origin text. + */ + private final Text origin; + /** + * Ctor. + * @param text The text + */ + public NoNulls(final Text text) { + this.origin = text; + } + @Override + public String asString() throws IOException { + if (this.origin == null) { + throw new IOException("NULL instead of a valid text"); + } + final String string = this.origin.asString(); + if (string == null) { + throw new IOException("NULL instead of a valid result string"); + } + return string; + } + @Override + public int compareTo(final Text text) { + if (text == null) { + throw new IllegalArgumentException( + "NULL parameter instead of a valid text" + ); + } + return new UncheckedText(this).compareTo(text); + } + } + } diff --git a/src/main/java/org/cactoos/text/NotNullText.java b/src/main/java/org/cactoos/text/NotNullText.java deleted file mode 100644 index 5633d5ca49..0000000000 --- a/src/main/java/org/cactoos/text/NotNullText.java +++ /dev/null @@ -1,66 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.text; - -import java.io.IOException; -import org.cactoos.Text; - -/** - * A safe Text. - * - *

There is no thread-safety guarantee. - * - * @author Fabricio Cabral (fabriciofx@gmail.com) - * @version $Id$ - * @since 0.3 - */ -public final class NotNullText implements Text { - - /** - * The text. - */ - private final Text origin; - - /** - * Ctor. - * @param text The text - */ - public NotNullText(final Text text) { - this.origin = text; - } - - @Override - public String asString() throws IOException { - if (this.origin == null) { - throw new IOException("invalid text (null)"); - } - return this.origin.asString(); - } - - @Override - public int compareTo(final Text text) { - return new UncheckedText(this).compareTo(text); - } - -} diff --git a/src/test/java/org/cactoos/text/NotNullTextTest.java b/src/test/java/org/cactoos/TextTest.java similarity index 72% rename from src/test/java/org/cactoos/text/NotNullTextTest.java rename to src/test/java/org/cactoos/TextTest.java index 76fca6fa16..a1887377de 100644 --- a/src/test/java/org/cactoos/text/NotNullTextTest.java +++ b/src/test/java/org/cactoos/TextTest.java @@ -21,25 +21,41 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package org.cactoos.text; +package org.cactoos; import java.io.IOException; -import org.cactoos.TextHasString; +import org.cactoos.text.StringAsText; import org.hamcrest.MatcherAssert; import org.junit.Test; /** - * Test case for {@link NotNullText}. + * Test case for {@link Text}. * @author Fabricio Cabral (fabriciofx@gmail.com) * @version $Id$ - * @since 0.3 + * @since 0.11 * @checkstyle JavadocMethodCheck (500 lines) */ -public final class NotNullTextTest { +public final class TextTest { @Test(expected = IOException.class) public void failForNullText() throws IOException { - new NotNullText(null).asString(); + new Text.NoNulls(null).asString(); + } + + @Test(expected = IOException.class) + public void failForReturnNullString() throws IOException { + new Text.NoNulls( + new Text() { + @Override + public String asString() throws IOException { + return null; + } + @Override + public int compareTo(final Text text) { + return 0; + } + } + ).asString(); } @Test @@ -47,7 +63,7 @@ public void checkForNullText() throws Exception { final String message = "Hello"; MatcherAssert.assertThat( "Can't work with null text", - new NotNullText( + new Text.NoNulls( new StringAsText(message) ), new TextHasString(message)