@ThreadSafe @Immutable public class MapAttributeConverter<T extends Map<?,?>> extends Object implements AttributeConverter<T>
Map
type and AttributeValue
.
This stores values in DynamoDB as a map from string to attribute value. This uses a configured StringAttributeConverter
to convert the map keys to a string, and a configured AttributeConverter
to convert the map values to an attribute
value.
This supports reading maps from DynamoDB. This uses a configured StringAttributeConverter
to convert the map keys, and
a configured AttributeConverter
to convert the map values.
A builder is exposed to allow defining how the map, key and value types are created and converted:
AttributeConverter<Map<MonthDay, String>> mapConverter =
MapAttributeConverter.builder(EnhancedType.mapOf(Integer.class, String.class))
.mapConstructor(HashMap::new)
.keyConverter(MonthDayStringConverter.create())
.valueConverter(StringAttributeConverter.create())
.build();
For frequently-used types, static methods are exposed to reduce the amount of boilerplate involved in creation:
AttributeConverter<Map<MonthDay, String>> mapConverter =
MapAttributeConverter.mapConverter(MonthDayStringConverter.create(),
StringAttributeConverter.create());
AttributeConverter<SortedMap<MonthDay, String>> sortedMapConverter =
MapAttributeConverter.sortedMapConverter(MonthDayStringConverter.create(),
StringAttributeConverter.create());
MapAttributeConverter
Modifier and Type | Class and Description |
---|---|
static class |
MapAttributeConverter.Builder<T extends Map<K,V>,K,V> |
public static <K,V> MapAttributeConverter<Map<K,V>> mapConverter(StringConverter<K> keyConverter, AttributeConverter<V> valueConverter)
public static <K,V> MapAttributeConverter<ConcurrentMap<K,V>> concurrentMapConverter(StringConverter<K> keyConverter, AttributeConverter<V> valueConverter)
public static <K,V> MapAttributeConverter<SortedMap<K,V>> sortedMapConverter(StringConverter<K> keyConverter, AttributeConverter<V> valueConverter)
public static <K,V> MapAttributeConverter<NavigableMap<K,V>> navigableMapConverter(StringConverter<K> keyConverter, AttributeConverter<V> valueConverter)
public static <T extends Map<K,V>,K,V> MapAttributeConverter.Builder<T,K,V> builder(EnhancedType<T> mapType)
public EnhancedType<T> type()
AttributeConverter
type
in interface AttributeConverter<T extends Map<?,?>>
public AttributeValueType attributeValueType()
AttributeConverter
AttributeValueType
that a converter stores and reads values
from DynamoDB via the AttributeValue
class.attributeValueType
in interface AttributeConverter<T extends Map<?,?>>
public AttributeValue transformFrom(T 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<T extends Map<?,?>>
public T 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<T extends Map<?,?>>
Copyright © 2022. All rights reserved.