Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 27, 2019
2 parents 63cdb7b + 1d0d5e3 commit 6b4a6b9
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 176 deletions.
58 changes: 58 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,20 @@
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.scalar.StickyScalar;
import org.cactoos.time.Iso;

/**
* TextOf
Expand Down Expand Up @@ -341,6 +349,56 @@ 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(
new StickyScalar<>(
() -> 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.

3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/time/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
* Time.
*
* @since 1.0
* @todo #980:30min Classes from package {@link org.cactoos.time} which are
* @todo #1006:30min Classes from package {@link org.cactoos.time} which are
* ending with `AsText` are extending TextEnvelope. It means that they are
* instances of {@link org.cactoos.Text}. They should be merged with
* {@link org.cactoos.text.TextOf} as ctor(s).
* Tests should be moved to TextOfTest.
*/
package org.cactoos.time;
54 changes: 54 additions & 0 deletions src/test/java/org/cactoos/text/TextOfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@
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;
import org.hamcrest.Matchers;
import org.hamcrest.core.IsNot;
import org.junit.Test;
import org.llorllale.cactoos.matchers.Assertion;
import org.llorllale.cactoos.matchers.IsBlank;
import org.llorllale.cactoos.matchers.TextHasString;
import org.llorllale.cactoos.matchers.TextIs;

Expand Down Expand Up @@ -313,4 +320,51 @@ public void printsStackTraceFromArray() {
new TextHasString("org.cactoos.text.TextOfTest")
);
}

@Test
public void readsLocalDateFormattedWithFormatString() {
final LocalDate date = LocalDate.of(2017, 12, 13);
new Assertion<>(
"Can't format a LocalDate with format.",
() -> new TextOf(date, "yyyy-MM-dd HH:mm:ss"),
new TextIs("2017-12-13 00:00:00")
).affirm();
}

@Test
public void readsLocalDateFormattedWithFormatStringWithLocale() {
final LocalDate date = LocalDate.of(2017, 12, 13);
new Assertion<>(
"Can't format a LocalDate with format using locale.",
() -> new TextOf(
date, "yyyy MMM dd. HH.mm.ss", Locale.FRENCH
),
new TextIs("2017 déc. 13. 00.00.00")
).affirm();
}

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

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

This file was deleted.

2 comments on commit 6b4a6b9

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 6b4a6b9 Jan 27, 2019

Choose a reason for hiding this comment

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

Puzzle 980-a6f67b21 disappeared from src/main/java/org/cactoos/time/package-info.java, that's why I closed #1006. 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 6b4a6b9 Jan 27, 2019

Choose a reason for hiding this comment

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

Puzzle 1006-0071ae30 discovered in src/main/java/org/cactoos/time/package-info.java and submitted as #1046. 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.