Skip to content

Commit

Permalink
#504 date package polished
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 25, 2017
1 parent 28a2560 commit d5d59d2
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 113 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ The MIT License (MIT)
<limit>
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.66</minimum>
<minimum>0.65</minimum>
</limit>
<limit>
<counter>COMPLEXITY</counter>
Expand Down
23 changes: 15 additions & 8 deletions src/main/java/org/cactoos/time/DateAsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package org.cactoos.time;

import java.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
Expand All @@ -39,15 +38,23 @@
* Formatter for date instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
* @since 0.27
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class DateAsText implements Text {
public final class DateAsText implements Text {

/**
* Scalar carrying the formatted date.
*/
private final UncheckedScalar<String> formatted;

/**
* Formats current time using the ISO format.
*/
public DateAsText() {
this(System.currentTimeMillis());
}

/**
* Formats the milliseconds using the ISO format.
* @param milliseconds Milliseconds to format.
Expand All @@ -57,7 +64,7 @@ public DateAsText(final long milliseconds) {
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC")
),
DateTimeFormatter.ISO_DATE_TIME
new Iso().get()
);
}

Expand Down Expand Up @@ -99,7 +106,7 @@ public DateAsText(final long milliseconds, final String format,
public DateAsText(final Date date) {
this(
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
DateTimeFormatter.ISO_DATE_TIME
new Iso().get()
);
}

Expand Down Expand Up @@ -139,7 +146,7 @@ public DateAsText(final Date date, final String format,
* @param date The date to format.
*/
public DateAsText(final TemporalAccessor date) {
this(date, DateTimeFormatter.ISO_DATE_TIME);
this(date, new Iso().get());
}

/**
Expand Down Expand Up @@ -176,12 +183,12 @@ public DateAsText(final TemporalAccessor date,
}

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

@Override
public final int compareTo(final Text text) {
public int compareTo(final Text text) {
return new UncheckedText(this).compareTo(text);
}

Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/cactoos/time/DateOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@
* Parser for {@link Date} instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
* @since 0.27
*/
public class DateOf implements Scalar<Date> {
public final class DateOf implements Scalar<Date> {

/**
* The parsed date.
*/
private final Scalar<Date> parsed;
private final UncheckedScalar<Date> parsed;

/**
* Parses the provided date as ISO formatted.
* @param date The date to parse.
*/
public DateOf(final String date) {
this(date, DateTimeFormatter.ISO_DATE_TIME);
public DateOf(final CharSequence date) {
this(date, new Iso().get());
}

/**
* Parses the date using the provided format.
* @param date The date to parse.
* @param format The format to use.
*/
public DateOf(final String date, final String format) {
public DateOf(final CharSequence date, final String format) {
this(date, DateTimeFormatter.ofPattern(format));
}

Expand All @@ -65,17 +65,18 @@ public DateOf(final String date, final String format) {
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public DateOf(final String date, final DateTimeFormatter formatter) {
public DateOf(final CharSequence date, final DateTimeFormatter formatter) {
this.parsed = new UncheckedScalar<>(
() -> Date.from(
LocalDateTime.from(formatter.parse(date))
.toInstant(ZoneOffset.UTC)
LocalDateTime.from(
formatter.parse(date)
).toInstant(ZoneOffset.UTC)
)
);
}

@Override
public final Date value() throws Exception {
public Date value() {
return this.parsed.value();
}

Expand Down
46 changes: 46 additions & 0 deletions src/main/java/org/cactoos/time/Iso.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cactoos.time;

import java.time.format.DateTimeFormatter;

/**
* The formatter.
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.27
*/
final class Iso {

/**
* Take it.
* @return Formatter
*/
public DateTimeFormatter get() {
assert this != null;
return DateTimeFormatter.ISO_OFFSET_DATE_TIME;
}

}

14 changes: 7 additions & 7 deletions src/main/java/org/cactoos/time/LocalDateTimeOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
* Parser for {@link LocalDateTime} instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
* @since 0.27
*/
public class LocalDateTimeOf implements Scalar<LocalDateTime> {
public final class LocalDateTimeOf implements Scalar<LocalDateTime> {
/**
* The parsed date.
*/
Expand All @@ -44,8 +44,8 @@ public class LocalDateTimeOf implements Scalar<LocalDateTime> {
* Parses ISO date to create {@link LocalDateTime} instances.
* @param date The date to parse.
*/
public LocalDateTimeOf(final String date) {
this(date, DateTimeFormatter.ISO_DATE_TIME);
public LocalDateTimeOf(final CharSequence date) {
this(date, new Iso().get());
}

/**
Expand All @@ -54,7 +54,7 @@ public LocalDateTimeOf(final String date) {
* @param date The date to parse.
* @param format The format to use.
*/
public LocalDateTimeOf(final String date, final String format) {
public LocalDateTimeOf(final CharSequence date, final String format) {
this(date, DateTimeFormatter.ofPattern(format));
}

Expand All @@ -64,15 +64,15 @@ public LocalDateTimeOf(final String date, final String format) {
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public LocalDateTimeOf(final String date,
public LocalDateTimeOf(final CharSequence date,
final DateTimeFormatter formatter) {
this.parsed = new UncheckedScalar<>(
() -> LocalDateTime.from(formatter.parse(date))
);
}

@Override
public final LocalDateTime value() throws Exception {
public LocalDateTime value() {
return this.parsed.value();
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/cactoos/time/OffsetDateTimeOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
* Parser for {@link OffsetDateTime} instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
* @since 0.27
*/
public class OffsetDateTimeOf implements Scalar<OffsetDateTime> {
public final class OffsetDateTimeOf implements Scalar<OffsetDateTime> {
/**
* The parsed date.
*/
Expand All @@ -46,8 +46,8 @@ public class OffsetDateTimeOf implements Scalar<OffsetDateTime> {
* Parses ISO date to create {@link OffsetDateTime} instances.
* @param date The date to parse.
*/
public OffsetDateTimeOf(final String date) {
this(date, DateTimeFormatter.ISO_DATE_TIME);
public OffsetDateTimeOf(final CharSequence date) {
this(date, new Iso().get());
}

/**
Expand All @@ -57,7 +57,7 @@ public OffsetDateTimeOf(final String date) {
* @param format The format to use.
* @param offset The offset to use.
*/
public OffsetDateTimeOf(final String date, final String format,
public OffsetDateTimeOf(final CharSequence date, final String format,
final ZoneOffset offset) {
this(date,
DateTimeFormatter.ofPattern(format).withZone(offset.normalized())
Expand All @@ -70,15 +70,15 @@ public OffsetDateTimeOf(final String date, final String format,
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public OffsetDateTimeOf(final String date,
public OffsetDateTimeOf(final CharSequence date,
final DateTimeFormatter formatter) {
this.parsed = new UncheckedScalar<>(
() -> ZonedDateTime.from(formatter.parse(date)).toOffsetDateTime()
);
}

@Override
public final OffsetDateTime value() throws Exception {
public OffsetDateTime value() {
return this.parsed.value();
}

Expand Down
18 changes: 8 additions & 10 deletions src/main/java/org/cactoos/time/ZonedDateTimeOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
* Parser for {@link ZonedDateTime} instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
* @since 0.27
*/
public class ZonedDateTimeOf implements Scalar<ZonedDateTime> {
public final class ZonedDateTimeOf implements Scalar<ZonedDateTime> {
/**
* The parsed date.
*/
Expand All @@ -45,8 +45,8 @@ public class ZonedDateTimeOf implements Scalar<ZonedDateTime> {
* Parses date to create {@link ZonedDateTime} instances.
* @param date The date to parse.
*/
public ZonedDateTimeOf(final String date) {
this(date, DateTimeFormatter.ISO_DATE_TIME);
public ZonedDateTimeOf(final CharSequence date) {
this(date, new Iso().get());
}

/**
Expand All @@ -56,11 +56,9 @@ public ZonedDateTimeOf(final String date) {
* @param format The format to use.
* @param zone The zone to use.
*/
public ZonedDateTimeOf(final String date, final String format,
public ZonedDateTimeOf(final CharSequence date, final String format,
final ZoneId zone) {
this(date,
DateTimeFormatter.ofPattern(format).withZone(zone)
);
this(date, DateTimeFormatter.ofPattern(format).withZone(zone));
}

/**
Expand All @@ -69,15 +67,15 @@ public ZonedDateTimeOf(final String date, final String format,
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public ZonedDateTimeOf(final String date,
public ZonedDateTimeOf(final CharSequence date,
final DateTimeFormatter formatter) {
this.parsed = new UncheckedScalar<>(
() -> ZonedDateTime.from(formatter.parse(date))
);
}

@Override
public final ZonedDateTime value() throws Exception {
public ZonedDateTime value() {
return this.parsed.value();
}

Expand Down
Loading

1 comment on commit d5d59d2

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on d5d59d2 Dec 25, 2017

Choose a reason for hiding this comment

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

Puzzle 504-de6e0b21 discovered in src/test/java/org/cactoos/time/ZonedDateTimeOfTest.java and submitted as #505. 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.