com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-java-sdk-dynamodb Show documentation
Show all versions of aws-java-sdk-dynamodb Show documentation
The AWS Java SDK for Amazon DynamoDB module holds the client classes that are used for communicating with Amazon DynamoDB Service
/*
* 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.annotation.SdkInternalApi;
/**
* Interface for converting types.
*
* @param The DynamoDB standard type.
* @param The object's field/property type.
*/
public interface DynamoDBTypeConverter {
/**
* Turns an object of type T into an object of type S.
*/
S convert(T object);
/**
* Turns an object of type S into an object of type T.
*/
T unconvert(S object);
/**
* An abstract converter with additional general purpose functions.
*/
@SdkInternalApi
static abstract class AbstractConverter implements DynamoDBTypeConverter {
public static ExtendedConverter join(DynamoDBTypeConverter source, DynamoDBTypeConverter target) {
return new ExtendedConverter(source, target);
}
public static NullSafeConverter nullSafe(DynamoDBTypeConverter converter) {
return new NullSafeConverter(converter);
}
public DynamoDBTypeConverter joinAll(DynamoDBTypeConverter ... targets) {
AbstractConverter converter = (AbstractConverter)nullSafe();
for (DynamoDBTypeConverter target : targets) {
if (target != null) {
converter = converter.join((DynamoDBTypeConverter)nullSafe(target));
}
}
return converter;
}
public ExtendedConverter join(DynamoDBTypeConverter target) {
return AbstractConverter.join(this, target);
}
public NullSafeConverter nullSafe() {
return AbstractConverter.nullSafe(this);
}
}
/**
* A converter which wraps a source and target converter.
*/
public static class ExtendedConverter extends AbstractConverter {
private final DynamoDBTypeConverter source;
private final DynamoDBTypeConverter target;
public ExtendedConverter(DynamoDBTypeConverter source, DynamoDBTypeConverter target) {
this.source = source;
this.target = target;
}
@Override
public S convert(final T o) {
return source.convert(target.convert(o));
}
@Override
public T unconvert(final S o) {
return target.unconvert(source.unconvert(o));
}
}
/**
* A general purpose delegating converter.
*/
public static class DelegateConverter extends AbstractConverter {
private final DynamoDBTypeConverter delegate;
public DelegateConverter(DynamoDBTypeConverter delegate) {
this.delegate = delegate;
}
@Override
public S convert(final T object) {
return delegate.convert(object);
}
@Override
public T unconvert(final S object) {
return delegate.unconvert(object);
}
}
/**
* A converter which evaluates nullability before convert/unconvert.
*/
public static class NullSafeConverter extends DelegateConverter {
public NullSafeConverter(DynamoDBTypeConverter delegate) {
super(delegate);
}
@Override
public S convert(final T object) {
return object == null ? null : super.convert(object);
}
@Override
public T unconvert(final S object) {
return object == null ? null : super.unconvert(object);
}
}
}