Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Aug 8, 2020
2 parents 3a2c5dc + 2848c94 commit e21e285
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 140 deletions.
44 changes: 19 additions & 25 deletions src/main/java/org/cactoos/text/HexOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
package org.cactoos.text;

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

/**
* Hexadecimal representation of Bytes.
Expand All @@ -34,7 +34,7 @@
* @since 0.28
*/
@SuppressWarnings("PMD.ConstructorShouldDoInitialization")
public final class HexOf implements Text {
public final class HexOf extends TextEnvelope {

/**
* The hexadecimal chars.
Expand All @@ -44,31 +44,25 @@ public final class HexOf implements Text {
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
};

/**
* The Bytes.
*/
private final Bytes bytes;

/**
* Ctor.
* @param source The bytes
* @param bytes The bytes
*/
public HexOf(final Bytes source) {
this.bytes = source;
public HexOf(final Bytes bytes) {
super(new Scalar<String>() {
@Override
public String value() throws Exception {
final byte[] bts = bytes.asBytes();
final char[] hex = new char[bts.length * 2];
int chr = -1;
for (int idx = 0; idx < bts.length; ++idx) {
// @checkstyle MagicNumber (3 line)
final int value = 0xff & bts[idx];
hex[++chr] = HexOf.HEX_CHARS[value >>> 4];
hex[++chr] = HexOf.HEX_CHARS[value & 0x0f];
}
return new String(hex);
}
});
}

@Override
public String asString() throws Exception {
final byte[] bts = this.bytes.asBytes();
final char[] hex = new char[bts.length * 2];
int chr = -1;
for (int idx = 0; idx < bts.length; ++idx) {
// @checkstyle MagicNumber (3 line)
final int value = 0xff & bts[idx];
hex[++chr] = HexOf.HEX_CHARS[value >>> 4];
hex[++chr] = HexOf.HEX_CHARS[value & 0x0f];
}
return new String(hex);
}

}
51 changes: 14 additions & 37 deletions src/main/java/org/cactoos/text/Randomized.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.List;
import java.util.Random;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.Unchecked;

Expand All @@ -37,33 +36,14 @@
* <p>There is no thread-safety guarantee.
*
* @since 0.32
* @todo #1116:30min All classes implementing Text need to be refactored
* to extend TextEnvelope - asString() should be removed and implementation
* from TextEnvelope should be used. This to-do should be moved to another
* class which need to be refactored.
*/
public final class Randomized implements Text {
public final class Randomized extends TextEnvelope {

/**
* Max length of generated text (if no length is specified).
*/
private static final int MAX_RANDOM_LENGTH = 255;

/**
* List of characters allowed for generating.
*/
private final List<Character> characters;

/**
* Length of generated text.
*/
private final Scalar<Integer> length;

/**
* Characters index randomizer.
*/
private final Random indices;

/**
* Ctor.
*/
Expand Down Expand Up @@ -159,21 +139,18 @@ public Randomized(final List<Character> chrs, final Scalar<Integer> len) {
* @param len Length of generated text.
* @param rnd Characters index randomizer.
*/
public Randomized(final List<Character> chrs, final Scalar<Integer> len,
final Random rnd) {
this.characters = chrs;
this.length = len;
this.indices = rnd;
}

@Override
public String asString() {
final int len = new Unchecked<>(this.length).value();
final StringBuilder builder = new StringBuilder(len);
final int bound = this.characters.size();
for (int index = 0; index < len; index = index + 1) {
builder.append(this.characters.get(this.indices.nextInt(bound)));
}
return builder.toString();
public Randomized(final List<Character> chrs, final Scalar<Integer> len, final Random rnd) {
super(new Scalar<String>() {
@Override
public String value() {
final int length = new Unchecked<>(len).value();
final StringBuilder builder = new StringBuilder(length);
final int bound = chrs.size();
for (int index = 0; index < length; index = index + 1) {
builder.append(chrs.get(rnd.nextInt(bound)));
}
return builder.toString();
}
});
}
}
21 changes: 8 additions & 13 deletions src/main/java/org/cactoos/text/Trimmed.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,24 +33,18 @@
*
* @since 0.1
*/
public final class Trimmed implements Text {

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

/**
* Ctor.
* @param text The text
*/
public Trimmed(final Text text) {
this.origin = text;
super(new Scalar<String>() {
@Override
public String value() throws Exception {
return text.asString().trim();
}
});
}

@Override
public String asString() throws Exception {
return this.origin.asString().trim();
}

}
35 changes: 15 additions & 20 deletions src/main/java/org/cactoos/text/TrimmedLeft.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,31 +33,25 @@
*
* @since 0.12
*/
public final class TrimmedLeft implements Text {

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

/**
* Ctor.
* @param text The text
*/
public TrimmedLeft(final Text text) {
this.origin = text;
super(new Scalar<String>() {
@Override
public String value() throws Exception {
final String string = text.asString();
int cursor = 0;
while (cursor < string.length() && Character.isWhitespace(
string.charAt(cursor)
)) {
cursor = cursor + 1;
}
return string.substring(cursor);
}
});
}

@Override
public String asString() throws Exception {
final String text = this.origin.asString();
int cursor = 0;
while (cursor < text.length() && Character.isWhitespace(
text.charAt(cursor)
)) {
cursor = cursor + 1;
}
return text.substring(cursor);
}

}
31 changes: 13 additions & 18 deletions src/main/java/org/cactoos/text/TrimmedRight.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,29 +33,23 @@
*
* @since 0.12
*/
public final class TrimmedRight implements Text {

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

/**
* Ctor.
* @param text The text
*/
public TrimmedRight(final Text text) {
this.origin = text;
super(new Scalar<String>() {
@Override
public String value() throws Exception {
final String string = text.asString();
int cursor = string.length() - 1;
while (cursor >= 0 && Character.isWhitespace(string.charAt(cursor))) {
cursor = cursor - 1;
}
return string.substring(0, cursor + 1);
}
});
}

@Override
public String asString() throws Exception {
final String text = this.origin.asString();
int cursor = text.length() - 1;
while (cursor >= 0 && Character.isWhitespace(text.charAt(cursor))) {
cursor = cursor - 1;
}
return text.substring(0, cursor + 1);
}

}
31 changes: 10 additions & 21 deletions src/main/java/org/cactoos/text/Upper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.cactoos.text;

import java.util.Locale;
import org.cactoos.Scalar;
import org.cactoos.Text;

/**
Expand All @@ -33,17 +34,7 @@
*
* @since 0.1
*/
public final class Upper implements Text {

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

/**
* The locale.
*/
private final Locale locale;
public final class Upper extends TextEnvelope {

/**
* Ctor.
Expand All @@ -64,16 +55,14 @@ public Upper(final Text text) {
/**
* Ctor.
* @param text The text
* @param lang Locale
* @param locale Locale
*/
public Upper(final Text text, final Locale lang) {
this.origin = text;
this.locale = lang;
}

@Override
public String asString() throws Exception {
return this.origin.asString().toUpperCase(this.locale);
public Upper(final Text text, final Locale locale) {
super(new Scalar<String>() {
@Override
public String value() throws Exception {
return text.asString().toUpperCase(locale);
}
});
}

}
4 changes: 2 additions & 2 deletions src/test/java/org/cactoos/io/AppendToTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public final class AppendToTest {
* Ensures that AppendTo is failing on a negative predicate result.
*/
@Test
public void failsIfFileDoesNotExist() {
public void failsIfFileDoesNotExist() throws Exception {
final File source = new File(
new Randomized(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
).asString()
);
new Assertion<>(
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/cactoos/iterator/ImmutableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void decoratesHasNext() {
}

@Test
public void decoratesToString() {
public void decoratesToString() throws Exception {
final String string = new Randomized().asString();
final Iterator<Object> iterator = new Iterator<Object>() {
public Object next() {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/cactoos/text/RandomizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
public final class RandomizedTest {

@Test
public void generatesRandomTextOfRandomLength() {
public void generatesRandomTextOfRandomLength() throws Exception {
new Assertion<>(
"Generated text is empty",
new Randomized().asString().length(),
Expand All @@ -50,7 +50,7 @@ public void generatesRandomTextOfRandomLength() {
}

@Test
public void generatesRandomTextOfSpecifiedLength() {
public void generatesRandomTextOfSpecifiedLength() throws Exception {
new Assertion<>(
"Generated text has incorrect length",
new Randomized(512).asString().length(),
Expand All @@ -59,7 +59,7 @@ public void generatesRandomTextOfSpecifiedLength() {
}

@Test
public void generatesRandomTextOfSpecifiedChars() {
public void generatesRandomTextOfSpecifiedChars() throws Exception {
new Assertion<>(
"Generated text contains not allowed characters",
new Randomized('a')
Expand Down

0 comments on commit e21e285

Please sign in to comment.