@ThreadSafe @Immutable public final class DurationAttributeConverter extends Object implements AttributeConverter<Duration>
Duration
and AttributeValue
.
This stores and reads values in DynamoDB as a number, so that they can be sorted numerically as part of a sort key.
Durations are stored in the format "[-]X[.YYYYYYYYY]", where X is the number of seconds in the duration, and Y is the number of nanoseconds in the duration, left padded with zeroes to a length of 9. The Y and decimal point may be excluded for durations that are of whole seconds. The duration may be preceded by a - to indicate a negative duration.
Examples:
Duration.ofDays(1)
is stored as ItemAttributeValueMapper.fromNumber("86400")
Duration.ofSeconds(9)
is stored as ItemAttributeValueMapper.fromNumber("9")
Duration.ofSeconds(-9)
is stored as ItemAttributeValueMapper.fromNumber("-9")
Duration.ofNanos(1_234_567_890)
is stored as ItemAttributeValueMapper.fromNumber("1.234567890")
Duration.ofMillis(1)
is stored as ItemAttributeValueMapper.fromNumber("0.001000000")
Duration.ofNanos(1)
is stored as ItemAttributeValueMapper.fromNumber("0.000000001")
Duration.ofNanos(-1)
is stored as ItemAttributeValueMapper.fromNumber("-0.000000001")
This can be created via create()
.
Modifier and Type | Method and Description |
---|---|
AttributeValueType |
attributeValueType()
The
AttributeValueType that a converter stores and reads values
from DynamoDB via the AttributeValue class. |
static DurationAttributeConverter |
create() |
AttributeValue |
transformFrom(Duration input)
Convert the provided Java object into an
AttributeValue . |
Duration |
transformTo(AttributeValue input)
Convert the provided
AttributeValue into a Java object. |
EnhancedType<Duration> |
type()
The type supported by this converter.
|
public static DurationAttributeConverter create()
public EnhancedType<Duration> type()
AttributeConverter
type
in interface AttributeConverter<Duration>
public AttributeValueType attributeValueType()
AttributeConverter
AttributeValueType
that a converter stores and reads values
from DynamoDB via the AttributeValue
class.attributeValueType
in interface AttributeConverter<Duration>
public AttributeValue transformFrom(Duration input)
AttributeConverter
AttributeValue
. This will raise a RuntimeException
if the
conversion fails, or the input is null.
Example:
InstantAsStringAttributeConverter converter = InstantAsStringAttributeConverter.create();
assertEquals(converter.transformFrom(Instant.EPOCH),
EnhancedAttributeValue.fromString("1970-01-01T00:00:00Z").toAttributeValue());
transformFrom
in interface AttributeConverter<Duration>
public Duration transformTo(AttributeValue input)
AttributeConverter
AttributeValue
into a Java object. This will raise a RuntimeException
if the
conversion fails, or the input is null.
Example:
InstantAsStringAttributeConverter converter = InstantAsStringAttributeConverter.create();
assertEquals(converter.transformTo(EnhancedAttributeValue.fromString("1970-01-01T00:00:00Z").toAttributeValue()),
Instant.EPOCH);
transformTo
in interface AttributeConverter<Duration>
Copyright © 2022. All rights reserved.