-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support pattern for duration serializer (#194)
* Extract duration unit converter to util package. ref #189 * Support unit pattern in duration serializer. ref #189
- Loading branch information
1 parent
d04e695
commit e404c02
Showing
6 changed files
with
358 additions
and
81 deletions.
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
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
82 changes: 82 additions & 0 deletions
82
datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/util/DurationUnitConverter.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,82 @@ | ||
package com.fasterxml.jackson.datatype.jsr310.util; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.time.temporal.TemporalUnit; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.fasterxml.jackson.datatype.jsr310.util.DurationUnitConverter.DurationSerialization.deserializer; | ||
|
||
/** | ||
* Handles the conversion of the duration based on the API of {@link Duration} for a restricted set of {@link ChronoUnit}. | ||
* Only the units considered as accurate are supported in this converter since are the only ones capable of handling | ||
* deserialization in a precise manner (see {@link ChronoUnit#isDurationEstimated}). | ||
* | ||
* @since 2.12 | ||
*/ | ||
public class DurationUnitConverter { | ||
|
||
protected static class DurationSerialization { | ||
final Function<Duration, Long> serializer; | ||
final Function<Long, Duration> deserializer; | ||
|
||
DurationSerialization( | ||
Function<Duration, Long> serializer, | ||
Function<Long, Duration> deserializer) { | ||
this.serializer = serializer; | ||
this.deserializer = deserializer; | ||
} | ||
|
||
static Function<Long, Duration> deserializer(TemporalUnit unit) { | ||
return v -> Duration.of(v, unit); | ||
} | ||
} | ||
|
||
private final static Map<String, DurationSerialization> UNITS; | ||
|
||
static { | ||
Map<String, DurationSerialization> units = new LinkedHashMap<>(); | ||
units.put(ChronoUnit.NANOS.name(), new DurationSerialization(Duration::toNanos, deserializer(ChronoUnit.NANOS))); | ||
units.put(ChronoUnit.MICROS.name(), new DurationSerialization(d -> d.toNanos() / 1000, deserializer(ChronoUnit.MICROS))); | ||
units.put(ChronoUnit.MILLIS.name(), new DurationSerialization(Duration::toMillis, deserializer(ChronoUnit.MILLIS))); | ||
units.put(ChronoUnit.SECONDS.name(), new DurationSerialization(Duration::getSeconds, deserializer(ChronoUnit.SECONDS))); | ||
units.put(ChronoUnit.MINUTES.name(), new DurationSerialization(Duration::toMinutes, deserializer(ChronoUnit.MINUTES))); | ||
units.put(ChronoUnit.HOURS.name(), new DurationSerialization(Duration::toHours, deserializer(ChronoUnit.HOURS))); | ||
units.put(ChronoUnit.HALF_DAYS.name(), new DurationSerialization(d -> d.toHours() / 12, deserializer(ChronoUnit.HALF_DAYS))); | ||
units.put(ChronoUnit.DAYS.name(), new DurationSerialization(Duration::toDays, deserializer(ChronoUnit.DAYS))); | ||
UNITS = units; | ||
} | ||
|
||
|
||
final DurationSerialization serialization; | ||
|
||
DurationUnitConverter(DurationSerialization serialization) { | ||
this.serialization = serialization; | ||
} | ||
|
||
public Duration convert(long value) { | ||
return serialization.deserializer.apply(value); | ||
} | ||
|
||
public long convert(Duration duration) { | ||
return serialization.serializer.apply(duration); | ||
} | ||
|
||
/** | ||
* @return Description of all allowed valued as a sequence of | ||
* double-quoted values separated by comma | ||
*/ | ||
public static String descForAllowed() { | ||
return "\"" + UNITS.keySet().stream() | ||
.collect(Collectors.joining("\", \"")) | ||
+ "\""; | ||
} | ||
|
||
public static DurationUnitConverter from(String unit) { | ||
DurationSerialization def = UNITS.get(unit); | ||
return (def == null) ? null : new DurationUnitConverter(def); | ||
} | ||
} |
Oops, something went wrong.