Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 21, 2018
2 parents 62e1fa4 + 2146d91 commit 4740927
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 193 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/text/JoinedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* <p>There is no thread-safety guarantee.
*
* @since 0.9
* @todo #849:30min Continue refactoring all classes implementing Text to extend
* @todo #878:30min Continue refactoring all classes implementing Text to extend
* TextEnvelope - in most cases asString should be removed and implementation
* from TextEnvelope should be used.
*/
Expand Down
43 changes: 12 additions & 31 deletions src/main/java/org/cactoos/text/PaddedStartText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -32,22 +33,7 @@
*
* @since 0.32
*/
public final class PaddedStartText implements Text {

/**
* The text.
*/
private final Text origin;

/**
* The minimum length of the resulting string.
*/
private final int length;

/**
* The character to be padded at the begining.
*/
private final char symbol;
public final class PaddedStartText extends TextEnvelope {

/**
* Ctor.
Expand All @@ -57,21 +43,16 @@ public final class PaddedStartText implements Text {
*/
public PaddedStartText(
final Text text, final int length, final char symbol) {
this.origin = text;
this.symbol = symbol;
this.length = length;
}

@Override
public String asString() throws Exception {
final String original = this.origin.asString();
final int diff = this.length - original.length();
final StringBuilder builder = new StringBuilder();
for (int len = diff; len > 0; --len) {
builder.append(this.symbol);
}
builder.append(original);
return builder.toString();
super((Scalar<String>) () -> {
final String original = text.asString();
return new JoinedText(
new TextOf(""),
new RepeatedText(
new TextOf(symbol), length - original.length()
),
text
).asString();
});
}
}

59 changes: 19 additions & 40 deletions src/main/java/org/cactoos/text/ReplacedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,7 @@
*
* @since 0.2
*/
public final class ReplacedText implements Text {

/**
* The text.
*/
private final Text origin;

/**
* The regular expression identifying the text to replace.
*/
private final Scalar<Pattern> regex;

/**
* The new replacement text.
*/
private final Func<Matcher, String> replacement;
public final class ReplacedText extends TextEnvelope {

/**
* Ctor.
Expand Down Expand Up @@ -91,35 +76,29 @@ public ReplacedText(
* Note: a {@link PatternSyntaxException} will be thrown if the
* regular expression's syntax is invalid.
* @param text The text
* @param pattern The regular expression
* @param regex The regular expression
* @param func Transforms the resulting matcher object into a replacement
* string. Any exceptions will be wrapped in an {@link IOException}.
*/
public ReplacedText(
final Text text,
final Scalar<Pattern> pattern,
final Scalar<Pattern> regex,
final Func<Matcher, String> func) {
this.origin = text;
this.regex = pattern;
this.replacement = func;
super((Scalar<String>) () -> {
final StringBuffer buffer = new StringBuffer();
final Matcher matcher = new IoCheckedScalar<>(regex)
.value()
.matcher(text.asString());
final IoCheckedFunc<Matcher, String> safe =
new IoCheckedFunc<>(func);
while (matcher.find()) {
matcher.appendReplacement(
buffer,
safe.apply(matcher)
);
}
matcher.appendTail(buffer);
return buffer.toString();
});
}

@Override
public String asString() throws Exception {
final StringBuffer buffer = new StringBuffer();
final Matcher matcher = new IoCheckedScalar<>(this.regex)
.value()
.matcher(this.origin.asString());
final IoCheckedFunc<Matcher, String> safe =
new IoCheckedFunc<>(this.replacement);
while (matcher.find()) {
matcher.appendReplacement(
buffer,
safe.apply(matcher)
);
}
matcher.appendTail(buffer);
return buffer.toString();
}

}
19 changes: 6 additions & 13 deletions src/main/java/org/cactoos/text/ReversedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,25 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
* Reverse the Text.
*
* @since 0.2
*/
public final class ReversedText implements Text {

/**
* The text.
*/
private final Text origin;
public final class ReversedText extends TextEnvelope {

/**
* Ctor.
*
* @param text The text
*/
public ReversedText(final Text text) {
this.origin = text;
super((Scalar<String>) () -> new StringBuilder(
text.asString()
).reverse().toString()
);
}

@Override
public String asString() throws Exception {
return new StringBuilder(this.origin.asString()).reverse().toString();
}

}
52 changes: 20 additions & 32 deletions src/main/java/org/cactoos/text/RotatedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,39 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
* Rotate (circular shift) a String of shift characters.
* @since 0.12
*/
public final class RotatedText implements Text {

/**
* The text.
*/
private final Text origin;

/**
* The move.
*/
private final int move;
public final class RotatedText extends TextEnvelope {

/**
* Ctor.
* @param text The text
* @param shift The shift
*/
@SuppressWarnings({"PMD.CallSuperInConstructor",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"})
public RotatedText(final Text text, final int shift) {
this.origin = text;
this.move = shift;
}

@Override
public String asString() throws Exception {
String text = this.origin.asString();
final int length = text.length();
if (length != 0 && this.move != 0 && this.move % length != 0) {
final StringBuilder builder = new StringBuilder(length);
int offset = -(this.move % length);
if (offset < 0) {
offset = text.length() + offset;
super((Scalar<String>) () -> {
String origin = text.asString();
final int length = origin.length();
if (length != 0 && shift != 0 && shift % length != 0) {
final StringBuilder builder = new StringBuilder(length);
int offset = -(shift % length);
if (offset < 0) {
offset = origin.length() + offset;
}
origin = builder.append(
origin.substring(offset)
).append(
origin.substring(0, offset)
).toString();
}
text = builder.append(
text.substring(offset)
).append(
text.substring(0, offset)
).toString();
}
return text;
return origin;
});
}

}
58 changes: 19 additions & 39 deletions src/main/java/org/cactoos/text/SubText.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,9 @@
* Extract a substring from a Text.
*
* <p>There is no thread-safety guarantee.
*
* @since 0.11
*/
public final class SubText implements Text {

/**
* The text.
*/
private final Text origin;

/**
* The start position in the text.
*/
private final UncheckedScalar<Integer> start;

/**
* The end position in the text.
*/
private final UncheckedScalar<Integer> end;
public final class SubText extends TextEnvelope {

/**
* Ctor.
Expand Down Expand Up @@ -103,28 +87,24 @@ public SubText(final Text text, final Scalar<Integer> strt,
/**
* Ctor.
* @param text The Text
* @param strt Start position in the text
* @param finish End position in the text
* @param start Start position in the text
* @param end End position in the text
*/
public SubText(final Text text, final UncheckedScalar<Integer> strt,
final UncheckedScalar<Integer> finish) {
this.origin = text;
this.start = strt;
this.end = finish;
@SuppressWarnings({"PMD.CallSuperInConstructor",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"})
public SubText(final Text text, final UncheckedScalar<Integer> start,
final UncheckedScalar<Integer> end) {
super((Scalar<String>) () -> {
int begin = start.value();
if (begin < 0) {
begin = 0;
}
int finish = end.value();
final String origin = text.asString();
if (origin.length() < finish) {
finish = origin.length();
}
return origin.substring(begin, finish);
});
}

@Override
public String asString() throws Exception {
int begin = this.start.value();
if (begin < 0) {
begin = 0;
}
int finish = this.end.value();
final String text = this.origin.asString();
if (text.length() < finish) {
finish = text.length();
}
return text.substring(begin, finish);
}

}
38 changes: 16 additions & 22 deletions src/main/java/org/cactoos/text/SwappedCaseText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,35 @@
*/
package org.cactoos.text;

import org.cactoos.Scalar;
import org.cactoos.Text;

/**
* Swaps the case of a Text changing upper and title case to lower case,
* and lower case to upper case.
* @since 0.13.3
*/
public final class SwappedCaseText implements Text {

/**
* The text.
*/
private final Text origin;
public final class SwappedCaseText extends TextEnvelope {

/**
* Ctor.
* @param text The text
*/
@SuppressWarnings({"PMD.CallSuperInConstructor",
"PMD.ConstructorOnlyInitializesOrCallOtherConstructors"})
public SwappedCaseText(final Text text) {
this.origin = text;
}

@Override
public String asString() throws Exception {
final String text = this.origin.asString();
final char[] chars = text.toCharArray();
for (int idx = 0; idx < chars.length; idx += 1) {
final char chr = chars[idx];
if (Character.isUpperCase(chr)) {
chars[idx] = Character.toLowerCase(chr);
} else if (Character.isLowerCase(chr)) {
chars[idx] = Character.toUpperCase(chr);
super((Scalar<String>) () -> {
final String origin = text.asString();
final char[] chars = origin.toCharArray();
for (int idx = 0; idx < chars.length; idx += 1) {
final char chr = chars[idx];
if (Character.isUpperCase(chr)) {
chars[idx] = Character.toLowerCase(chr);
} else if (Character.isLowerCase(chr)) {
chars[idx] = Character.toUpperCase(chr);
}
}
}
return new String(chars);
return new String(chars);
});
}

}
Loading

2 comments on commit 4740927

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 4740927 May 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 849-f712c12a disappeared from src/main/java/org/cactoos/text/JoinedText.java, that's why I closed #878. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 4740927 May 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 878-f712c12a discovered in src/main/java/org/cactoos/text/JoinedText.java and submitted as #897. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.