|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.enhanced.dynamodb; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.function.Function; |
| 23 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 24 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 25 | +import software.amazon.awssdk.utils.Validate; |
| 26 | + |
| 27 | +/** |
| 28 | + * A converter between an {@link Enum} and {@link AttributeValue}. |
| 29 | + * |
| 30 | + * <p> |
| 31 | + * This stores values in DynamoDB as a string. |
| 32 | + * |
| 33 | + * <p> |
| 34 | + * Use EnumAttributeConverter::create in order to use Enum::toString as the enum identifier |
| 35 | + * |
| 36 | + * <p> |
| 37 | + * Use EnumAttributeConverter::createWithNameAsKeys in order to use Enum::name as the enum identifier |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * This can be created via {@link #create(Class)}. |
| 41 | + */ |
| 42 | +@SdkPublicApi |
| 43 | +public final class EnumAttributeConverter<T extends Enum<T>> implements AttributeConverter<T> { |
| 44 | + |
| 45 | + private final Class<T> enumClass; |
| 46 | + private final Map<String, T> enumValueMap; |
| 47 | + |
| 48 | + private final Function<T, String> keyExtractor; |
| 49 | + |
| 50 | + private EnumAttributeConverter(Class<T> enumClass, Function<T, String> keyExtractor) { |
| 51 | + this.enumClass = enumClass; |
| 52 | + this.keyExtractor = keyExtractor; |
| 53 | + |
| 54 | + Map<String, T> mutableEnumValueMap = new LinkedHashMap<>(); |
| 55 | + Arrays.stream(enumClass.getEnumConstants()) |
| 56 | + .forEach(enumConstant -> mutableEnumValueMap.put(keyExtractor.apply(enumConstant), enumConstant)); |
| 57 | + |
| 58 | + this.enumValueMap = Collections.unmodifiableMap(mutableEnumValueMap); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates an EnumAttributeConverter for an {@link Enum}. |
| 63 | + * |
| 64 | + * <p> |
| 65 | + * Uses Enum::toString as the enum identifier. |
| 66 | + * |
| 67 | + * @param enumClass The enum class to be used |
| 68 | + * @return an EnumAttributeConverter |
| 69 | + * @param <T> the enum subclass |
| 70 | + */ |
| 71 | + public static <T extends Enum<T>> EnumAttributeConverter<T> create(Class<T> enumClass) { |
| 72 | + return new EnumAttributeConverter<>(enumClass, Enum::toString); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates an EnumAttributeConverter for an {@link Enum}. |
| 77 | + * |
| 78 | + * <p> |
| 79 | + * Uses Enum::name as the enum identifier. |
| 80 | + * |
| 81 | + * @param enumClass The enum class to be used |
| 82 | + * @return an EnumAttributeConverter |
| 83 | + * @param <T> the enum subclass |
| 84 | + */ |
| 85 | + public static <T extends Enum<T>> EnumAttributeConverter<T> createWithNameAsKeys(Class<T> enumClass) { |
| 86 | + return new EnumAttributeConverter<>(enumClass, Enum::name); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns the proper {@link AttributeValue} for the given enum type. |
| 91 | + * |
| 92 | + * @param input the enum type to be converted |
| 93 | + * @return AttributeValue |
| 94 | + */ |
| 95 | + @Override |
| 96 | + public AttributeValue transformFrom(T input) { |
| 97 | + return AttributeValue.builder().s(keyExtractor.apply(input)).build(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns the proper enum type for the given {@link AttributeValue} input. |
| 102 | + * |
| 103 | + * @param input the AttributeValue to be converted |
| 104 | + * @return an enum type |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public T transformTo(AttributeValue input) { |
| 108 | + Validate.isTrue(input.s() != null, "Cannot convert non-string value to enum."); |
| 109 | + T returnValue = enumValueMap.get(input.s()); |
| 110 | + |
| 111 | + if (returnValue == null) { |
| 112 | + throw new IllegalArgumentException(String.format("Unable to convert string value '%s' to enum type '%s'", |
| 113 | + input.s(), enumClass)); |
| 114 | + } |
| 115 | + |
| 116 | + return returnValue; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns the {@link EnhancedType} of the converter. |
| 121 | + * |
| 122 | + * @return EnhancedType |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public EnhancedType<T> type() { |
| 126 | + return EnhancedType.of(enumClass); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns the {@link AttributeValueType} of the converter. |
| 131 | + * |
| 132 | + * @return AttributeValueType |
| 133 | + */ |
| 134 | + @Override |
| 135 | + public AttributeValueType attributeValueType() { |
| 136 | + return AttributeValueType.S; |
| 137 | + } |
| 138 | +} |
0 commit comments