
com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperFieldModel 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 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 static com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGenerateStrategy.ALWAYS;
import static com.amazonaws.services.dynamodbv2.datamodeling.StandardTypeConverters.Vector.LIST;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.BEGINS_WITH;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.BETWEEN;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.CONTAINS;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.EQ;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.GE;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.GT;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.IN;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.NULL;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.LE;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.LT;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.NE;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.NOT_CONTAINS;
import static com.amazonaws.services.dynamodbv2.model.ComparisonOperator.NOT_NULL;
import static com.amazonaws.services.dynamodbv2.model.KeyType.HASH;
import static com.amazonaws.services.dynamodbv2.model.KeyType.RANGE;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue;
import com.amazonaws.services.dynamodbv2.model.KeyType;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Field model.
*
* @param The object type.
* @param The field model type.
*/
public class DynamoDBMapperFieldModel implements DynamoDBAutoGenerator, DynamoDBTypeConverter {
public static enum DynamoDBAttributeType { B, N, S, BS, NS, SS, BOOL, NULL, L, M; };
private final DynamoDBTypeConverter attributeValueConverter;
private final DynamoDBMapperFieldModel.Properties properties;
private final DynamoDBMapperFieldModel.Reflect reflect;
/**
* Creates a new field model instance.
* @param builder The builder.
*/
private DynamoDBMapperFieldModel(final DynamoDBMapperFieldModel.Builder builder) {
this.properties = new DynamoDBMapperFieldModel.Properties.Immutable(builder);
this.attributeValueConverter = builder.attributeValueConverter;
this.reflect = builder.reflect;
}
/**
* Gets the ID.
* @return The ID.
*/
final DynamoDBMapperFieldModel.Id id() {
return properties.id();
}
/**
* @deprecated replaced by {@link DynamoDBMapperFieldModel#name}
*/
@Deprecated
public String getDynamoDBAttributeName() {
return properties.attributeName();
}
/**
* @deprecated replaced by {@link DynamoDBMapperFieldModel#attributeType}
*/
@Deprecated
public DynamoDBAttributeType getDynamoDBAttributeType() {
return properties.attributeType();
}
/**
* Gets the attribute name.
* @return The attribute name.
*/
public final String name() {
return properties.attributeName();
}
/**
* Gets the value from the object instance.
* @param object The object instance.
* @return The value.
*/
public final V get(final T object) {
try {
return reflect.get(object);
} catch (final RuntimeException e) {
throw new DynamoDBMappingException(id().err("could not get"), e);
}
}
/**
* Sets the value on the object instance.
* @param object The object instance.
* @param value The value.
*/
public final void set(final T object, final V value) {
try {
reflect.set(object, value);
} catch (final RuntimeException e) {
throw new DynamoDBMappingException(id().err("could not set"), e);
}
}
/**
* {@inheritDoc}
*/
@Override
public final DynamoDBAutoGenerateStrategy getGenerateStrategy() {
if (properties.autoGenerator() != null) {
return properties.autoGenerator().getGenerateStrategy();
}
return null;
}
/**
* {@inheritDoc}
*/
@Override
public final V generate(final V currentValue) {
try {
return properties.autoGenerator().generate(currentValue);
} catch (final RuntimeException e) {
throw new DynamoDBMappingException(id().err("could not generate"), e);
}
}
/**
* {@inheritDoc}
*/
@Override
public final AttributeValue convert(final V object) {
try {
return attributeValueConverter.convert(object);
} catch (final RuntimeException e) {
throw new DynamoDBMappingException(id().err("could not convert"), e);
}
}
/**
* {@inheritDoc}
*/
@Override
public final V unconvert(final AttributeValue object) {
try {
return attributeValueConverter.unconvert(object);
} catch (final RuntimeException e) {
throw new DynamoDBMappingException(id().err("could not unconvert"), e);
}
}
/**
* Gets the DynamoDB attribute type.
* @return The DynamoDB attribute type.
*/
public final DynamoDBAttributeType attributeType() {
return properties.attributeType();
}
/**
* Gets the key type.
* @return The key type, or null if not a key.
*/
public final KeyType keyType() {
return properties.keyType();
}
/**
* Indicates if this attribute is a version attribute.
* @return True if it is, false otherwise.
*/
public final boolean versioned() {
return properties.versioned();
}
/**
* Gets the global secondary indexes.
* @param keyType The key type.
* @return The list of global secondary indexes.
*/
public final List globalSecondaryIndexNames(final KeyType keyType) {
if (properties.globalSecondaryIndexNames().containsKey(keyType)) {
return properties.globalSecondaryIndexNames().get(keyType);
}
return Collections.emptyList();
}
/**
* Gets the local secondary indexes.
* @return The list of local secondary indexes.
*/
public final List localSecondaryIndexNames() {
return properties.localSecondaryIndexNames();
}
/**
* Returns true if the field matches any key or index key.
* @param keyTypes The key types.
* @return True if matching any key type.
*/
public final boolean anyKey(final KeyType ... keyTypes) {
for (final KeyType k : (keyTypes.length == 0 ? KeyType.values() : keyTypes)) {
if (keyType() == k) {
return true;
} else if (!globalSecondaryIndexNames(k).isEmpty()) {
return true;
} else if (RANGE == k && !localSecondaryIndexNames().isEmpty()) {
return true;
}
}
return false;
}
/**
* Creates an expectation that the value should exist.
* @param value The value.
* @return The expected value.
* @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression#withExpectedEntry
* @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression#withExpectedEntry
* @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue
*/
public final ExpectedAttributeValue expectedExists(final V value) {
return new ExpectedAttributeValue().withExists(true).withValue(convert(value));
}
/**
* Creates an expectation that the value should not exist.
* @return The expected value.
* @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression#withExpectedEntry
* @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression#withExpectedEntry
* @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue
*/
public final ExpectedAttributeValue expectedNotExists() {
return new ExpectedAttributeValue().withExists(false);
}
/**
* Creates an expectation that the value should exist if the value is not
* null or an expectation that the value should not exist otherwise.
* @param value The value.
* @return The expected value.
* @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBSaveExpression#withExpectedEntry
* @see com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDeleteExpression#withExpectedEntry
* @see com.amazonaws.services.dynamodbv2.model.ExpectedAttributeValue
*/
public final ExpectedAttributeValue expectedIfExists(final V value) {
return value == null ? expectedNotExists() : expectedExists(value);
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#BEGINS_WITH
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition beginsWith(final V value) {
return new Condition().withComparisonOperator(BEGINS_WITH).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified values.
* @param lo The start of the range (inclusive).
* @param hi The end of the range (inclusive).
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#BETWEEN
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition between(final V lo, final V hi) {
return new Condition().withComparisonOperator(BETWEEN).withAttributeValueList(convert(lo), convert(hi));
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#CONTAINS
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition contains(final V value) {
return new Condition().withComparisonOperator(CONTAINS).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#EQ
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition eq(final V value) {
return new Condition().withComparisonOperator(EQ).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#GE
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition ge(final V value) {
return new Condition().withComparisonOperator(GE).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#GT
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition gt(final V value) {
return new Condition().withComparisonOperator(GT).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified values.
* @param values The values.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#IN
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition in(final Collection values) {
return new Condition().withComparisonOperator(IN).withAttributeValueList(LIST.convert(values, this));
}
/**
* Creates a condition which filters on the specified values.
* @param values The values.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#IN
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition in(final V ... values) {
return in(Arrays.asList(values));
}
/**
* Creates a condition which filters on the specified value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#NULL
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition isNull() {
return new Condition().withComparisonOperator(NULL);
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#LE
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition le(final V value) {
return new Condition().withComparisonOperator(LE).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#LT
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition lt(final V value) {
return new Condition().withComparisonOperator(LT).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#NE
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition ne(final V value) {
return new Condition().withComparisonOperator(NE).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified value.
* @param value The value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#NOT_CONTAINS
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition notContains(final V value) {
return new Condition().withComparisonOperator(NOT_CONTAINS).withAttributeValueList(convert(value));
}
/**
* Creates a condition which filters on the specified value.
* @return The condition.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#NOT_NULL
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition notNull() {
return new Condition().withComparisonOperator(NOT_NULL);
}
/**
* Creates a condition which filters on any non-null argument; if {@code lo}
* is null a {@code LE} condition is applied on {@code hi}, if {@code hi}
* is null a {@code GE} condition is applied on {@code lo}.
* @param lo The start of the range (inclusive).
* @param hi The end of the range (inclusive).
* @return The condition or null if both arguments are null.
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#BETWEEN
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#EQ
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#GE
* @see com.amazonaws.services.dynamodbv2.model.ComparisonOperator#LE
* @see com.amazonaws.services.dynamodbv2.model.Condition
*/
public final Condition betweenAny(final V lo, final V hi) {
return lo == null ? (hi == null ? null : le(hi)) : (hi == null ? ge(lo) : (lo.equals(hi) ? eq(lo) : between(lo,hi)));
}
/**
* The field's "global" identifier.
*/
public static final class Id extends DynamoDBMapperTableModel.Id {
private final String name;
/**
* Construts a new field identifier.
*/
public Id(final Class type, final String name) {
super(type);
this.name = name;
}
/**
* Constructs a new identifier from an existing one.
*/
public Id(final Id id, final String name) {
super(id);
this.name = name;
}
/**
* {@inheritDoc}
*/
@Override
public final boolean equals(final Object o) {
return o instanceof Id && super.equals(o) && ((Id)o).name.equals(name);
}
/**
* {@inheritDoc}
*/
@Override
public final int hashCode() {
return super.hashCode() ^ name.hashCode();
}
/**
* {@inheritDoc}
*/
@Override
public final String toString() {
return new StringBuilder().append(super.toString()).append("[").append(name).append("]").toString();
}
}
/**
* {@link DynamoDBMapperFieldModel} builder.
*/
static class Builder extends DynamoDBMapperFieldModel.Properties.Buildable {
private DynamoDBTypeConverter attributeValueConverter;
private DynamoDBMapperFieldModel.Reflect reflect;
/**
* Constructs a new builder with the optional defaults.
*/
public Builder(final DynamoDBMapperFieldModel.Properties ... defaults) {
super(defaults);
}
/**
* Sets the attribute value converter.
*/
public final Builder with(final DynamoDBTypeConverter attributeValueConverter) {
this.attributeValueConverter = attributeValueConverter;
return this;
}
/**
* Sets the model's reflection property.
*/
public final Builder with(final DynamoDBMapperFieldModel.Reflect reflect) {
this.reflect = reflect;
return this;
}
/**
* Builds the instance.
*/
public final DynamoDBMapperFieldModel build() {
if (keyType() != null) {
if (attributeType().name().matches("[BNS]") == false) {
throw new DynamoDBMappingException(id().err(
"must be scalar (B, N, or S) for key but is %s", attributeType()));
} else if (autoGenerator() != null && autoGenerator().getGenerateStrategy() == ALWAYS) {
throw new DynamoDBMappingException(id().err(
"must not have auto-generated key with ALWAYS strategy"));
}
}
return new DynamoDBMapperFieldModel(this);
}
}
/**
* The field model properties.
*/
static interface Properties {
public DynamoDBMapperFieldModel.Id id();
public Class targetType();
public String attributeName();
public DynamoDBAttributeType attributeType();
public KeyType keyType();
public boolean versioned();
public Map> globalSecondaryIndexNames();
public List localSecondaryIndexNames();
public DynamoDBAutoGenerator autoGenerator();
public DynamoDBTypeConverter typeConverter();
/**
* Immutable properties.
*/
static class Immutable implements Properties {
private static final Properties
© 2015 - 2025 Weber Informatics LLC | Privacy Policy