Skip to content

Commit

Permalink
#444 refactoring DateOf. Splitted it into several distinct classes wi…
Browse files Browse the repository at this point in the history
…thout using base class.
  • Loading branch information
svendiedrichsen committed Dec 20, 2017
1 parent 9322fe8 commit 7bfb893
Show file tree
Hide file tree
Showing 8 changed files with 529 additions and 311 deletions.
193 changes: 17 additions & 176 deletions src/main/java/org/cactoos/time/DateOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,222 +24,63 @@
package org.cactoos.time;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import org.cactoos.Func;
import java.util.Date;
import org.cactoos.Scalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* Parser for date instances.
* Parser for {@link Date} instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @param <T> Type of the date to parse to.
* @since 1.0
*/
public class DateOf<T extends TemporalAccessor> implements Scalar<T> {
public class DateOf implements Scalar<Date> {

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

/**
* Parses the date using the provided format.
* Parses the provided date as ISO formatted.
* @param date The date to parse.
* @param format The format to use.
* @param converter The converter to convert
* {@link TemporalAccessor} into type T
*/
public DateOf(final String date, final String format,
final Func<TemporalAccessor, T> converter) {
this(date, DateTimeFormatter.ofPattern(format), converter);
public DateOf(final String date) {
this(date, DateTimeFormatter.ISO_DATE_TIME);
}

/**
* Parses the provided date as ISO formatted.
* Parses the date using the provided format.
* @param date The date to parse.
* @param converter The converter to convert
* @param format The format to use.
* {@link TemporalAccessor} into type T
*/
public DateOf(final String date,
final Func<TemporalAccessor, T> converter) {
this(date, DateTimeFormatter.ISO_DATE_TIME, converter);
public DateOf(final String date, final String format) {
this(date, DateTimeFormatter.ofPattern(format));
}

/**
* Parsing the date using the provided formatter.
* @param date The date to parse.
* @param formatter The formatter to use.
* @param converter The converter to convert
* {@link TemporalAccessor} into type T
*/
public DateOf(final String date, final DateTimeFormatter formatter,
final Func<TemporalAccessor, T> converter) {
public DateOf(final String date, final DateTimeFormatter formatter) {
this.parsed = new UncheckedScalar<>(
() -> converter.apply(formatter.parse(date))
() -> Date.from(
LocalDateTime.from(formatter.parse(date))
.toInstant(ZoneOffset.UTC)
)
);
}

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

/**
* Convenience class for parsing to {@link java.util.Date} instances.
*/
public static class Date implements Scalar<java.util.Date> {
/**
* Internal parser to create {@link LocalDateTime} instance.
*/
private final Local internal;
/**
* Parses ISO date string to create {@link java.util.Date} instances.
* @param date The date to parse.
*/
public Date(final String date) {
this(new Local(date));
}
/**
* Parses date using the provided format to create
* {@link java.util.Date} instances.
* @param date The date to parse.
* @param format The format to use.
*/
public Date(final String date, final String format) {
this(new Local(date, format));
}
/**
* Parses the date using the formatter to create
* {@link java.util.Date} instances.
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public Date(final String date, final DateTimeFormatter formatter) {
this(new Local(date, formatter));
}
/**
* Private stor.
* @param local The {@link Local} to use.
*/
private Date(final Local local) {
this.internal = local;
}

@Override
public final java.util.Date value() throws Exception {
return java.util.Date.from(
this.internal.value().toInstant(ZoneOffset.UTC)
);
}
}
/**
* Convenience class for parsing to {@link LocalDateTime} instances.
*/
public static class Local extends DateOf<LocalDateTime> {
/**
* Parses date using the provided format to create
* {@link LocalDateTime} instances.
* @param date The date to parse.
* @param format The format to use.
*/
public Local(final String date, final String format) {
super(date, format, LocalDateTime::from);
}
/**
* Parses ISO date to create {@link LocalDateTime} instances.
* @param date The date to parse.
*/
public Local(final String date) {
super(date, LocalDateTime::from);
}
/**
* Parses the date using the formatter to create
* {@link LocalDateTime} instances.
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public Local(final String date, final DateTimeFormatter formatter) {
super(date, formatter, LocalDateTime::from);
}
}

/**
* Convenience class for parsing to {@link OffsetDateTime} instances.
*/
public static class Offset extends DateOf<OffsetDateTime> {
/**
* Parses date using the provided format to create
* {@link OffsetDateTime} instances.
* @param date The date to parse.
* @param format The format to use.
* @param offset The offset to use.
*/
public Offset(final String date, final String format,
final ZoneOffset offset) {
super(date,
DateTimeFormatter.ofPattern(format)
.withZone(offset.normalized()),
temporal -> ZonedDateTime.from(temporal).toOffsetDateTime()
);
}
/**
* Parses ISO date to create {@link OffsetDateTime} instances.
* @param date The date to parse.
*/
public Offset(final String date) {
super(date, OffsetDateTime::from);
}
/**
* Parses the date using the formatter to create
* {@link OffsetDateTime} instances.
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public Offset(final String date, final DateTimeFormatter formatter) {
super(date, formatter, OffsetDateTime::from);
}
}

/**
* Convenience class for parsing to {@link ZonedDateTime} instances.
*/
public static class Zoned extends DateOf<ZonedDateTime> {
/**
* Parses date to create {@link ZonedDateTime} instances.
* @param date The date to parse.
*/
public Zoned(final String date) {
super(date, ZonedDateTime::from);
}
/**
* Parses date using the provided format to create
* {@link ZonedDateTime} instances.
* @param date The date to parse.
* @param format The format to use.
* @param zone The zone to use.
*/
public Zoned(final String date, final String format,
final ZoneId zone) {
super(date,
DateTimeFormatter.ofPattern(format).withZone(zone),
ZonedDateTime::from
);
}
/**
* Parses the date using the formatter to create
* {@link ZonedDateTime} instances.
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public Zoned(final String date, final DateTimeFormatter formatter) {
super(date, formatter, ZonedDateTime::from);
}
}

}
79 changes: 79 additions & 0 deletions src/main/java/org/cactoos/time/LocalDateTimeOf.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* 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.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.cactoos.Scalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* Parser for {@link LocalDateTime} instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
*/
public class LocalDateTimeOf implements Scalar<LocalDateTime> {
/**
* The parsed date.
*/
private final UncheckedScalar<LocalDateTime> parsed;

/**
* 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);
}

/**
* Parses date using the provided format to create
* {@link LocalDateTime} instances.
* @param date The date to parse.
* @param format The format to use.
*/
public LocalDateTimeOf(final String date, final String format) {
this(date, DateTimeFormatter.ofPattern(format));
}

/**
* Parses the date using the formatter to create
* {@link LocalDateTime} instances.
* @param date The date to parse.
* @param formatter The formatter to use.
*/
public LocalDateTimeOf(final String date,
final DateTimeFormatter formatter) {
this.parsed = new UncheckedScalar<>(
() -> LocalDateTime.from(formatter.parse(date))
);
}

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

}
Loading

0 comments on commit 7bfb893

Please sign in to comment.