-
Notifications
You must be signed in to change notification settings - Fork 337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support of conversion to java.time classes #593
Open
Chaosfirebolt
wants to merge
1
commit into
cbeust:master
Choose a base branch
from
Chaosfirebolt:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
src/main/java/com/beust/jcommander/converters/InstantConverter.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,30 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.time.Instant; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
/** | ||
* Converter to {@link Instant}. | ||
*/ | ||
public final class InstantConverter extends JavaTimeConverter<Instant> { | ||
|
||
public InstantConverter(String optionName) { | ||
super(optionName, Instant.class); | ||
} | ||
|
||
@Override | ||
protected List<DateTimeFormatter> supportedFormats() { | ||
return List.of(DateTimeFormatter.ISO_INSTANT); | ||
} | ||
|
||
@Override | ||
protected Instant parse(String value, DateTimeFormatter formatter) { | ||
try { | ||
long ms = Long.parseLong(value); | ||
return Instant.ofEpochMilli(ms); | ||
} catch (NumberFormatException e) { | ||
return formatter.parse(value, Instant::from); | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/beust/jcommander/converters/JavaTimeConverter.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,80 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import com.beust.jcommander.ParameterException; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.time.temporal.TemporalAccessor; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Base class for all {@link java.time} converters. | ||
* | ||
* @param <T> concrete type to parse into | ||
*/ | ||
public abstract class JavaTimeConverter<T extends TemporalAccessor> extends BaseConverter<T> { | ||
|
||
private final Class<T> toClass; | ||
|
||
/** | ||
* Inheritor constructors should have only 1 parameter - optionName. | ||
* | ||
* @param optionName name of the option | ||
* @param toClass type to parse into | ||
*/ | ||
protected JavaTimeConverter(String optionName, Class<T> toClass) { | ||
super(optionName); | ||
this.toClass = toClass; | ||
} | ||
|
||
@Override | ||
public final T convert(String value) { | ||
return supportedFormats().stream() | ||
.map(new Mapper(value)) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElseThrow(() -> new ParameterException(errorMessage(value))); | ||
} | ||
|
||
/** | ||
* Supported formats for this type, e.g. {@code HH:mm:ss} | ||
* | ||
* @return list of supported formats | ||
*/ | ||
protected abstract List<DateTimeFormatter> supportedFormats(); | ||
|
||
/** | ||
* Parse the value using the specified formatter. | ||
* | ||
* @param value value to parse | ||
* @param formatter formatter specifying supported format | ||
* @return parsed value | ||
*/ | ||
protected abstract T parse(String value, DateTimeFormatter formatter); | ||
|
||
private String errorMessage(String value) { | ||
return getErrorString(value, "a " + toClass.getSimpleName()); | ||
} | ||
|
||
private final class Mapper implements Function<DateTimeFormatter, T> { | ||
|
||
private final String value; | ||
|
||
private Mapper(String value) { | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public T apply(DateTimeFormatter formatter) { | ||
try { | ||
return parse(value, formatter); | ||
} catch (DateTimeParseException exc) { | ||
return null; | ||
} catch (Exception exc) { | ||
throw new ParameterException(errorMessage(value), exc); | ||
} | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/beust/jcommander/converters/LocalDateConverter.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,25 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
/** | ||
* Converter to {@link LocalDate}. | ||
*/ | ||
public class LocalDateConverter extends JavaTimeConverter<LocalDate> { | ||
|
||
public LocalDateConverter(String optionName) { | ||
super(optionName, LocalDate.class); | ||
} | ||
|
||
@Override | ||
protected List<DateTimeFormatter> supportedFormats() { | ||
return List.of(DateTimeFormatter.ISO_LOCAL_DATE, DateTimeFormatter.ofPattern("dd-MM-yyyy")); | ||
} | ||
|
||
@Override | ||
protected LocalDate parse(String value, DateTimeFormatter formatter) { | ||
return LocalDate.parse(value, formatter); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/beust/jcommander/converters/LocalDateTimeConverter.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,29 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
import java.util.List; | ||
|
||
/** | ||
* Converter for {@link LocalDateTime}. | ||
*/ | ||
public class LocalDateTimeConverter extends JavaTimeConverter<LocalDateTime> { | ||
|
||
public LocalDateTimeConverter(String optionName) { | ||
super(optionName, LocalDateTime.class); | ||
} | ||
|
||
@Override | ||
protected List<DateTimeFormatter> supportedFormats() { | ||
return List.of( | ||
DateTimeFormatter.ISO_LOCAL_DATE_TIME, | ||
new DateTimeFormatterBuilder().appendPattern("dd-MM-yyyy").appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter() | ||
); | ||
} | ||
|
||
@Override | ||
protected LocalDateTime parse(String value, DateTimeFormatter formatter) { | ||
return LocalDateTime.parse(value, formatter); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/beust/jcommander/converters/LocalTimeConverter.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,25 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.time.LocalTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
/** | ||
* Converter for {@link LocalTime}. | ||
*/ | ||
public class LocalTimeConverter extends JavaTimeConverter<LocalTime> { | ||
|
||
public LocalTimeConverter(String optionName) { | ||
super(optionName, LocalTime.class); | ||
} | ||
|
||
@Override | ||
protected List<DateTimeFormatter> supportedFormats() { | ||
return List.of(DateTimeFormatter.ISO_LOCAL_TIME); | ||
} | ||
|
||
@Override | ||
protected LocalTime parse(String value, DateTimeFormatter formatter) { | ||
return LocalTime.parse(value, formatter); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/beust/jcommander/converters/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,25 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
/** | ||
* Converter for {@link OffsetDateTime}. | ||
*/ | ||
public class OffsetDateTimeConverter extends JavaTimeConverter<OffsetDateTime> { | ||
|
||
public OffsetDateTimeConverter(String optionName) { | ||
super(optionName, OffsetDateTime.class); | ||
} | ||
|
||
@Override | ||
protected List<DateTimeFormatter> supportedFormats() { | ||
return List.of(DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
} | ||
|
||
@Override | ||
protected OffsetDateTime parse(String value, DateTimeFormatter formatter) { | ||
return OffsetDateTime.parse(value, formatter); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/beust/jcommander/converters/OffsetTimeConverter.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,25 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.time.OffsetTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
/** | ||
* Converter for {@link OffsetTime}. | ||
*/ | ||
public class OffsetTimeConverter extends JavaTimeConverter<OffsetTime> { | ||
|
||
public OffsetTimeConverter(String optionName) { | ||
super(optionName, OffsetTime.class); | ||
} | ||
|
||
@Override | ||
protected List<DateTimeFormatter> supportedFormats() { | ||
return List.of(DateTimeFormatter.ISO_OFFSET_TIME); | ||
} | ||
|
||
@Override | ||
protected OffsetTime parse(String value, DateTimeFormatter formatter) { | ||
return OffsetTime.parse(value, formatter); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/beust/jcommander/converters/ZonedDateTimeConverter.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,25 @@ | ||
package com.beust.jcommander.converters; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
/** | ||
* Converter for {@link ZonedDateTime}. | ||
*/ | ||
public class ZonedDateTimeConverter extends JavaTimeConverter<ZonedDateTime> { | ||
|
||
public ZonedDateTimeConverter(String optionName) { | ||
super(optionName, ZonedDateTime.class); | ||
} | ||
|
||
@Override | ||
protected List<DateTimeFormatter> supportedFormats() { | ||
return List.of(DateTimeFormatter.ISO_ZONED_DATE_TIME); | ||
} | ||
|
||
@Override | ||
protected ZonedDateTime parse(String value, DateTimeFormatter formatter) { | ||
return ZonedDateTime.parse(value, formatter); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#ISO_DATE_TIME the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really.
ISO_DATE_TIME
has the same date format asISO_LOCAL_DATE_TIME
-year-month-day
, but it can handle offset and zone id, if they are present. The extra pattern I wrote has the date part inday-month-year
format, which is also widely used as far as I know.How would you like to handle that one? I would leave it as is, as it follows more strictly a local date time format (no offset/zone information), but I don't really insist on that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mkarg What are your thoughts on my previous comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will share them as soon as I have the time to.