Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yegor256/cactoos
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 21, 2017
2 parents 8212a77 + b05ba99 commit 28a2560
Show file tree
Hide file tree
Showing 12 changed files with 1,115 additions and 0 deletions.
189 changes: 189 additions & 0 deletions src/main/java/org/cactoos/time/DateAsText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* 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.io.IOException;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
import java.util.Locale;
import org.cactoos.Text;
import org.cactoos.scalar.UncheckedScalar;
import org.cactoos.text.UncheckedText;

/**
* Formatter for date instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class DateAsText implements Text {
/**
* Scalar carrying the formatted date.
*/
private final UncheckedScalar<String> formatted;

/**
* Formats the milliseconds using the ISO format.
* @param milliseconds Milliseconds to format.
*/
public DateAsText(final long milliseconds) {
this(
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC")
),
DateTimeFormatter.ISO_DATE_TIME
);
}

/**
* Formats the milliseconds using the format and the default locale.
* @param milliseconds Milliseconds to format.
* @param format The format to use.
*/
public DateAsText(final long milliseconds, final String format) {
this(
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC")
),
format,
Locale.getDefault(Locale.Category.FORMAT)
);
}

/**
* Formats the milliseconds as date using the format and the locale.
* @param milliseconds Milliseconds to format as date.
* @param format The format to use.
* @param locale The locale to use for the format.
*/
public DateAsText(final long milliseconds, final String format,
final Locale locale) {
this(
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC")
),
DateTimeFormatter.ofPattern(format, locale)
);
}

/**
* Formats the date with ISO format using the system zone.
* @param date The date to format.
*/
public DateAsText(final Date date) {
this(
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
DateTimeFormatter.ISO_DATE_TIME
);
}

/**
* Formats the date with to format using the default locale and the system
* zone.
* @param date The date to format.
* @param format The format to use.
*/
public DateAsText(final Date date, final String format) {
this(
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
DateTimeFormatter.ofPattern(
format,
Locale.getDefault(Locale.Category.FORMAT)
)
);
}

/**
* Formats the date using the format and locale using the system default
* zone.
* @param date The date to format.
* @param format The format to use.
* @param locale The locale to use.
*/
public DateAsText(final Date date, final String format,
final Locale locale) {
this(
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
DateTimeFormatter.ofPattern(format, locale)
);
}

/**
* Formats date using ISO date time format.
* @param date The date to format.
*/
public DateAsText(final TemporalAccessor date) {
this(date, DateTimeFormatter.ISO_DATE_TIME);
}

/**
* Formats date using provided date time format string using default locale.
* @param date The date to format.
* @param format The format to use.
*/
public DateAsText(final TemporalAccessor 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.
*/
public DateAsText(final TemporalAccessor 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.
*/
public DateAsText(final TemporalAccessor date,
final DateTimeFormatter formatter) {
this.formatted = new UncheckedScalar<>(
() -> formatter.format(date)
);
}

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

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

}

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

/**
* Parser for {@link Date} instances.
* @author Sven Diedrichsen (sven.diedrichsen@gmail.com)
* @version $Id$
* @since 1.0
*/
public class DateOf implements Scalar<Date> {

/**
* The parsed date.
*/
private final Scalar<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);
}

/**
* 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) {
this(date, DateTimeFormatter.ofPattern(format));
}

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

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

}
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 28a2560

Please sign in to comment.