/* * Copyright 2016-2023 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. * You may obtain a copy of the License at: * * http://aws.amazon.com/apache2.0 * * 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 com.amazonaws.services.dynamodbv2.datamodeling; import com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties.Bean; import com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties.BeanMap; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.util.regex.Pattern; /** * Annotation to convert an object into a single delimited {@link String} * attribute. * *
* @DynamoDBDelimited( * attributeNames={"areaCode","exchange","subscriber"}, * delimiter='-' * ) * public PhoneNumber getPhoneNumber() ** *
Where,
** public class PhoneNumber { * private String areaCode; * private String exchange; * private String subscriber; * * public String getAreaCode() { return areaCode; } * public void setAreaCode(String areaCode) { this.areaCode = areaCode; } * * public String getExchange() { return exchange; } * public void setExchange(String exchange) { this.exchange = exchange; } * * public String getSubscriber() { return subscriber; } * public void setSubscriber(String subscriber) { this.subscriber = subscriber; } * } ** *
Would write,
*PhoneNumber("206","266","1000")
= "206-266-1000"
PhoneNumber("206",null,"1000")
= "206--1000"
PhoneNumber("206",null,null)
= "206--"
PhoneNumber(null,"266","1000")
= "-266-1000"
PhoneNumber(null,"266",null)
= "-266-"
PhoneNumber(null,null,"1000")
= "--1000"
PhoneNumber(null,null,null)
= null
null
= null
""
= empty string not allowed by DDB but would produce empty object
"--"
= PhoneNumber(null,null,null)
"-----"
= PhoneNumber(null,null,null)
"206"
= PhoneNumber("206",null,null)
"206-266"
= PhoneNumber("206","266",null)
"206-266-1000-1234-5678"
= PhoneNumber("206","266","1000")
The converter does not protect against values which may also contain the * delimiter. If more advanced conversion is required, consider implementing, * a custom {@link DynamoDBTypeConverter}.
* *New delimited values may always be appended to the string, however, there * are some risks in distributed systems where, if one system has updated * delimiting instructions and begins to persist new values, other systems, * which also persist that same data, would effectively truncate it back to the * original format.
* *Auto-generated annotations are not supported on field/property.
* *TYpe-converted annotations, annotated by {@link DynamoDBTypeConverted}, * where the output type is {@link String} are supported. * *
May be used as a meta-annotation.
*/ @DynamoDB @DynamoDBTypeConverted(converter=DynamoDBDelimited.Converter.class) @DynamoDBTyped(DynamoDBMapperFieldModel.DynamoDBAttributeType.S) @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) public @interface DynamoDBDelimited { /** * The delimiter for separating attribute values; default is|
.
*/
char delimiter() default '|';
/**
* The ordered list of attribute/field names.
*/
String[] attributeNames();
/**
* Type converter for string delimited attributes.
*/
static final class Converter