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

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGenerated Maven / Gradle / Ivy

Go to download

The AWS Java SDK for Amazon DynamoDB module holds the client classes that are used for communicating with Amazon DynamoDB Service

There is a newer version: 1.9.11
Show newest version
/*
 * Copyright 2011-2016 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 java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Annotation to mark a property as using a custom auto-generator.
 *
 * May be annotated on a user-defined annotation to pass additional information
 * to the {@link DynamoDBAutoGenerator}.
 *
 * A minimal example using getter annotations,
 * 
 * @DynamoDBTable(tableName="TestTable")
 * public class TestClass {
 *     private String key, value;
 *
 *     @DynamoDBHashKey
 *     @CustomGeneratedKey(prefix="test-") //<- user-defined annotation
 *     public String getKey() { return this.key; }
 *     public void setKey(String key) { this.key = key; }
 *
 *     public String getValue() { return this.value; }
 *     public void setValue(String value) { this.value = value; }
 * }
 * 
* * And user-defined annotation, *
 * @DynamoDBAutoGenerated(generator=CustomGeneratedKey.Generator.class)
 * @Retention(RetentionPolicy.RUNTIME)
 * @Target({ElementType.METHOD})
 * public @interface CustomGeneratedKey {
 *     String prefix() default "";
 *
 *     public static final class Generator implements DynamoDBAutoGenerator<String> {
 *         private final String prefix;
 *         public Generator(final CustomGeneratedKey annotation) {
 *             this.prefix = annotation.prefix();
 *         }
 *         public Generator() {
 *             this.prefix = "";
 *         }
 *         @Override
 *         public DynamoDBAutoGenerateStrategy getGenerateStrategy() {
 *             return DynamoDBAutoGenerateStrategy.CREATE;
 *         }
 *         @Override
 *         public final T generate(final T currentValue) {
 *             return prefix + UUID.randomUUID.toString();
 *         }
 *     }
 * }
 * 
* * Alternatively, the property/field may be annotated directly (which requires * the generator to provide a default constructor), *
 * @DynamoDBHashKey
 * @DynamoDBAutoGenerated(generator=CustomGeneratedKey.Generator.class)
 * public String getKey() { return this.key; }
 * 
* * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedDefault * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedTimestamp * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGenerator * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBVersionAttribute */ @DynamoDB @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE}) public @interface DynamoDBAutoGenerated { /** * The auto-generator class for this property. */ Class generator(); /** * Annotation auto-generator factory. */ static final class Generators { static DynamoDBAutoGenerator of(final Class targetType, final Annotation annotation) { final DynamoDBAutoGenerated generated; if (annotation.annotationType() == DynamoDBAutoGenerated.class) { generated = (DynamoDBAutoGenerated)annotation; } else { generated = annotation.annotationType().getAnnotation(DynamoDBAutoGenerated.class); if (generated == null) { throw new DynamoDBMappingException("could not resolve auto-generator: " + annotation); } } DynamoDBAutoGenerator generator = null; try { if (annotation != generated) { try { generator = generated.generator() .getConstructor(Class.class, annotation.annotationType()) .newInstance(targetType, annotation); } catch (final NoSuchMethodException no) {} } if (generator == null) { try { generator = generated.generator().getConstructor(Class.class) .newInstance(targetType); } catch (final NoSuchMethodException no) { generator = generated.generator().newInstance(); } } } catch (final Exception e) { throw new DynamoDBMappingException("could not create auto-generator: " + annotation, e); } if (generator.getGenerateStrategy() == DynamoDBAutoGenerateStrategy.CREATE && targetType.isPrimitive()) { throw new DynamoDBMappingException("type [" + targetType + "] is not supported" + "; primitives are not allowed when auto-generate strategy is CREATE"); } return generator; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy