Skip to content

Commit

Permalink
Merge pull request #139 from Gmugra/134_API_add_java_time_OffsetDateTime
Browse files Browse the repository at this point in the history
#134 : API: add default support for java.time.OffsetDateTime
  • Loading branch information
Gmugra authored May 22, 2021
2 parents 5f98e5c + 05db617 commit aa9b6ee
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ app.date=12.11.2005
1. `@ConverterClass`
- `@Target({METHOD, ANNOTATION_TYPE})`
- apply custom converter implementation
1. `@LocalDateParser`, `@LocalDateTimeParser`, `@ZonedDateTimeParser`
1. `@LocalDateParser`, `@LocalDateTimeParser`, `@ZonedDateTimeParser`, `@OffsetDateTimeParser`
- `@Target(METHOD)`
- apply a parameterized by formats converter to the relevant java.time.* type

Expand Down Expand Up @@ -234,6 +234,7 @@ The return type of the interface methods must either:
- `java.time.LocalDate`
- `java.time.LocalDateTime`
- `java.time.ZonedDateTime`
- `java.time.OffsetDateTime`
- `net.cactusthorn.config.core.converter.bytesize.ByteSize`
1. Be `List<T>`, `Set<T>` or `SortedSet<T>`, where **T** satisfies 2, 3 or 4 above. The resulting collection is read-only.
1. Be `Map<K,V>` or `SortedMap<K,V>`, where
Expand Down Expand Up @@ -381,6 +382,7 @@ Several of these annotations shipped with the library:
* `@LocalDateParser`
* `@LocalDateTimeParser`
* `@ZonedDateTimeParser`
* `@OffsetDateTimeParser`

## Loaders

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2021, Alexei Khatskevich
*
* Licensed under the BSD 3-Clause license.
* You may obtain a copy of the License at
*
* https://github.com/Gmugra/net.cactusthorn.config/blob/main/LICENSE
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.core.converter;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.SOURCE;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import net.cactusthorn.config.core.converter.standard.OffsetDateTimeConverter;

@Retention(SOURCE) @Target(METHOD) @ConverterClass(OffsetDateTimeConverter.class) public @interface OffsetDateTimeParser {
String[] value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2021, Alexei Khatskevich
*
* Licensed under the BSD 3-Clause license.
* You may obtain a copy of the License at
*
* https://github.com/Gmugra/net.cactusthorn.config/blob/main/LICENSE
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.core.converter.standard;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

import net.cactusthorn.config.core.converter.Converter;

public class OffsetDateTimeConverter implements Converter<OffsetDateTime> {

@Override public OffsetDateTime convert(String value, String[] parameters) {
if (parameters == null || parameters.length == 0 || parameters.length == 1 && "".equals(parameters[0])) {
return OffsetDateTime.parse(value);
}
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
for (String parameter : parameters) {
builder.appendPattern('[' + parameter + ']');
}
// @formatter:off
DateTimeFormatter formatter =
builder.parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
.parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.parseDefaulting(ChronoField.MICRO_OF_SECOND, 0)
.parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
.toFormatter();
// @formatter:on
return OffsetDateTime.parse(value, formatter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2021, Alexei Khatskevich
*
* Licensed under the BSD 3-Clause license.
* You may obtain a copy of the License at
*
* https://github.com/Gmugra/net.cactusthorn.config/blob/main/LICENSE
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.cactusthorn.config.core.converter.standard;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

import org.junit.jupiter.api.Test;

import net.cactusthorn.config.core.converter.Converter;

public class OffsetDateTimeConverterTest {

static final OffsetDateTime DATETIME = OffsetDateTime.of(2011, 11, 12, 1, 30, 0, 0, ZoneOffset.of("+02:00"));
static final Converter<OffsetDateTime> CONVERTER = new OffsetDateTimeConverter();

@Test public void simple() {
OffsetDateTime result = CONVERTER.convert("2011-11-12T01:30:00+02:00");
assertEquals(DATETIME, result);
}

@Test public void simpleEmptyParameters() {
OffsetDateTime result = CONVERTER.convert("2011-11-12T01:30:00+02:00", new String[0]);
assertEquals(DATETIME, result);
}

@Test public void simpleNullParameter() {
OffsetDateTime result = CONVERTER.convert("2011-11-12T01:30:00+02:00", null);
assertEquals(DATETIME, result);
}

@Test public void complex() {
OffsetDateTime result = CONVERTER.convert("12.11.2011T01:30:00+02:00",
new String[] { "yyyy-MM-dd'T'HH:mm:ssXXX", "dd.MM.yyyy'T'HH:mm:ssXXX" });
assertEquals(DATETIME, result);
}

@Test public void singleParam() {
OffsetDateTime result = CONVERTER.convert("12.11.2011T01:30:00+02:00", new String[] { "dd.MM.yyyy'T'HH:mm:ssXXX" });
assertEquals(DATETIME, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.util.Optional;

import net.cactusthorn.config.core.Config;
import net.cactusthorn.config.core.converter.LocalDateParser;
import net.cactusthorn.config.core.converter.LocalDateTimeParser;
import net.cactusthorn.config.core.converter.OffsetDateTimeParser;
import net.cactusthorn.config.core.converter.ZonedDateTimeParser;

@Config public interface ConfigParamConverter {
Expand All @@ -37,5 +39,7 @@

@ZonedDateTimeParser({"dd.MM.yyyy' 'HH:mm:sszzz"}) Optional<ZonedDateTime> zonedDateTime();

@OffsetDateTimeParser({ "yyyy-MM-dd'T'HH:mm:ssXXX", "dd.MM.yyyy'T'HH:mm:ssXXX" }) Optional<OffsetDateTime> offsetDateTime();

Optional<LocalDate> localDateDefault();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -62,4 +64,12 @@ public class ConfigParamConverterTest {
ConfigParamConverter config = ConfigFactory.builder().setSource(properties).build().create(ConfigParamConverter.class);
assertEquals(LocalDate.of(2011, 11, 12), config.localDateDefault().get());
}

@Test public void offsetDateTime() {
Map<String, String> properties = new HashMap<>();
properties.put("localDate", "12.11.2011");
properties.put("offsetDateTime", "12.11.2011T01:30:00+02:00");
ConfigParamConverter config = ConfigFactory.builder().setSource(properties).build().create(ConfigParamConverter.class);
assertEquals(OffsetDateTime.of(2011, 11, 12, 1, 30, 0, 0, ZoneOffset.of("+02:00")), config.offsetDateTime().get());
}
}

0 comments on commit aa9b6ee

Please sign in to comment.