|
| 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.time.Instant; |
| 19 | + |
| 20 | +import software.amazon.awssdk.annotations.SdkPublicApi; |
| 21 | +import software.amazon.awssdk.annotations.ThreadSafe; |
| 22 | +import software.amazon.awssdk.enhanced.dynamodb.TypeToken; |
| 23 | +import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.InstantAsIntegerAttributeConverter; |
| 24 | +import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.InstantAsStringAttributeConverter; |
| 25 | +import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.StringAttributeConverter; |
| 26 | +import software.amazon.awssdk.enhanced.dynamodb.mapper.AttributeValueType; |
| 27 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 28 | + |
| 29 | +/** |
| 30 | + * Converts between a specific Java type and an {@link AttributeValue}. |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * Examples: |
| 34 | + * <ul> |
| 35 | + * <li>The {@link StringAttributeConverter} converts a {@link String} into a DynamoDB string |
| 36 | + * ({@link software.amazon.awssdk.services.dynamodb.model.AttributeValue#s()}).</li> |
| 37 | + * <li>The {@link InstantAsIntegerAttributeConverter} converts an {@link Instant} into a DynamoDB number |
| 38 | + * ({@link software.amazon.awssdk.services.dynamodb.model.AttributeValue#n()}).</li> |
| 39 | + * <li>The {@link InstantAsStringAttributeConverter} converts an {@link Instant} into a DynamoDB string |
| 40 | + * ({@link software.amazon.awssdk.services.dynamodb.model.AttributeValue#s()}).</li> |
| 41 | + * </ul> |
| 42 | + */ |
| 43 | +@SdkPublicApi |
| 44 | +@ThreadSafe |
| 45 | +public interface AttributeConverter<T> { |
| 46 | + /** |
| 47 | + * Convert the provided Java object into an {@link AttributeValue}. This will raise a {@link RuntimeException} if the |
| 48 | + * conversion fails, or the input is null. |
| 49 | + * |
| 50 | + * <p> |
| 51 | + * Example: |
| 52 | + * {@code |
| 53 | + * InstantAsStringAttributeConverter converter = InstantAsStringAttributeConverter.create(); |
| 54 | + * assertEquals(converter.toAttributeValue(Instant.EPOCH), |
| 55 | + * ItemAttributeValue.fromString("1970-01-01T00:00:00Z")); |
| 56 | + * } |
| 57 | + */ |
| 58 | + AttributeValue transformFrom(T input); |
| 59 | + |
| 60 | + /** |
| 61 | + * Convert the provided {@link AttributeValue} into a Java object. This will raise a {@link RuntimeException} if the |
| 62 | + * conversion fails, or the input is null. |
| 63 | + * |
| 64 | + * <p> |
| 65 | + * Example: |
| 66 | + * {@code |
| 67 | + * InstantAsStringAttributeConverter converter = InstantAsStringAttributeConverter.create(); |
| 68 | + * assertEquals(converter.fromAttributeValue(ItemAttributeValue.fromString("1970-01-01T00:00:00Z")), |
| 69 | + * Instant.EPOCH); |
| 70 | + * } |
| 71 | + */ |
| 72 | + T transformTo(AttributeValue input); |
| 73 | + |
| 74 | + /** |
| 75 | + * The type supported by this converter. |
| 76 | + */ |
| 77 | + TypeToken<T> type(); |
| 78 | + |
| 79 | + /** |
| 80 | + * The {@link AttributeValueType} that a converter stores and reads values |
| 81 | + * from DynamoDB via the {@link AttributeValue} class. |
| 82 | + */ |
| 83 | + AttributeValueType attributeValueType(); |
| 84 | +} |
0 commit comments