-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from Gmugra/134_API_add_java_time_OffsetDateTime
#134 : API: add default support for java.time.OffsetDateTime
- Loading branch information
Showing
6 changed files
with
162 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
core/src/main/java/net/cactusthorn/config/core/converter/OffsetDateTimeParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ""; | ||
} |
52 changes: 52 additions & 0 deletions
52
...src/main/java/net/cactusthorn/config/core/converter/standard/OffsetDateTimeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...test/java/net/cactusthorn/config/core/converter/standard/OffsetDateTimeConverterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters