Skip to content

Commit

Permalink
#897, all classes implementing Text were refactored to extend TextEnv…
Browse files Browse the repository at this point in the history
…elope in package org.cactoos.time
  • Loading branch information
butenkos committed Jul 4, 2018
1 parent 84f7c9d commit 4348d7e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 85 deletions.
25 changes: 9 additions & 16 deletions src/main/java/org/cactoos/time/DateAsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,15 @@
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
import org.cactoos.Text;
import org.cactoos.Scalar;
import org.cactoos.text.TextEnvelope;

/**
* Formatter for {@link Date} instances.
* @since 0.27
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class DateAsText implements Text {

/**
* Internal text carrying the formatted date.
*/
private final ZonedDateTimeAsText text;
public final class DateAsText extends TextEnvelope {

/**
* Formats current time using the ISO format.
Expand Down Expand Up @@ -124,16 +120,13 @@ public DateAsText(final Date date, final String format,
* @param formatter The formatter to use.
*/
public DateAsText(final Date date, final DateTimeFormatter formatter) {
this.text = new ZonedDateTimeAsText(
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
super((Scalar<String>) () -> new ZonedDateTimeAsText(
ZonedDateTime.ofInstant(
date.toInstant(),
ZoneId.of("UTC")
),
formatter
);
).asString());
}

@Override
public String asString() {
return this.text.asString();
}

}

25 changes: 6 additions & 19 deletions src/main/java/org/cactoos/time/LocalDateAsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,15 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.cactoos.Text;
import org.cactoos.scalar.UncheckedScalar;
import org.cactoos.Scalar;
import org.cactoos.text.TextEnvelope;

/**
* Formatter for {@link java.time.LocalDate} instances.
* @since 0.27
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class LocalDateAsText implements Text {

/**
* Scalar carrying the formatted date.
*/
private final UncheckedScalar<String> formatted;
public final class LocalDateAsText extends TextEnvelope {

/**
* Formats date using ISO date time format.
Expand Down Expand Up @@ -80,16 +75,8 @@ public LocalDateAsText(final LocalDate date, final String format,
*/
public LocalDateAsText(final LocalDate date,
final DateTimeFormatter formatter) {
this.formatted = new UncheckedScalar<>(
() -> formatter.format(
ZonedDateTime.of(date, LocalTime.MIN, ZoneId.systemDefault())
)
);
super((Scalar<String>) () -> formatter.format(
ZonedDateTime.of(date, LocalTime.MIN, ZoneId.systemDefault())
));
}

@Override
public String asString() {
return this.formatted.value();
}

}
22 changes: 6 additions & 16 deletions src/main/java/org/cactoos/time/LocalDateTimeAsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,15 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.cactoos.Text;
import org.cactoos.scalar.UncheckedScalar;
import org.cactoos.Scalar;
import org.cactoos.text.TextEnvelope;

/**
* Formatter for {@link java.time.LocalDateTime} instances.
* @since 0.27
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class LocalDateTimeAsText implements Text {

/**
* Scalar carrying the formatted date.
*/
private final UncheckedScalar<String> formatted;
public final class LocalDateTimeAsText extends TextEnvelope {

/**
* Formats date using ISO date time format.
Expand Down Expand Up @@ -78,14 +73,9 @@ public LocalDateTimeAsText(final LocalDateTime date, final String format,
*/
public LocalDateTimeAsText(final LocalDateTime date,
final DateTimeFormatter formatter) {
this.formatted = new UncheckedScalar<>(
() -> formatter.format(date.atZone(ZoneId.systemDefault()))
super((Scalar<String>) () -> formatter.format(
date.atZone(ZoneId.systemDefault())
)
);
}

@Override
public String asString() {
return this.formatted.value();
}

}
21 changes: 4 additions & 17 deletions src/main/java/org/cactoos/time/OffsetDateTimeAsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,15 @@
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.cactoos.Text;
import org.cactoos.scalar.UncheckedScalar;
import org.cactoos.Scalar;
import org.cactoos.text.TextEnvelope;

/**
* Formatter for {@link java.time.OffsetDateTime} instances.
* @since 0.27
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class OffsetDateTimeAsText implements Text {

/**
* Scalar carrying the formatted date.
*/
private final UncheckedScalar<String> formatted;
public final class OffsetDateTimeAsText extends TextEnvelope {

/**
* Formats date using ISO date time format.
Expand Down Expand Up @@ -78,14 +73,6 @@ public OffsetDateTimeAsText(final OffsetDateTime date, final String format,
*/
public OffsetDateTimeAsText(final OffsetDateTime date,
final DateTimeFormatter formatter) {
this.formatted = new UncheckedScalar<>(
() -> formatter.format(date)
);
super((Scalar<String>) () -> formatter.format(date));
}

@Override
public String asString() {
return this.formatted.value();
}

}
21 changes: 4 additions & 17 deletions src/main/java/org/cactoos/time/ZonedDateTimeAsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,15 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.cactoos.Text;
import org.cactoos.scalar.UncheckedScalar;
import org.cactoos.Scalar;
import org.cactoos.text.TextEnvelope;

/**
* Formatter for {@link java.time.ZonedDateTime} instances.
* @since 0.27
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public final class ZonedDateTimeAsText implements Text {

/**
* Scalar carrying the formatted date.
*/
private final UncheckedScalar<String> formatted;
public final class ZonedDateTimeAsText extends TextEnvelope {

/**
* Formats date using ISO date time format.
Expand Down Expand Up @@ -77,14 +72,6 @@ public ZonedDateTimeAsText(final ZonedDateTime date, final String format,
*/
public ZonedDateTimeAsText(final ZonedDateTime date,
final DateTimeFormatter formatter) {
this.formatted = new UncheckedScalar<>(
() -> formatter.format(date)
);
super((Scalar<String>) () -> formatter.format(date));
}

@Override
public String asString() {
return this.formatted.value();
}

}

0 comments on commit 4348d7e

Please sign in to comment.