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

com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverterFactory 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.12.725
Show newest version
/*
 * Copyright 2016-2020 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.StandardTypeConverters.Vector;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * {@link DynamoDBTypeConverter} factory and supporting classes.
 *
 * 

To override standard type-conversions,

*
 * DynamoDBMapperConfig config = DynamoDBMapperConfig.builder()
 *     .withTypeConverterFactory(DynamoDBTypeConverterFactory.standard().override()
 *         .with(String.class, MyObject.class, new StringToMyObjectConverter())
 *         .build())
 *     .build();
 * 
*

Then, on the property, specify the attribute binding,

*
 * @DynamoDBTyped(DynamoDBAttributeType.S)
 * public MyObject getMyObject()
 * 
* * @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig */ public abstract class DynamoDBTypeConverterFactory { /** * Gets the type-converter matching the target conversion type. * @param The DynamoDB standard type. * @param The object's field/property type. * @param sourceType The source conversion type. * @param targetType The target conversion type. * @return The type-converter, or null if no match. */ public abstract DynamoDBTypeConverter getConverter(Class sourceType, Class targetType); /** * Creates a type-converter factory builder using this factory as defaults. */ public final Builder override() { return new Builder(this); } /** * Returns the standard type-converter factory. To override, the factory, * @see DynamoDBTypeConverterFactory#override */ public static final DynamoDBTypeConverterFactory standard() { return StandardTypeConverters.factory(); } /** * Builder for overriding type-converters. */ public static final class Builder { private final ConverterMap overrides = new ConverterMap(); private final DynamoDBTypeConverterFactory defaults; private Builder(DynamoDBTypeConverterFactory defaults) { this.defaults = defaults; } public Builder with(Class sourceType, Class targetType, DynamoDBTypeConverter converter) { if (Vector.SET.is(sourceType) || Vector.LIST.is(sourceType) || Vector.MAP.is(sourceType)) { throw new DynamoDBMappingException("type [" + sourceType + "] is not supported" + "; type-converter factory only supports scalar conversions"); } overrides.put(sourceType, targetType, converter); return this; } public DynamoDBTypeConverterFactory build() { return new OverrideFactory(defaults, overrides); } } /** * A delegating {@link DynamoDBTypeConverterFactory}. */ public static class DelegateFactory extends DynamoDBTypeConverterFactory { private final DynamoDBTypeConverterFactory delegate; public DelegateFactory(DynamoDBTypeConverterFactory delegate) { this.delegate = delegate; } @Override public DynamoDBTypeConverter getConverter(Class sourceType, Class targetType) { return delegate.getConverter(sourceType, targetType); } } /** * Delegate factory to allow selected types to be overriden. */ private static class OverrideFactory extends DelegateFactory { private final ConverterMap overrides; public OverrideFactory(DynamoDBTypeConverterFactory defaults, ConverterMap overrides) { super(defaults); this.overrides = overrides; } @Override public DynamoDBTypeConverter getConverter(Class sourceType, Class targetType) { DynamoDBTypeConverter converter = overrides.get(sourceType, targetType); if (converter == null) { converter = super.getConverter(sourceType, targetType); } return converter; } } /** * Map of source and target pairs to the converter. */ private static final class ConverterMap extends LinkedHashMap,DynamoDBTypeConverter> { private static final long serialVersionUID = -1L; public void put(Class sourceType, Class targetType, DynamoDBTypeConverter converter) { put(Key.of(sourceType, targetType), converter); } @SuppressWarnings("unchecked") public DynamoDBTypeConverter get(Class sourceType, Class targetType) { for (final Entry,DynamoDBTypeConverter> entry : entrySet()) { if (entry.getKey().isAssignableFrom(sourceType, targetType)) { return (DynamoDBTypeConverter)entry.getValue(); } } return null; } } /** * Source and target conversion type pair. */ private static final class Key extends SimpleImmutableEntry,Class> { private static final long serialVersionUID = -1L; private Key(Class sourceType, Class targetType) { super(sourceType, targetType); } public boolean isAssignableFrom(Class sourceType, Class targetType) { return getKey().isAssignableFrom(sourceType) && getValue().isAssignableFrom(targetType); } public static Key of(Class sourceType, Class targetType) { return new Key(sourceType, targetType); } } }