Skip to content

Commit

Permalink
(#1116) Extends TextEnvelope when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed May 25, 2019
1 parent aac3b94 commit ba66fa7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 83 deletions.
52 changes: 14 additions & 38 deletions src/main/java/org/cactoos/text/FormattedText.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
package org.cactoos.text;

import java.util.Collection;
import java.util.Collections;
import java.util.Formatter;
import java.util.Locale;
import org.cactoos.Scalar;
import org.cactoos.Text;
import org.cactoos.list.ListOf;

Expand All @@ -36,27 +36,8 @@
* <p>There is no thread-safety guarantee.
*
* @since 0.1
* @todo #1063: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 FormattedText implements Text {

/**
* Pattern.
*/
private final Text pattern;

/**
* Arguments.
*/
private final Collection<Object> args;

/**
* Format locale.
*/
private final Locale locale;
public final class FormattedText extends TextEnvelope {

/**
* New formatted string with default locale.
Expand Down Expand Up @@ -148,27 +129,22 @@ public FormattedText(
*
* @param ptn Pattern
* @param locale Format locale
* @param arguments Arguments
* @param args Arguments
*/
public FormattedText(
final Text ptn,
final Locale locale,
final Collection<Object> arguments
final Collection<Object> args
) {
this.pattern = ptn;
this.locale = locale;
this.args = Collections.unmodifiableCollection(arguments);
}

@Override
public String asString() throws Exception {
final StringBuilder out = new StringBuilder(0);
try (final Formatter fmt = new Formatter(out, this.locale)) {
fmt.format(
this.pattern.asString(),
this.args.toArray(new Object[0])
);
}
return out.toString();
super((Scalar<String>) () -> {
final StringBuilder out = new StringBuilder(0);
try (final Formatter fmt = new Formatter(out, locale)) {
fmt.format(
ptn.asString(),
args.toArray()
);
}
return out.toString();
});
}
}
39 changes: 16 additions & 23 deletions src/main/java/org/cactoos/text/NoNulls.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,33 +33,25 @@
*
* @since 0.11
*/
public final class NoNulls implements Text {
/**
* The origin text.
*/
private final Text origin;

public final class NoNulls extends TextEnvelope {
/**
* Ctor.
* @param text The text
*/
public NoNulls(final Text text) {
this.origin = text;
}

@Override
public String asString() throws Exception {
if (this.origin == null) {
throw new IllegalArgumentException(
"NULL instead of a valid text"
);
}
final String string = this.origin.asString();
if (string == null) {
throw new IllegalStateException(
"NULL instead of a valid result string"
);
}
return string;
super((Scalar<String>) () -> {
if (text == null) {
throw new IllegalArgumentException(
"NULL instead of a valid text"
);
}
final String string = text.asString();
if (string == null) {
throw new IllegalStateException(
"NULL instead of a valid result string"
);
}
return string;
});
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/cactoos/text/Randomized.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
* <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 {

Expand Down
29 changes: 7 additions & 22 deletions src/main/java/org/cactoos/text/Synced.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,18 +33,7 @@
*
* @since 0.18
*/
public final class Synced implements Text {

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

/**
* The lock.
*/
private final Object lock;

public final class Synced extends TextEnvelope {
/**
* Ctor.
* @param text The text
Expand All @@ -58,15 +48,10 @@ public Synced(final Text text) {
* @param lck The lock
*/
public Synced(final Text text, final Object lck) {
this.origin = text;
this.lock = lck;
super((Scalar<String>) () -> {
synchronized (lck) {
return text.asString();
}
});
}

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

}

0 comments on commit ba66fa7

Please sign in to comment.