
software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.SetAttributeConverter 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 static java.util.stream.Collectors.toList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.core.SdkBytes;
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.TypeConvertingVisitor;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.utils.Validate;
/**
* A converter between a specific {@link Collection} type and {@link EnhancedAttributeValue}.
*
*
* This stores values in DynamoDB as a list of attribute values. This uses a configured {@link AttributeConverter} to convert
* the collection contents to an attribute value.
*
*
* This supports reading a list of attribute values. This uses a configured {@link AttributeConverter} to convert
* the collection contents.
*
*
* A builder is exposed to allow defining how the collection and element types are created and converted:
*
* {@literal AttributeConverter> listConverter =
* CollectionAttributeConverter.builder(EnhancedType.listOf(Integer.class))
* .collectionConstructor(ArrayList::new)
* .elementConverter(IntegerAttributeConverter.create())
* .build()}
*
*
*
* For frequently-used types, static methods are exposed to reduce the amount of boilerplate involved in creation:
*
* {@literal AttributeConverter> listConverter =
* CollectionAttributeConverter.listConverter(IntegerAttributeConverter.create());}
*
*
*
* {@literal AttributeConverter> collectionConverer =
* CollectionAttributeConverter.collectionConverter(IntegerAttributeConverter.create());}
*
*
*
* {@literal AttributeConverter> setConverter =
* CollectionAttributeConverter.setConverter(IntegerAttributeConverter.create());}
*
*
*
* {@literal AttributeConverter> sortedSetConverter =
* CollectionAttributeConverter.sortedSetConverter(IntegerAttributeConverter.create());}
*
*
* @see MapAttributeConverter
*/
@SdkInternalApi
@ThreadSafe
@Immutable
public class SetAttributeConverter> implements AttributeConverter {
private final Delegate delegate;
private SetAttributeConverter(Delegate delegate) {
this.delegate = delegate;
}
public static SetAttributeConverter> setConverter(AttributeConverter elementConverter) {
return builder(EnhancedType.setOf(elementConverter.type()))
.collectionConstructor(LinkedHashSet::new)
.elementConverter(elementConverter)
.build();
}
public static , U> SetAttributeConverter.Builder builder(EnhancedType collectionType) {
return new Builder<>(collectionType);
}
@Override
public EnhancedType type() {
return delegate.type();
}
@Override
public AttributeValueType attributeValueType() {
return delegate.attributeValueType();
}
@Override
public AttributeValue transformFrom(T input) {
return delegate.transformFrom(input);
}
@Override
public T transformTo(AttributeValue input) {
return delegate.transformTo(input);
}
private static final class Delegate, U> implements AttributeConverter {
private final EnhancedType type;
private final Supplier extends T> collectionConstructor;
private final AttributeConverter elementConverter;
private final AttributeValueType attributeValueType;
private Delegate(Builder builder) {
this.type = builder.collectionType;
this.collectionConstructor = builder.collectionConstructor;
this.elementConverter = builder.elementConverter;
this.attributeValueType = attributeValueTypeForSet(this.elementConverter);
}
@Override
public EnhancedType type() {
return type;
}
@Override
public AttributeValueType attributeValueType() {
return attributeValueType;
}
@Override
public AttributeValue transformFrom(T input) {
return flatten(input.stream()
.map(elementConverter::transformFrom)
.collect(toList()));
}
@Override
public T transformTo(AttributeValue input) {
return EnhancedAttributeValue.fromAttributeValue(input)
.convert(new TypeConvertingVisitor(type.rawClass(), SetAttributeConverter.class) {
@Override
public T convertSetOfStrings(List value) {
return convertCollection(value, v -> AttributeValue.builder().s(v).build());
}
@Override
public T convertSetOfNumbers(List value) {
return convertCollection(value, v -> AttributeValue.builder().n(v).build());
}
@Override
public T convertSetOfBytes(List value) {
return convertCollection(value, v -> AttributeValue.builder().b(v).build());
}
@Override
public T convertListOfAttributeValues(List value) {
return convertCollection(value, Function.identity());
}
private T convertCollection(Collection collection,
Function transformFrom) {
Collection