forked from yegor256/cactoos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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 three arguments. | ||
* | ||
* @param <X> Type of input | ||
* @param <Y> Type of input | ||
* @param <Z> Type of input | ||
* @param <W> Type of output | ||
* @since 0.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface TriFunc<X, Y, Z, W> { | ||
|
||
/** | ||
* 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* 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 java.util.Collection; | ||
import org.cactoos.TriFunc; | ||
import org.cactoos.list.ListOf; | ||
|
||
/** | ||
* A String splitter preserving all tokens. | ||
* Unlike regular Split, stores empty "" tokens | ||
* created by adjacent regex separators. | ||
* | ||
* <p> | ||
* Examples: | ||
* 1) text - " hello there ", regex - " " | ||
* result: ["", "hello", "there", ""] | ||
* 2) text - "aaa", regex - "a" | ||
* result: ["", "", "", ""] | ||
* </p> | ||
* | ||
* @since 0.0 | ||
*/ | ||
public final class TriFuncSplitPreserve | ||
implements TriFunc | ||
<String, String, Integer, Collection<String>> { | ||
@Override | ||
public Collection<String> apply( | ||
final String str, | ||
final String regex, | ||
final Integer lmt | ||
) { | ||
final ListOf<String> ret = new ListOf<>(); | ||
int start = 0; | ||
int pos = str.indexOf(regex); | ||
while (pos >= start) { | ||
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 || regex.isEmpty()) { | ||
if (start < str.length()) { | ||
ret.add(str.substring(start)); | ||
} else if (start == str.length()) { | ||
ret.add(""); | ||
} | ||
} | ||
return ret; | ||
} | ||
} |
179 changes: 179 additions & 0 deletions
179
src/main/java/org/cactoos/text/SplitPreserveAllTokens.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
* 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.TriFuncSplitPreserve; | ||
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. | ||
* | ||
* @since 0.0 | ||
*/ | ||
public final class SplitPreserveAllTokens extends IterableEnvelope<Text> { | ||
/** | ||
* Ctor. | ||
* | ||
* @param text The text | ||
*/ | ||
public SplitPreserveAllTokens(final CharSequence text) { | ||
this(new TextOf(text), new TextOf(" ")); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param text The text | ||
*/ | ||
public SplitPreserveAllTokens(final Text text) { | ||
this(text, new TextOf(" ")); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param text The text | ||
* @param lmt The limit | ||
*/ | ||
public SplitPreserveAllTokens(final CharSequence text, final int lmt) { | ||
this(new TextOf(text), new TextOf(" "), lmt); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param text The text | ||
* @param lmt The limit | ||
*/ | ||
public SplitPreserveAllTokens(final Text text, final int lmt) { | ||
this(text, new TextOf(" "), lmt); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* | ||
* @param text The text | ||
* @param rgx The regex | ||
*/ | ||
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 | ||
*/ | ||
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 | ||
*/ | ||
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 | ||
*/ | ||
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 | ||
*/ | ||
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 | ||
*/ | ||
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 | ||
*/ | ||
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 | ||
*/ | ||
public SplitPreserveAllTokens( | ||
final Text text, | ||
final Text rgx, | ||
final int lmt | ||
) { | ||
super( | ||
new Mapped<>( | ||
TextOf::new, | ||
new IterableOf<>( | ||
() -> new IteratorOf<>( | ||
new TriFuncSplitPreserve() | ||
.apply( | ||
text.toString(), | ||
rgx.toString(), | ||
lmt | ||
).toArray(new String[0]) | ||
) | ||
) | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.