dev.orne.beans.converters.ZoneOffsetConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of beans Show documentation
Show all versions of beans Show documentation
Orne bean development utilities
package dev.orne.beans.converters;
/*-
* #%L
* Orne Beans
* %%
* Copyright (C) 2020 Orne Developments
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.TemporalAccessor;
import javax.validation.constraints.NotNull;
/**
* Implementation of {@code Converter} that converts {@code ZoneOffset} instances
* to and from {@code String} using ID as {@code String} representation by default.
*
* @author (w) Iker Hernaez
* @version 1.0, 2020-05
* @since 0.1
*/
public class ZoneOffsetConverter
extends AbstractDateTimeConverter {
/**
* The zone offset by id parser.
*/
public static final DateTimeFormatter BY_ID_PARSER =
new DateTimeFormatterBuilder()
.appendOffsetId()
.optionalStart()
.appendLiteral('[')
.parseCaseSensitive()
.appendZoneRegionId()
.appendLiteral(']')
.toFormatter();
/**
* Creates a new instance that throws a {@code ConversionException} if an
* error occurs, using ISO-8601 as default conversion format.
*/
public ZoneOffsetConverter() {
super(BY_ID_PARSER);
setDefaultParsers();
}
/**
* Creates a new instance that returns a default value if an error occurs,
* using ISO-8601 as default conversion format.
*
* @param defaultValue The default value to be returned if the value to be
* converted is missing or an error occurs converting the value
*/
public ZoneOffsetConverter(
final ZoneOffset defaultValue) {
super(BY_ID_PARSER, defaultValue);
setDefaultParsers();
}
/**
* Creates a new instance that throws a {@code ConversionException} if an
* error occurs.
*
* @param formatter The temporal value formatter and default parser
*/
public ZoneOffsetConverter(
final @NotNull DateTimeFormatter formatter) {
super(formatter);
setDefaultParsers();
}
/**
* Creates a new instance that returns a default value if an error occurs.
*
* @param formatter The temporal value formatter and default parser
* @param defaultValue The default value to be returned if the value to be
* converted is missing or an error occurs converting the value
*/
public ZoneOffsetConverter(
final @NotNull DateTimeFormatter formatter,
final ZoneOffset defaultValue) {
super(formatter, defaultValue);
setDefaultParsers();
}
/**
* Sets the default parsers for this converter.
*/
private final void setDefaultParsers() {
setParsers(
BY_ID_PARSER,
DateTimeFormatter.ISO_ZONED_DATE_TIME,
DateTimeFormatter.ISO_OFFSET_DATE_TIME,
DateTimeFormatter.ISO_OFFSET_DATE,
DateTimeFormatter.ISO_OFFSET_TIME);
}
/**
* {@inheritDoc}
*/
@Override
protected @NotNull Class> getDefaultType() {
return ZoneOffset.class;
}
/**
* {@inheritDoc}
*/
@Override
protected T fromTemporalAccessor(
final @NotNull Class type,
final @NotNull TemporalAccessor value) {
return type.cast(ZoneOffset.from(value));
}
}