Skip to content

Commit

Permalink
Moved LocalDateTimeAsText functionality to TextOf ctors (yegor256#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
igor committed Jan 15, 2019
1 parent 247e34b commit e1262fd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 175 deletions.
51 changes: 51 additions & 0 deletions src/main/java/org/cactoos/text/TextOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,19 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import org.cactoos.Bytes;
import org.cactoos.Input;
import org.cactoos.Scalar;
import org.cactoos.io.BytesOf;
import org.cactoos.io.InputOf;
import org.cactoos.iterable.Mapped;
import org.cactoos.time.Iso;

/**
* TextOf
Expand Down Expand Up @@ -341,6 +348,50 @@ public TextOf(final InputStream input) {
this(new InputOf(new InputStreamReader(input, StandardCharsets.UTF_8)));
}

/**
* Formats date using ISO date time format.
* @param date The date to format.
* @since 1.0
*/
public TextOf(final LocalDate date) {
this(date, new Iso().value());
}

/**
* Formats date using provided date time format string using default locale.
* @param date The date to format.
* @param format The format to use.
* @since 1.0
*/
public TextOf(final LocalDate date, final String format) {
this(date, format, Locale.getDefault(Locale.Category.FORMAT));
}

/**
* Formats the date using the provided format string using the provided
* locale.
* @param date The date to format.
* @param format The format string to use.
* @param locale The locale to use.
* @since 1.0
*/
public TextOf(final LocalDate date, final String format,
final Locale locale) {
this(date, DateTimeFormatter.ofPattern(format, locale));
}

/**
* Formats the date using the provided formatter.
* @param date The date to format.
* @param formatter The formatter to use.
* @since 1.0
*/
public TextOf(final LocalDate date, final DateTimeFormatter formatter) {
this(() -> formatter.format(
ZonedDateTime.of(date, LocalTime.MIN, ZoneId.systemDefault())
));
}

/**
* Ctor.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/time/Iso.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* The formatter.
* @since 0.27
*/
final class Iso implements Scalar<DateTimeFormatter> {
public final class Iso implements Scalar<DateTimeFormatter> {

@Override
public DateTimeFormatter value() {
Expand Down
82 changes: 0 additions & 82 deletions src/main/java/org/cactoos/time/LocalDateAsText.java

This file was deleted.

53 changes: 53 additions & 0 deletions src/test/java/org/cactoos/text/TextOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
import java.io.InputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Locale;
import org.cactoos.io.BytesOf;
import org.cactoos.io.InputOf;
import org.hamcrest.MatcherAssert;
Expand Down Expand Up @@ -311,4 +316,52 @@ public void printsStackTraceFromArray() {
)
);
}

@Test
public void readsLocalDateFormattedWithFormatString() throws IOException {
final LocalDate date = LocalDate.of(2017, 12, 13);
MatcherAssert.assertThat(
"Can't format a LocalDate with format.",
new TextOf(date, "yyyy-MM-dd HH:mm:ss").asString(),
Matchers.is("2017-12-13 00:00:00")
);
}

@Test
public void readsLocalDateFormattedWithFormatStringWithLocale()
throws IOException {
final LocalDate date = LocalDate.of(2017, 12, 13);
MatcherAssert.assertThat(
"Can't format a LocalDate with format using locale.",
new TextOf(
date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH
).asString(),
Matchers.is("2017 déc. 13. 00.00.00")
);
}

@Test
public void readsLocalDateFormattedAsIsoDateTime() throws IOException {
final LocalDate date = LocalDate.of(2017, 12, 13);
MatcherAssert.assertThat(
"Can't format a LocalDate with default/ISO format.",
new TextOf(date).asString(),
Matchers.is(
MessageFormat.format(
"2017-12-13T00:00:00{0}",
date.atTime(LocalTime.MIN).atZone(ZoneId.systemDefault())
.getOffset().toString()
)
)
);
}

@Test
public void readsCurrentLocalDateAsText() throws IOException {
MatcherAssert.assertThat(
"Can't format a LocalDate with ISO format.",
new TextOf(LocalDate.now()).asString(),
Matchers.notNullValue()
);
}
}
92 changes: 0 additions & 92 deletions src/test/java/org/cactoos/time/LocalDateAsTextTest.java

This file was deleted.

0 comments on commit e1262fd

Please sign in to comment.