From 88f7b8649fe0cc47b71c1896d353760eecf439ca Mon Sep 17 00:00:00 2001 From: tjann Date: Fri, 1 Nov 2024 11:24:41 +0300 Subject: [PATCH 1/7] Adding an OOP analogue for StringUtils.splitPreserveAllTokens --- .../org/cactoos/func/BiFuncSplitPreserve.java | 25 +++ .../cactoos/text/SplitPreserveAllTokens.java | 181 ++++++++++++++++++ .../org/cactoos/text/SplitPreserveTest.java | 90 +++++++++ 3 files changed, 296 insertions(+) create mode 100644 src/main/java/org/cactoos/func/BiFuncSplitPreserve.java create mode 100644 src/main/java/org/cactoos/text/SplitPreserveAllTokens.java create mode 100644 src/test/java/org/cactoos/text/SplitPreserveTest.java diff --git a/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java b/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java new file mode 100644 index 000000000..cef50270b --- /dev/null +++ b/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java @@ -0,0 +1,25 @@ +package org.cactoos.func; + +import org.cactoos.BiFunc; +import org.cactoos.list.ListOf; + +import java.util.Collection; + +public class BiFuncSplitPreserve implements BiFunc> { + @Override + public Collection apply(String str, String regex) throws Exception { + ListOf ret = new ListOf<>(); + int start = 0; + int pos = str.indexOf(regex); + while (pos >= start) { + ret.add(str.substring(start, pos)); + start = pos + regex.length(); + pos = str.indexOf(regex, start); + } + if (start < str.length()) + ret.add(str.substring(start)); + else if (start == str.length()) + ret.add(""); + return ret; + } +} diff --git a/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java new file mode 100644 index 000000000..bd60eeddb --- /dev/null +++ b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java @@ -0,0 +1,181 @@ +/* + * 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.func.BiFuncSplitPreserve; +import org.cactoos.iterable.IterableEnvelope; +import org.cactoos.iterable.IterableOf; +import org.cactoos.iterable.Mapped; +import org.cactoos.iterator.IteratorOf; + +/** + * Splits the Text into an array, 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 { + /** + * Ctor. + * + * @param text The text + * @see String#split(String) + */ + public SplitPreserveAllTokens(final CharSequence text) { + this(new TextOf(text), new TextOf(" ")); + } + + /** + * Ctor. + * + * @param text The text + * @see String#split(String) + */ + public SplitPreserveAllTokens(final Text text) { + this(text, new TextOf(" ")); + } + /** + * Ctor. + * + * @param text The text + * @param lmt The limit + * @see String#split(String, int) + */ + public SplitPreserveAllTokens(final CharSequence text, final int lmt) { + this(new TextOf(text), new TextOf(" "), lmt); + } + + /** + * Ctor. + * + * @param text The text + * @param lmt The limit + * @see String#split(String, int) + */ + public SplitPreserveAllTokens(final Text text, final int lmt) { + this(text, new TextOf(" "), lmt); + } + /** + * Ctor. + * + * @param text The text + * @param rgx The regex + * @see String#split(String) + */ + public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx) { + this(new TextOf(text), new TextOf(rgx)); + } + + /** + * Ctor. + * + * @param text The text + * @param rgx The regex + * @param lmt The limit + * @see String#split(String, int) + */ + public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx, final int lmt) { + this(new TextOf(text), new TextOf(rgx), lmt); + } + + /** + * Ctor. + * @param text The text + * @param rgx The regex + * @see String#split(String) + */ + public SplitPreserveAllTokens(final CharSequence text, final Text rgx) { + this(new TextOf(text), rgx); + } + + /** + * Ctor. + * @param text The text + * @param rgx The regex + * @param lmt The limit + * @see String#split(String, int) + */ + public SplitPreserveAllTokens(final CharSequence text, final Text rgx, final int lmt) { + this(new TextOf(text), rgx, lmt); + } + + /** + * Ctor. + * @param text The text + * @param rgx The regex + * @see String#split(String) + */ + public SplitPreserveAllTokens(final Text text, final CharSequence rgx) { + this(text, new TextOf(rgx)); + } + + /** + * Ctor. + * @param text The text + * @param rgx The regex + * @param lmt The limit + * @see String#split(String, int) + */ + public SplitPreserveAllTokens(final Text text, final CharSequence rgx, final int lmt) { + this(text, new TextOf(rgx), lmt); + } + + /** + * Ctor. + * @param text The text + * @param rgx The regex + * @see String#split(String) + */ + public SplitPreserveAllTokens(final Text text, final Text rgx) { + this(text, rgx, 0); + } + + /** + * Ctor. + * @param text The text + * @param rgx The regex + * @param lmt The limit + * @see String#split(String, int) + */ + public SplitPreserveAllTokens( + final Text text, final Text rgx, final int lmt + ) { + super( + new Mapped<>( + TextOf::new, + new IterableOf<>( + () -> new IteratorOf<>( + new BiFuncSplitPreserve() + .apply(text.toString(), + rgx.toString()) + .toArray(new String[0])) + ) + ) + ); + } + +} diff --git a/src/test/java/org/cactoos/text/SplitPreserveTest.java b/src/test/java/org/cactoos/text/SplitPreserveTest.java new file mode 100644 index 000000000..77c872c9f --- /dev/null +++ b/src/test/java/org/cactoos/text/SplitPreserveTest.java @@ -0,0 +1,90 @@ +package org.cactoos.text; + +import org.cactoos.Text; +import org.cactoos.iterable.IterableOf; +import org.hamcrest.Matchers; +import org.hamcrest.core.IsNot; +import org.junit.jupiter.api.Test; +import org.llorllale.cactoos.matchers.Assertion; + +import java.util.ArrayList; +import java.util.Iterator; + +public class SplitPreserveTest { + @Test + void checkingSplit() throws Exception { + String txt = " "; + ArrayList array = new ArrayList<>(); + array.add(new TextOf("")); + array.add(new TextOf("")); + array.add(new TextOf("")); + array.add(new TextOf("")); + new Assertion<>( + "Adjacent separators must create an empty element", + getLength(new Split(new TextOf(txt), new TextOf(" ")).iterator()), + IsNot.not(Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator()))) + ).affirm(); + + txt = " how "; + array = new ArrayList<>(); + array.add(new TextOf("")); + array.add(new TextOf("how")); + array.add(new TextOf("")); + + new Assertion<>( + "Adjacent separators must create an empty element", + getLength(new Split(new TextOf(txt), new TextOf(" ")).iterator()), + IsNot.not(Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator()))) + ).affirm(); + } + + int getLength(Iterator iter) { + int count = 0; + while (iter.hasNext()) { + iter.next(); + count++; + } + return count; + } + + @Test + void checkingSplitPreserveTokens() throws Exception { + String txt = " "; + ArrayList array = new ArrayList<>(); + array.add(new TextOf("")); + array.add(new TextOf("")); + array.add(new TextOf("")); + array.add(new TextOf("")); + + new Assertion<>( + "Adjacent separators must create an empty element", + getLength(new SplitPreserveAllTokens(new TextOf(txt), new TextOf(" ")).iterator()), + Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator())) + ).affirm(); + + txt = "lol\\ / dude"; + array = new ArrayList<>(); + array.add(new TextOf("lol\\")); + array.add(new TextOf("")); + array.add(new TextOf("/")); + array.add(new TextOf("dude")); + + new Assertion<>( + "Adjacent separators must create an empty element", + getLength(new SplitPreserveAllTokens(new TextOf(txt), new TextOf(" ")).iterator()), + Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator())) + ).affirm(); + + txt = " how "; + array = new ArrayList<>(); + array.add(new TextOf("")); + array.add(new TextOf("how")); + array.add(new TextOf("")); + + new Assertion<>( + "Adjacent separators must create an empty element", + getLength(new SplitPreserveAllTokens(new TextOf(txt), new TextOf(" ")).iterator()), + Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator())) + ).affirm(); + } +} \ No newline at end of file From 05879a27ea546038874351f9ed7b6db097d2812d Mon Sep 17 00:00:00 2001 From: tjann Date: Fri, 1 Nov 2024 11:37:43 +0300 Subject: [PATCH 2/7] Adding an OOP analogue for StringUtils.splitPreserveAllTokens --- .../org/cactoos/func/BiFuncSplitPreserve.java | 24 +++++++++++++++++++ .../org/cactoos/text/SplitPreserveTest.java | 24 +++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java b/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java index cef50270b..c79ef2270 100644 --- a/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java +++ b/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java @@ -1,3 +1,27 @@ +/* + * 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.func; import org.cactoos.BiFunc; diff --git a/src/test/java/org/cactoos/text/SplitPreserveTest.java b/src/test/java/org/cactoos/text/SplitPreserveTest.java index 77c872c9f..45522de1c 100644 --- a/src/test/java/org/cactoos/text/SplitPreserveTest.java +++ b/src/test/java/org/cactoos/text/SplitPreserveTest.java @@ -1,3 +1,27 @@ +/* + * 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; From 148bbba62a699eec32d2a67623d6426b45dfa97f Mon Sep 17 00:00:00 2001 From: tjann Date: Fri, 1 Nov 2024 11:51:38 +0300 Subject: [PATCH 3/7] Adding an OOP analogue for StringUtils.splitPreserveAllTokens --- .../java/org/cactoos/func/BiFuncSplitPreserve.java | 2 +- .../org/cactoos/text/SplitPreserveAllTokens.java | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java b/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java index c79ef2270..400997351 100644 --- a/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java +++ b/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java @@ -31,7 +31,7 @@ public class BiFuncSplitPreserve implements BiFunc> { @Override - public Collection apply(String str, String regex) throws Exception { + public Collection apply(final String str, final String regex) throws Exception { ListOf ret = new ListOf<>(); int start = 0; int pos = str.indexOf(regex); diff --git a/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java index bd60eeddb..2a332c6ec 100644 --- a/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java +++ b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java @@ -45,6 +45,7 @@ public final class SplitPreserveAllTokens extends IterableEnvelope { * @param text The text * @see String#split(String) */ + public SplitPreserveAllTokens(final CharSequence text) { this(new TextOf(text), new TextOf(" ")); } @@ -58,6 +59,7 @@ public SplitPreserveAllTokens(final CharSequence text) { public SplitPreserveAllTokens(final Text text) { this(text, new TextOf(" ")); } + /** * Ctor. * @@ -65,6 +67,7 @@ public SplitPreserveAllTokens(final Text text) { * @param lmt The limit * @see String#split(String, int) */ + public SplitPreserveAllTokens(final CharSequence text, final int lmt) { this(new TextOf(text), new TextOf(" "), lmt); } @@ -76,9 +79,11 @@ public SplitPreserveAllTokens(final CharSequence text, final int lmt) { * @param lmt The limit * @see String#split(String, int) */ + public SplitPreserveAllTokens(final Text text, final int lmt) { this(text, new TextOf(" "), lmt); } + /** * Ctor. * @@ -86,6 +91,7 @@ public SplitPreserveAllTokens(final Text text, final int lmt) { * @param rgx The regex * @see String#split(String) */ + public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx) { this(new TextOf(text), new TextOf(rgx)); } @@ -98,6 +104,7 @@ public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx) { * @param lmt The limit * @see String#split(String, int) */ + public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx, final int lmt) { this(new TextOf(text), new TextOf(rgx), lmt); } @@ -108,6 +115,7 @@ public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx, f * @param rgx The regex * @see String#split(String) */ + public SplitPreserveAllTokens(final CharSequence text, final Text rgx) { this(new TextOf(text), rgx); } @@ -119,6 +127,7 @@ public SplitPreserveAllTokens(final CharSequence text, final Text rgx) { * @param lmt The limit * @see String#split(String, int) */ + public SplitPreserveAllTokens(final CharSequence text, final Text rgx, final int lmt) { this(new TextOf(text), rgx, lmt); } @@ -129,6 +138,7 @@ public SplitPreserveAllTokens(final CharSequence text, final Text rgx, final int * @param rgx The regex * @see String#split(String) */ + public SplitPreserveAllTokens(final Text text, final CharSequence rgx) { this(text, new TextOf(rgx)); } @@ -140,6 +150,7 @@ public SplitPreserveAllTokens(final Text text, final CharSequence rgx) { * @param lmt The limit * @see String#split(String, int) */ + public SplitPreserveAllTokens(final Text text, final CharSequence rgx, final int lmt) { this(text, new TextOf(rgx), lmt); } @@ -150,6 +161,7 @@ public SplitPreserveAllTokens(final Text text, final CharSequence rgx, final int * @param rgx The regex * @see String#split(String) */ + public SplitPreserveAllTokens(final Text text, final Text rgx) { this(text, rgx, 0); } @@ -161,6 +173,7 @@ public SplitPreserveAllTokens(final Text text, final Text rgx) { * @param lmt The limit * @see String#split(String, int) */ + public SplitPreserveAllTokens( final Text text, final Text rgx, final int lmt ) { From 22e9066b27840d0f5d64106a35799ed50514ce16 Mon Sep 17 00:00:00 2001 From: tjann Date: Fri, 1 Nov 2024 15:45:27 +0300 Subject: [PATCH 4/7] Adding an OOP analogue for StringUtils.splitPreserveAllTokens --- src/main/java/org/cactoos/TriFunc.java | 53 ++++++++ ...reserve.java => TriFuncSplitPreserve.java} | 32 +++-- .../cactoos/text/SplitPreserveAllTokens.java | 59 ++++----- .../org/cactoos/text/SplitPreserveTest.java | 121 +++++++++++++----- 4 files changed, 185 insertions(+), 80 deletions(-) create mode 100644 src/main/java/org/cactoos/TriFunc.java rename src/main/java/org/cactoos/func/{BiFuncSplitPreserve.java => TriFuncSplitPreserve.java} (68%) diff --git a/src/main/java/org/cactoos/TriFunc.java b/src/main/java/org/cactoos/TriFunc.java new file mode 100644 index 000000000..ca0858670 --- /dev/null +++ b/src/main/java/org/cactoos/TriFunc.java @@ -0,0 +1,53 @@ +/* + * 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; + +/** + * Function that accepts two arguments. + * + *

If you don't want to have any checked exceptions being thrown + * out of your {@link BiFunc}, you can use + * {@link org.cactoos.func.UncheckedBiFunc} decorator. Also + * you may try {@link org.cactoos.func.IoCheckedBiFunc}.

+ * + *

There is no thread-safety guarantee. + * + * @param Type of input + * @param Type of input + * @param Type of input + * @param Type of output + */ +@FunctionalInterface +public interface TriFunc { + + /** + * Apply it. + * @param first The first argument + * @param second The second argument + * @param third The third argument + * @return The result + * @throws Exception If fails + */ + W apply(X first, Y second, Z third) throws Exception; +} diff --git a/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java similarity index 68% rename from src/main/java/org/cactoos/func/BiFuncSplitPreserve.java rename to src/main/java/org/cactoos/func/TriFuncSplitPreserve.java index 400997351..a4396e610 100644 --- a/src/main/java/org/cactoos/func/BiFuncSplitPreserve.java +++ b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java @@ -24,26 +24,40 @@ package org.cactoos.func; -import org.cactoos.BiFunc; +import org.cactoos.TriFunc; import org.cactoos.list.ListOf; - import java.util.Collection; -public class BiFuncSplitPreserve implements BiFunc> { +/** + * Used for avoiding static method calls. + */ +public final class TriFuncSplitPreserve + implements TriFunc + > { @Override - public Collection apply(final String str, final String regex) throws Exception { - ListOf ret = new ListOf<>(); + public Collection apply( + final String str, + final String regex, + final Integer lmt + ) { + final ListOf ret = new ListOf<>(); int start = 0; int pos = str.indexOf(regex); while (pos >= start) { + if (lmt > 0 && ret.size() == lmt) { + break; + } ret.add(str.substring(start, pos)); start = pos + regex.length(); pos = str.indexOf(regex, start); } - if (start < str.length()) - ret.add(str.substring(start)); - else if (start == str.length()) - ret.add(""); + if (lmt <= 0 || ret.size() < lmt) { + if (start < str.length()) { + ret.add(str.substring(start)); + } else if (start == str.length()) { + ret.add(""); + } + } return ret; } } diff --git a/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java index 2a332c6ec..8c3f3f002 100644 --- a/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java +++ b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java @@ -24,7 +24,7 @@ package org.cactoos.text; import org.cactoos.Text; -import org.cactoos.func.BiFuncSplitPreserve; +import org.cactoos.func.TriFuncSplitPreserve; import org.cactoos.iterable.IterableEnvelope; import org.cactoos.iterable.IterableOf; import org.cactoos.iterable.Mapped; @@ -34,8 +34,6 @@ * Splits the Text into an array, 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 { @@ -43,9 +41,7 @@ public final class SplitPreserveAllTokens extends IterableEnvelope { * Ctor. * * @param text The text - * @see String#split(String) */ - public SplitPreserveAllTokens(final CharSequence text) { this(new TextOf(text), new TextOf(" ")); } @@ -54,7 +50,6 @@ public SplitPreserveAllTokens(final CharSequence text) { * Ctor. * * @param text The text - * @see String#split(String) */ public SplitPreserveAllTokens(final Text text) { this(text, new TextOf(" ")); @@ -65,9 +60,7 @@ public SplitPreserveAllTokens(final Text text) { * * @param text The text * @param lmt The limit - * @see String#split(String, int) */ - public SplitPreserveAllTokens(final CharSequence text, final int lmt) { this(new TextOf(text), new TextOf(" "), lmt); } @@ -77,9 +70,7 @@ public SplitPreserveAllTokens(final CharSequence text, final int lmt) { * * @param text The text * @param lmt The limit - * @see String#split(String, int) */ - public SplitPreserveAllTokens(final Text text, final int lmt) { this(text, new TextOf(" "), lmt); } @@ -89,9 +80,7 @@ public SplitPreserveAllTokens(final Text text, final int lmt) { * * @param text The text * @param rgx The regex - * @see String#split(String) */ - public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx) { this(new TextOf(text), new TextOf(rgx)); } @@ -102,93 +91,89 @@ public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx) { * @param text The text * @param rgx The regex * @param lmt The limit - * @see String#split(String, int) */ - public SplitPreserveAllTokens(final CharSequence text, final CharSequence rgx, final int lmt) { this(new TextOf(text), new TextOf(rgx), lmt); } /** * Ctor. + * * @param text The text * @param rgx The regex - * @see String#split(String) */ - public SplitPreserveAllTokens(final CharSequence text, final Text rgx) { this(new TextOf(text), rgx); } /** * Ctor. + * * @param text The text * @param rgx The regex * @param lmt The limit - * @see String#split(String, int) */ - public SplitPreserveAllTokens(final CharSequence text, final Text rgx, final int lmt) { this(new TextOf(text), rgx, lmt); } /** * Ctor. + * * @param text The text * @param rgx The regex - * @see String#split(String) */ - public SplitPreserveAllTokens(final Text text, final CharSequence rgx) { this(text, new TextOf(rgx)); } /** * Ctor. + * * @param text The text * @param rgx The regex * @param lmt The limit - * @see String#split(String, int) */ - public SplitPreserveAllTokens(final Text text, final CharSequence rgx, final int lmt) { this(text, new TextOf(rgx), lmt); } /** * Ctor. + * * @param text The text * @param rgx The regex - * @see String#split(String) */ - public SplitPreserveAllTokens(final Text text, final Text rgx) { this(text, rgx, 0); } /** * Ctor. + * * @param text The text * @param rgx The regex * @param lmt The limit - * @see String#split(String, int) */ - public SplitPreserveAllTokens( - final Text text, final Text rgx, final int lmt + final Text text, + final Text rgx, + final int lmt ) { super( - new Mapped<>( - TextOf::new, - new IterableOf<>( - () -> new IteratorOf<>( - new BiFuncSplitPreserve() - .apply(text.toString(), - rgx.toString()) - .toArray(new String[0])) - ) + new Mapped<>( + TextOf::new, + new IterableOf<>( + () -> new IteratorOf<>( + new TriFuncSplitPreserve() + .apply( + text.toString(), + rgx.toString(), + lmt + ).toArray(new String[0]) + ) ) + ) ); } - } diff --git a/src/test/java/org/cactoos/text/SplitPreserveTest.java b/src/test/java/org/cactoos/text/SplitPreserveTest.java index 45522de1c..97f27f127 100644 --- a/src/test/java/org/cactoos/text/SplitPreserveTest.java +++ b/src/test/java/org/cactoos/text/SplitPreserveTest.java @@ -30,85 +30,138 @@ import org.hamcrest.core.IsNot; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; - import java.util.ArrayList; import java.util.Iterator; -public class SplitPreserveTest { +final class SplitPreserveTest { @Test - void checkingSplit() throws Exception { + void checkingSplit() { String txt = " "; - ArrayList array = new ArrayList<>(); + final String msg = "Adjacent separators must create an empty element"; + ArrayList array = new ArrayList<>(4); array.add(new TextOf("")); array.add(new TextOf("")); array.add(new TextOf("")); array.add(new TextOf("")); new Assertion<>( - "Adjacent separators must create an empty element", - getLength(new Split(new TextOf(txt), new TextOf(" ")).iterator()), - IsNot.not(Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator()))) + msg, + this.getLength( + new Split( + new TextOf(txt), + new TextOf(" ") + ).iterator() + ), + IsNot.not( + Matchers.equalTo( + this.getLength( + new IterableOf( + array.iterator() + ).iterator() + ) + ) + ) ).affirm(); - txt = " how "; - array = new ArrayList<>(); + array = new ArrayList<>(3); array.add(new TextOf("")); array.add(new TextOf("how")); array.add(new TextOf("")); - new Assertion<>( - "Adjacent separators must create an empty element", - getLength(new Split(new TextOf(txt), new TextOf(" ")).iterator()), - IsNot.not(Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator()))) + msg, + this.getLength( + new Split( + new TextOf(txt), + new TextOf(" ") + ).iterator() + ), + IsNot.not( + Matchers.equalTo( + this.getLength( + new IterableOf( + array.iterator() + ).iterator() + ) + ) + ) ).affirm(); } - int getLength(Iterator iter) { + int getLength(final Iterator iter) { int count = 0; while (iter.hasNext()) { iter.next(); - count++; + count += 1; } return count; } @Test - void checkingSplitPreserveTokens() throws Exception { + void checkingSplitPreserveTokens() { String txt = " "; - ArrayList array = new ArrayList<>(); + final String msg = "Adjacent separators must create an empty element"; + ArrayList array = new ArrayList<>(4); array.add(new TextOf("")); array.add(new TextOf("")); array.add(new TextOf("")); array.add(new TextOf("")); - new Assertion<>( - "Adjacent separators must create an empty element", - getLength(new SplitPreserveAllTokens(new TextOf(txt), new TextOf(" ")).iterator()), - Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator())) + msg, + this.getLength( + new SplitPreserveAllTokens( + new TextOf(txt), + new TextOf(" ") + ).iterator() + ), + Matchers.equalTo( + this.getLength( + new IterableOf( + array.iterator() + ).iterator() + ) + ) ).affirm(); - txt = "lol\\ / dude"; - array = new ArrayList<>(); + array = new ArrayList<>(4); array.add(new TextOf("lol\\")); array.add(new TextOf("")); array.add(new TextOf("/")); array.add(new TextOf("dude")); - new Assertion<>( - "Adjacent separators must create an empty element", - getLength(new SplitPreserveAllTokens(new TextOf(txt), new TextOf(" ")).iterator()), - Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator())) + msg, + this.getLength( + new SplitPreserveAllTokens( + new TextOf(txt), + new TextOf(" ") + ).iterator() + ), + Matchers.equalTo( + this.getLength( + new IterableOf( + array.iterator() + ).iterator() + ) + ) ).affirm(); - txt = " how "; - array = new ArrayList<>(); + array = new ArrayList<>(3); array.add(new TextOf("")); array.add(new TextOf("how")); array.add(new TextOf("")); - new Assertion<>( - "Adjacent separators must create an empty element", - getLength(new SplitPreserveAllTokens(new TextOf(txt), new TextOf(" ")).iterator()), - Matchers.equalTo(getLength(new IterableOf(array.iterator()).iterator())) + msg, + this.getLength( + new SplitPreserveAllTokens( + new TextOf(txt), + new TextOf(" ") + ).iterator() + ), + Matchers.equalTo( + this.getLength( + new IterableOf( + array.iterator() + ).iterator() + ) + ) ).affirm(); } -} \ No newline at end of file +} From fe443766c753052220c52ab91a917eb9dc4f012c Mon Sep 17 00:00:00 2001 From: tjann Date: Fri, 1 Nov 2024 17:18:57 +0300 Subject: [PATCH 5/7] Correct the code to avoid stylechecker violation --- src/main/java/org/cactoos/TriFunc.java | 10 ++-------- .../java/org/cactoos/func/TriFuncSplitPreserve.java | 8 +++++--- .../java/org/cactoos/text/SplitPreserveAllTokens.java | 2 +- src/test/java/org/cactoos/text/SplitPreserveTest.java | 10 ++++++++-- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/cactoos/TriFunc.java b/src/main/java/org/cactoos/TriFunc.java index ca0858670..e500276b8 100644 --- a/src/main/java/org/cactoos/TriFunc.java +++ b/src/main/java/org/cactoos/TriFunc.java @@ -24,19 +24,13 @@ package org.cactoos; /** - * Function that accepts two arguments. - * - *

If you don't want to have any checked exceptions being thrown - * out of your {@link BiFunc}, you can use - * {@link org.cactoos.func.UncheckedBiFunc} decorator. Also - * you may try {@link org.cactoos.func.IoCheckedBiFunc}.

- * - *

There is no thread-safety guarantee. + * Function that accepts three arguments. * * @param Type of input * @param Type of input * @param Type of input * @param Type of output + * @since 0.0 */ @FunctionalInterface public interface TriFunc { diff --git a/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java index a4396e610..2c995aba1 100644 --- a/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java +++ b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java @@ -24,16 +24,18 @@ package org.cactoos.func; +import java.util.Collection; import org.cactoos.TriFunc; import org.cactoos.list.ListOf; -import java.util.Collection; /** * Used for avoiding static method calls. + * + * @since 0.0 */ public final class TriFuncSplitPreserve - implements TriFunc - > { + implements TriFunc + > { @Override public Collection apply( final String str, diff --git a/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java index 8c3f3f002..b3b54bdec 100644 --- a/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java +++ b/src/main/java/org/cactoos/text/SplitPreserveAllTokens.java @@ -34,7 +34,7 @@ * Splits the Text into an array, including empty * tokens created by adjacent separators. * - * @since 0.9 + * @since 0.0 */ public final class SplitPreserveAllTokens extends IterableEnvelope { /** diff --git a/src/test/java/org/cactoos/text/SplitPreserveTest.java b/src/test/java/org/cactoos/text/SplitPreserveTest.java index 97f27f127..575e6b9a4 100644 --- a/src/test/java/org/cactoos/text/SplitPreserveTest.java +++ b/src/test/java/org/cactoos/text/SplitPreserveTest.java @@ -24,15 +24,21 @@ package org.cactoos.text; +import java.util.ArrayList; +import java.util.Iterator; import org.cactoos.Text; import org.cactoos.iterable.IterableOf; import org.hamcrest.Matchers; import org.hamcrest.core.IsNot; import org.junit.jupiter.api.Test; import org.llorllale.cactoos.matchers.Assertion; -import java.util.ArrayList; -import java.util.Iterator; +/** + * Testing correctness of SplitPreserveAllTokens. + * Compare with Split class in specified cases. + * + * @since 0.0 + */ final class SplitPreserveTest { @Test void checkingSplit() { From 78683cb984a54c8480f7785c5e577717bb242bc2 Mon Sep 17 00:00:00 2001 From: tjann Date: Sat, 2 Nov 2024 13:54:00 +0300 Subject: [PATCH 6/7] Description for TriFuncSplitPreserve.java added + bug fixed --- .../org/cactoos/func/TriFuncSplitPreserve.java | 16 +++++++++++++--- .../java/org/cactoos/text/SplitPreserveTest.java | 8 ++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java index 2c995aba1..c0d6e257f 100644 --- a/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java +++ b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java @@ -29,7 +29,17 @@ import org.cactoos.list.ListOf; /** - * Used for avoiding static method calls. + * A String splitter preserving all tokens. + * Unlike regular Split, stores empty "" tokens + * created by adjacent regex separators. + * + *

+ * Examples: + * 1) text - " hello there", regex - " " + * result: ["", "hello", "there", ""] + * 2) text - "aaa", regex - "a" + * result: ["", "", "", ""] + *

* * @since 0.0 */ @@ -46,14 +56,14 @@ public Collection apply( int start = 0; int pos = str.indexOf(regex); while (pos >= start) { - if (lmt > 0 && ret.size() == lmt) { + if (lmt > 0 && ret.size() == lmt || regex.isEmpty()) { break; } ret.add(str.substring(start, pos)); start = pos + regex.length(); pos = str.indexOf(regex, start); } - if (lmt <= 0 || ret.size() < lmt) { + if (lmt <= 0 || ret.size() < lmt || regex.isEmpty()) { if (start < str.length()) { ret.add(str.substring(start)); } else if (start == str.length()) { diff --git a/src/test/java/org/cactoos/text/SplitPreserveTest.java b/src/test/java/org/cactoos/text/SplitPreserveTest.java index 575e6b9a4..01dc0a4c5 100644 --- a/src/test/java/org/cactoos/text/SplitPreserveTest.java +++ b/src/test/java/org/cactoos/text/SplitPreserveTest.java @@ -42,7 +42,7 @@ final class SplitPreserveTest { @Test void checkingSplit() { - String txt = " "; + String txt = "aaa"; final String msg = "Adjacent separators must create an empty element"; ArrayList array = new ArrayList<>(4); array.add(new TextOf("")); @@ -54,7 +54,7 @@ void checkingSplit() { this.getLength( new Split( new TextOf(txt), - new TextOf(" ") + new TextOf("a") ).iterator() ), IsNot.not( @@ -103,7 +103,7 @@ int getLength(final Iterator iter) { @Test void checkingSplitPreserveTokens() { - String txt = " "; + String txt = "aaa"; final String msg = "Adjacent separators must create an empty element"; ArrayList array = new ArrayList<>(4); array.add(new TextOf("")); @@ -115,7 +115,7 @@ void checkingSplitPreserveTokens() { this.getLength( new SplitPreserveAllTokens( new TextOf(txt), - new TextOf(" ") + new TextOf("a") ).iterator() ), Matchers.equalTo( From 1e2a0ca9abd712a8f9edf1e78801da8705791c7b Mon Sep 17 00:00:00 2001 From: tjann Date: Sat, 2 Nov 2024 13:57:27 +0300 Subject: [PATCH 7/7] Description for TriFuncSplitPreserve.java added + bug fixed --- src/main/java/org/cactoos/func/TriFuncSplitPreserve.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java index c0d6e257f..068f30b5a 100644 --- a/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java +++ b/src/main/java/org/cactoos/func/TriFuncSplitPreserve.java @@ -35,7 +35,7 @@ * *

* Examples: - * 1) text - " hello there", regex - " " + * 1) text - " hello there ", regex - " " * result: ["", "hello", "there", ""] * 2) text - "aaa", regex - "a" * result: ["", "", "", ""]