Class DurationAttributeConverter
- java.lang.Object
-
- software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.DurationAttributeConverter
-
- All Implemented Interfaces:
AttributeConverter<Duration>
@ThreadSafe @Immutable public final class DurationAttributeConverter extends Object implements AttributeConverter<Duration>
A converter betweenDuration
andAttributeValue
.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 asItemAttributeValueMapper.fromNumber("86400")
Duration.ofSeconds(9)
is stored asItemAttributeValueMapper.fromNumber("9")
Duration.ofSeconds(-9)
is stored asItemAttributeValueMapper.fromNumber("-9")
Duration.ofNanos(1_234_567_890)
is stored asItemAttributeValueMapper.fromNumber("1.234567890")
Duration.ofMillis(1)
is stored asItemAttributeValueMapper.fromNumber("0.001000000")
Duration.ofNanos(1)
is stored asItemAttributeValueMapper.fromNumber("0.000000001")
Duration.ofNanos(-1)
is stored asItemAttributeValueMapper.fromNumber("-0.000000001")
This can be created via
create()
.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AttributeValueType
attributeValueType()
TheAttributeValueType
that a converter stores and reads values from DynamoDB via theAttributeValue
class.static DurationAttributeConverter
create()
AttributeValue
transformFrom(Duration input)
Convert the provided Java object into anAttributeValue
.Duration
transformTo(AttributeValue input)
Convert the providedAttributeValue
into a Java object.EnhancedType<Duration>
type()
The type supported by this converter.
-
-
-
Method Detail
-
create
public static DurationAttributeConverter create()
-
type
public EnhancedType<Duration> type()
Description copied from interface:AttributeConverter
The type supported by this converter.- Specified by:
type
in interfaceAttributeConverter<Duration>
-
attributeValueType
public AttributeValueType attributeValueType()
Description copied from interface:AttributeConverter
TheAttributeValueType
that a converter stores and reads values from DynamoDB via theAttributeValue
class.- Specified by:
attributeValueType
in interfaceAttributeConverter<Duration>
-
transformFrom
public AttributeValue transformFrom(Duration input)
Description copied from interface:AttributeConverter
Convert the provided Java object into anAttributeValue
. This will raise aRuntimeException
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());
- Specified by:
transformFrom
in interfaceAttributeConverter<Duration>
-
transformTo
public Duration transformTo(AttributeValue input)
Description copied from interface:AttributeConverter
Convert the providedAttributeValue
into a Java object. This will raise aRuntimeException
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);
- Specified by:
transformTo
in interfaceAttributeConverter<Duration>
-
-