
com.amazonaws.services.dynamodbv2.datamodeling.StandardModelFactories Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-java-sdk-osgi Show documentation
Show all versions of aws-java-sdk-osgi Show documentation
The AWS SDK for Java with support for OSGi. The AWS SDK for Java provides Java APIs for building software on AWS' cost-effective, scalable, and reliable infrastructure products. The AWS Java SDK allows developers to code against APIs for all of Amazon's infrastructure web services (Amazon S3, Amazon EC2, Amazon SQS, Amazon Relational Database Service, Amazon AutoScaling, etc).
/*
* Copyright 2016-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 com.amazonaws.annotation.SdkInternalApi;
import com.amazonaws.services.dynamodbv2.datamodeling.ConversionSchema.Dependencies;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel.DynamoDBAttributeType;
import com.amazonaws.services.dynamodbv2.datamodeling.StandardAttributeTypes.AttributeType;
import com.amazonaws.services.dynamodbv2.datamodeling.StandardBeanProperties.Bean;
import com.amazonaws.services.dynamodbv2.datamodeling.StandardParameterTypes.ParamType;
import com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.ConvertToMap;
import com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Scalar;
import com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Vector;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ScalarAttributeType;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardAttributeTypes.AttributeType.BOOL;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardAttributeTypes.AttributeType.L;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardAttributeTypes.AttributeType.M;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardAttributeTypes.AttributeType.NULL;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardAttributeTypes.AttributeType.S;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardAttributeTypes.AttributeType.SS;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Scalar.BOOLEAN;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Scalar.STRING;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Vector.LIST;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Vector.MAP;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Vector.SET;
/**
* Pre-defined strategies for mapping between Java types and DynamoDB types.
*/
@SdkInternalApi
final class StandardModelFactories {
/**
* Gets the default model factory factory.
* @param depends The dependencies.
* @return The model factory factory.
*/
static final DynamoDBMapperModelFactory.Factory of(final Dependencies depends) {
return new SchemaFactoryFactory(depends);
}
/**
* {@link DynamoDBMapperModelFactory} mapped by {@link ConversionSchema}.
*/
private static final class SchemaFactoryFactory implements DynamoDBMapperModelFactory.Factory {
private final ConcurrentMap cache;
private final Dependencies depends;
private SchemaFactoryFactory(final Dependencies depends) {
this.cache = new ConcurrentHashMap();
this.depends = depends;
}
@Override
public DynamoDBMapperModelFactory getModelFactory(final DynamoDBMapperConfig config) {
final ConversionSchema schema = config.getConversionSchema();
if (!cache.containsKey(schema)) {
cache.putIfAbsent(schema, new SchemaFactory(config, schema, depends));
}
return cache.get(schema);
}
}
/**
* {@link DynamoDBMapperTableModel} factory with {@link ItemConverter}.
*/
private static final class SchemaFactory implements DynamoDBMapperModelFactory {
private final ConcurrentMap,DynamoDBMapperTableModel>> cache;
private final DynamoDBMapperConfig config;
private final Conversions conversions;
private final ItemConverter converter;
/**
* Constructs a new schema factory.
*/
private SchemaFactory(final DynamoDBMapperConfig config, final ConversionSchema schema, final Dependencies depends) {
this.cache = new ConcurrentHashMap,DynamoDBMapperTableModel>>();
this.conversions = new Conversions(this).with(depends).with(schema);
this.converter = schema.getConverter(depends);
this.config = config;
}
/**
* {@inheritDoc}
*/
@Override
public DynamoDBMapperTableModel getTableModel(final Class targetType) {
if (!cache.containsKey(targetType)) {
cache.putIfAbsent(targetType, newTableModel(targetType));
}
return (DynamoDBMapperTableModel)cache.get(targetType);
}
/**
* Creates a new {@link DynamoDBMapperTableModel} for the given type.
*/
private DynamoDBMapperTableModel newTableModel(final Class targetType) {
final StandardAnnotationMaps.TableMap annotations = StandardAnnotationMaps.of(targetType);
final DynamoDBMapperTableModel.Builder table = new DynamoDBMapperTableModel.Builder(annotations);
if (annotations.tableName() != null) {
table.withTableName(config.getTableNameResolver(true).getTableName(targetType, config));
}
for (final Bean bean : StandardBeanProperties.of(targetType).values()) {
final DynamoDBMapperFieldModel.Builder field;
field = new DynamoDBMapperFieldModel.Builder(bean.id(), bean.annotations());
field.with(newConverter(bean));
field.with(bean.reflect());
table.with(field.build());
}
return table.build();
}
/**
* Creates an {@link AttributeValue} converter with {@link ItemConverter}
*/
private DynamoDBMapperValueConverter newConverter(final Bean,T> bean) {
if (bean.annotations().typeConverted() != null) {
final DynamoDBTypeConverter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy