diff --git a/src/main/java/org/cactoos/text/SubText.java b/src/main/java/org/cactoos/text/SubText.java new file mode 100644 index 0000000000..27ea2736ba --- /dev/null +++ b/src/main/java/org/cactoos/text/SubText.java @@ -0,0 +1,131 @@ +/** + * 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.Scalar; +import org.cactoos.Text; +import org.cactoos.func.UncheckedScalar; + +/** + * Extract a substring from a Text. + * + *

There is no thread-safety guarantee. + * + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.11 + */ +public final class SubText implements Text { + + /** + * The text. + */ + private final Text origin; + + /** + * The start position in the text. + */ + private final UncheckedScalar start; + + /** + * The end position in the text. + */ + private final UncheckedScalar end; + + /** + * Ctor. + * @param text The String + * @param strt Start position in the text + */ + public SubText(final String text, final int strt) { + this(new StringAsText(text), strt); + } + + /** + * Ctor. + * @param text The String + * @param strt Start position in the text + * @param end End position in the text + */ + public SubText(final String text, final int strt, final int end) { + this(new StringAsText(text), strt, end); + } + + /** + * Ctor. + * @param text The Text + * @param strt Start position in the text + */ + public SubText(final Text text, final int strt) { + this(text, () -> strt, () -> text.asString().length()); + } + + /** + * Ctor. + * @param text The Text + * @param strt Start position in the text + * @param end End position in the text + */ + public SubText(final Text text, final int strt, final int end) { + this(text, () -> strt, () -> end); + } + + /** + * Ctor. + * @param text The Text + * @param strt Start position in the text + * @param end End position in the text + */ + public SubText(final Text text, final Scalar strt, + final Scalar end) { + this(text, new UncheckedScalar<>(strt), new UncheckedScalar<>(end)); + } + + /** + * Ctor. + * @param text The Text + * @param strt Start position in the text + * @param end End position in the text + */ + public SubText(final Text text, final UncheckedScalar strt, + final UncheckedScalar end) { + this.origin = text; + this.start = strt; + this.end = end; + } + + @Override + public String asString() throws IOException { + return this.origin.asString().substring( + this.start.value(), + this.end.value() + ); + } + + @Override + public int compareTo(final Text text) { + return new UncheckedText(this).compareTo(text); + } +} diff --git a/src/test/java/org/cactoos/text/SubTextTest.java b/src/test/java/org/cactoos/text/SubTextTest.java new file mode 100644 index 0000000000..a62c43941b --- /dev/null +++ b/src/test/java/org/cactoos/text/SubTextTest.java @@ -0,0 +1,58 @@ +/** + * 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 org.cactoos.TextHasString; +import org.hamcrest.MatcherAssert; +import org.junit.Test; + +/** + * Test case for {@link SubText}. + * @author Fabricio Cabral (fabriciofx@gmail.com) + * @version $Id$ + * @since 0.11 + * @checkstyle JavadocMethodCheck (500 lines) + */ +public final class SubTextTest { + + @Test + public void cutTextWithStartAndEnd() { + MatcherAssert.assertThat( + "Can't cut a text with start and end", + // @checkstyle MagicNumber (1 line) + new SubText("hello world", 2, 9), + new TextHasString("llo wor") + ); + } + + @Test + public void cutTextWithStart() { + MatcherAssert.assertThat( + "Can't cut a text with start", + new SubText("cut here", 2), + new TextHasString("t here") + ); + } + +}