Skip to content

Add SplitPreserveAllTokens class for splitting text #1747

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/main/java/org/cactoos/text/SplitPreserveAllTokens.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2024 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.Text;
import org.cactoos.iterable.IterableEnvelope;
import org.cactoos.iterable.IterableOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.iterator.IteratorOf;

/**
* Split the Text using whitespace as the separator,
* preserving all tokens, including empty tokens created by adjacent separators.
*
* @see String#split(String)
* @see String#split(String, int)
* @since 0.9
*/
public final class SplitPreserveAllTokens extends IterableEnvelope<Text> {

/**
* Ctor.
*
* @param text The CharSequence to be split into tokens
*/
public SplitPreserveAllTokens(final CharSequence text) {
this(new TextOf(text));
}

/**
* Ctor.
*
* @param text The Text to be split into tokens
*/
public SplitPreserveAllTokens(final Text text) {
super(
new Mapped<>(
TextOf::new,
new IterableOf<>(
() -> new IteratorOf<>(text.asString().split(" ", -1))
)
)
);
}

}
128 changes: 128 additions & 0 deletions src/test/java/org/cactoos/text/SplitPreserveAllTokensTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2024 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.Text;
import org.cactoos.iterable.IterableOf;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.llorllale.cactoos.matchers.Assertion;

/**
* A test class for {@link SplitPreserveAllTokens}.
*
* @since 1.0
*/
@SuppressWarnings("PMD.TooManyMethods")
final class SplitPreserveAllTokensTest {

/**
* Test method for {@link SplitPreserveAllTokens#SplitPreserveAllTokens(Text)}.
* <p>
*/
@Test
void splitForEmptyString() {
final SplitPreserveAllTokens texts = new SplitPreserveAllTokens(new TextOf(""));
new Assertion<>(
"Must split empty string",
texts,
new IsEqual<>(new IterableOf<>(new TextOf("")))
).affirm();
}

/**
* Test method for {@link SplitPreserveAllTokens#SplitPreserveAllTokens(Text)}.
* <p>
* This test verifies the splitting behavior of
* {@link SplitPreserveAllTokens} class when the input string contains a single space.
* The expected result is an iterable containing the substrings "abc" and "def".
*/
@Test
void splitForOneSpace() {
final SplitPreserveAllTokens texts = new SplitPreserveAllTokens(new TextOf("abc def"));
new Assertion<>(
"Must split string with one space",
texts,
new IsEqual<>(new IterableOf<>(new TextOf("abc"), new TextOf("def")))
).affirm();
}

/**
* Test method for {@link SplitPreserveAllTokens#SplitPreserveAllTokens(Text)}.
* <p>
* This test verifies the splitting behavior of
* {@link SplitPreserveAllTokens} class when the input string contains two consecutive spaces.
* The expected result is an iterable containing the substrings "abc", empty string, and "def".
*/
@Test
void splitForTwoSpaces() {
final SplitPreserveAllTokens texts = new SplitPreserveAllTokens(new TextOf("abc def"));
new Assertion<>(
"Must split string with two spaces",
texts,
new IsEqual<>(
new IterableOf<>(
new TextOf("abc"), new TextOf(""), new TextOf("def")
))
).affirm();
}

/**
* Test method for {@link SplitPreserveAllTokens#SplitPreserveAllTokens(Text)}.
* <p>
* This test verifies the splitting behavior of {@link SplitPreserveAllTokens} class
* when the input string starts with a space.
*/
@Test
void splitForLeadingSpaces() {
final SplitPreserveAllTokens texts = new SplitPreserveAllTokens(new TextOf(" abc"));
new Assertion<>(
"Must split string with leading spaces",
texts,
new IsEqual<>(
new IterableOf<>(
new TextOf(""), new TextOf("abc")
))
).affirm();
}

/**
* Test method for {@link SplitPreserveAllTokens#SplitPreserveAllTokens(Text)}.
* <p>
* This test verifies the splitting behavior of {@link SplitPreserveAllTokens} class
* when the input string both starts and ends with a space.
*/
@Test
void splitForTrailingSpaces() {
final SplitPreserveAllTokens texts = new SplitPreserveAllTokens(new TextOf(" abc "));
new Assertion<>(
"Must split string with trailing spaces",
texts,
new IsEqual<>(
new IterableOf<>(
new TextOf(""), new TextOf("abc"), new TextOf("")
))
).affirm();
}
}
Loading