All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.MapAttributeConverter Maven / Gradle / Ivy

/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType;
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.StringConverter;
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.TypeConvertingVisitor;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

/**
 * A converter between a specific {@link Map} type and {@link AttributeValue}.
 *
 * 

* This stores values in DynamoDB as a map from string to attribute value. This uses a configured {@link StringAttributeConverter} * to convert the map keys to a string, and a configured {@link AttributeConverter} to convert the map values to an attribute * value. * *

* This supports reading maps from DynamoDB. This uses a configured {@link StringAttributeConverter} to convert the map keys, and * a configured {@link 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> 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> mapConverter = * MapAttributeConverter.mapConverter(MonthDayStringConverter.create(), * StringAttributeConverter.create()); *

* AttributeConverter> sortedMapConverter = * MapAttributeConverter.sortedMapConverter(MonthDayStringConverter.create(), * StringAttributeConverter.create()); * * * @see MapAttributeConverter */ @SdkInternalApi @ThreadSafe @Immutable public class MapAttributeConverter> implements AttributeConverter { private final Delegate delegate; private MapAttributeConverter(Delegate delegate) { this.delegate = delegate; } public static MapAttributeConverter> mapConverter(StringConverter keyConverter, AttributeConverter valueConverter) { return builder(EnhancedType.mapOf(keyConverter.type(), valueConverter.type())) .mapConstructor(LinkedHashMap::new) .keyConverter(keyConverter) .valueConverter(valueConverter) .build(); } public static MapAttributeConverter> concurrentMapConverter(StringConverter keyConverter, AttributeConverter valueConverter) { return builder(EnhancedType.concurrentMapOf(keyConverter.type(), valueConverter.type())) .mapConstructor(ConcurrentHashMap::new) .keyConverter(keyConverter) .valueConverter(valueConverter) .build(); } public static MapAttributeConverter> sortedMapConverter(StringConverter keyConverter, AttributeConverter valueConverter) { return builder(EnhancedType.sortedMapOf(keyConverter.type(), valueConverter.type())) .mapConstructor(TreeMap::new) .keyConverter(keyConverter) .valueConverter(valueConverter) .build(); } public static MapAttributeConverter> navigableMapConverter(StringConverter keyConverter, AttributeConverter valueConverter) { return builder(EnhancedType.navigableMapOf(keyConverter.type(), valueConverter.type())) .mapConstructor(TreeMap::new) .keyConverter(keyConverter) .valueConverter(valueConverter) .build(); } public static , K, V> Builder builder(EnhancedType mapType) { return new Builder<>(mapType); } @Override public EnhancedType type() { return delegate.type(); } @Override public AttributeValueType attributeValueType() { return AttributeValueType.M; } @Override public AttributeValue transformFrom(T input) { return delegate.toAttributeValue(input).toAttributeValue(); } @Override public T transformTo(AttributeValue input) { return delegate.fromAttributeValue(input); } private static final class Delegate, K, V> { private final EnhancedType type; private final Supplier mapConstructor; private final StringConverter keyConverter; private final AttributeConverter valueConverter; private Delegate(Builder builder) { this.type = builder.mapType; this.mapConstructor = builder.mapConstructor; this.keyConverter = builder.keyConverter; this.valueConverter = builder.valueConverter; } public EnhancedType type() { return type; } public EnhancedAttributeValue toAttributeValue(T input) { Map result = new LinkedHashMap<>(); input.forEach((k, v) -> result.put(keyConverter.toString(k), valueConverter.transformFrom(v))); return EnhancedAttributeValue.fromMap(result); } public T fromAttributeValue(AttributeValue input) { return EnhancedAttributeValue.fromAttributeValue(input) .convert(new TypeConvertingVisitor(Map.class, MapAttributeConverter.class) { @Override public T convertMap(Map value) { T result = mapConstructor.get(); value.forEach((k, v) -> result.put(keyConverter.fromString(k), valueConverter.transformTo(v))); return result; } }); } } public static final class Builder, K, V> { private final EnhancedType mapType; private StringConverter keyConverter; private AttributeConverter valueConverter; private Supplier mapConstructor; private Builder(EnhancedType mapType) { this.mapType = mapType; } public Builder mapConstructor(Supplier mapConstructor) { this.mapConstructor = (Supplier) mapConstructor; return this; } public Builder keyConverter(StringConverter keyConverter) { this.keyConverter = keyConverter; return this; } public Builder valueConverter(AttributeConverter valueConverter) { this.valueConverter = valueConverter; return this; } public MapAttributeConverter build() { return new MapAttributeConverter<>(new Delegate<>(this)); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy