Class StaticImmutableTableSchema<T,B>
- java.lang.Object
-
- software.amazon.awssdk.enhanced.dynamodb.mapper.StaticImmutableTableSchema<T,B>
-
- All Implemented Interfaces:
TableSchema<T>
@ThreadSafe public final class StaticImmutableTableSchema<T,B> extends Object implements TableSchema<T>
Implementation ofTableSchemathat builds a schema for immutable data objects based on directly declared attributes. Just likeStaticTableSchemawhich is the equivalent implementation for mutable objects, this is the most direct, and thus fastest, implementation ofTableSchema.Example using a fictional 'Customer' immutable data item class that has an inner builder class named 'Builder':-
static final TableSchema<Customer> CUSTOMER_TABLE_SCHEMA = StaticImmutableTableSchema.builder(Customer.class, Customer.Builder.class) .newItemBuilder(Customer::builder, Customer.Builder::build) .addAttribute(String.class, a -> a.name("account_id") .getter(Customer::accountId) .setter(Customer.Builder::accountId) .tags(primaryPartitionKey())) .addAttribute(Integer.class, a -> a.name("sub_id") .getter(Customer::subId) .setter(Customer.Builder::subId) .tags(primarySortKey())) .addAttribute(String.class, a -> a.name("name") .getter(Customer::name) .setter(Customer.Builder::name) .tags(secondaryPartitionKey("customers_by_name"))) .addAttribute(Instant.class, a -> a.name("created_date") .getter(Customer::createdDate) .setter(Customer.Builder::createdDate) .tags(secondarySortKey("customers_by_date"), secondarySortKey("customers_by_name"))) .build();
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStaticImmutableTableSchema.Builder<T,B>Builder for aStaticImmutableTableSchema
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AttributeConverterProviderattributeConverterProvider()The table schemaAttributeConverterProvider.List<String>attributeNames()Returns a complete list of attribute names that are mapped by thisTableSchemaAttributeValueattributeValue(T item, String key)Returns a single attribute value from the modelled object.static <T,B>
StaticImmutableTableSchema.Builder<T,B>builder(Class<T> itemClass, Class<B> builderClass)Creates a builder for aStaticImmutableTableSchematyped to specific immutable data item class.static <T,B>
StaticImmutableTableSchema.Builder<T,B>builder(EnhancedType<T> itemType, EnhancedType<B> builderType)Creates a builder for aStaticImmutableTableSchematyped to specific immutable data item class.AttributeConverter<T>converterForAttribute(Object key)AttributeConverterthat is applied to the given key.booleanisAbstract()A boolean value that represents whether thisTableSchemais abstract which means that it cannot be used to directly create records as it is lacking required structural elements to map to a table, such as a primary key, but can be referred to and embedded by other schemata.Map<String,AttributeValue>itemToMap(T item, boolean ignoreNulls)Takes a modelled object and converts it into a raw map ofAttributeValuethat the DynamoDb low-level SDK can work with.Map<String,AttributeValue>itemToMap(T item, Collection<String> attributes)Takes a modelled object and extracts a specific set of attributes which are then returned as a map ofAttributeValuethat the DynamoDb low-level SDK can work with.EnhancedType<T>itemType()Returns theEnhancedTypethat represents the 'Type' of the Java object this table schema object maps to and from.TmapToItem(Map<String,AttributeValue> attributeMap)Takes a raw DynamoDb SDK representation of a record in a table and maps it to a Java object.TmapToItem(Map<String,AttributeValue> attributeMap, boolean preserveEmptyObject)Takes a raw DynamoDb SDK representation of a record in a table and maps it to a Java object.StaticTableMetadatatableMetadata()Returns the object that describes the structure of the table being modelled by the mapper.
-
-
-
Method Detail
-
builder
public static <T,B> StaticImmutableTableSchema.Builder<T,B> builder(Class<T> itemClass, Class<B> builderClass)
Creates a builder for aStaticImmutableTableSchematyped to specific immutable data item class.- Type Parameters:
T- The type of the immutable item thisTableSchemawill map records to.B- The type of the builder used by thisTableSchemato construct immutable items with.- Parameters:
itemClass- The immutable data item class object that theStaticImmutableTableSchemais to map to.builderClass- The builder class object that can be used to construct instances of the immutable data item.- Returns:
- A newly initialized builder
-
builder
public static <T,B> StaticImmutableTableSchema.Builder<T,B> builder(EnhancedType<T> itemType, EnhancedType<B> builderType)
Creates a builder for aStaticImmutableTableSchematyped to specific immutable data item class.- Type Parameters:
T- The type of the immutable item thisTableSchemawill map records to.B- The type of the builder used by thisTableSchemato construct immutable items with.- Parameters:
itemType- TheEnhancedTypeof the immutable data item class object that theStaticImmutableTableSchemais to map to.builderType- The builderEnhancedTypethat can be used to construct instances of the immutable data item.- Returns:
- A newly initialized builder
-
tableMetadata
public StaticTableMetadata tableMetadata()
Description copied from interface:TableSchemaReturns the object that describes the structure of the table being modelled by the mapper. This includes information such as the table name, index keys and attribute tags.- Specified by:
tableMetadatain interfaceTableSchema<T>- Returns:
- A
TableMetadataobject that contains structural information about the table being modelled.
-
mapToItem
public T mapToItem(Map<String,AttributeValue> attributeMap, boolean preserveEmptyObject)
Description copied from interface:TableSchemaTakes a raw DynamoDb SDK representation of a record in a table and maps it to a Java object. A new object is created to fulfil this operation.If attributes are missing from the map, that will not cause an error, however if attributes are found in the map which the mapper does not know how to map, an exception will be thrown.
In the scenario where all attribute values in the map are null, it will return null if
preserveEmptyObjectis true. If it's false, an empty object will be returned.Note that
preserveEmptyObjectonly applies to the top level Java object, if it has nested "empty" objects, they will be mapped as null. You can useDynamoDbPreserveEmptyObjectto configure this behavior for nested objects.API Implementors Note:
This method must be implemented if
preserveEmptyObjectbehavior is to be supported- Specified by:
mapToItemin interfaceTableSchema<T>- Parameters:
attributeMap- A map of String toAttributeValuethat contains all the raw attributes to map.preserveEmptyObject- whether to initialize this Java object as empty class if all fields are null- Returns:
- A new instance of a Java object with all the attributes mapped onto it.
- See Also:
TableSchema.mapToItem(Map)
-
mapToItem
public T mapToItem(Map<String,AttributeValue> attributeMap)
Description copied from interface:TableSchemaTakes a raw DynamoDb SDK representation of a record in a table and maps it to a Java object. A new object is created to fulfil this operation.If attributes are missing from the map, that will not cause an error, however if attributes are found in the map which the mapper does not know how to map, an exception will be thrown.
If all attribute values in the attributeMap are null, null will be returned. Use
TableSchema.mapToItem(Map, boolean)instead if you need to preserve empty object.API Implementors Note:
TableSchema.mapToItem(Map, boolean)must be implemented ifpreserveEmptyObjectbehavior is desired.- Specified by:
mapToItemin interfaceTableSchema<T>- Parameters:
attributeMap- A map of String toAttributeValuethat contains all the raw attributes to map.- Returns:
- A new instance of a Java object with all the attributes mapped onto it.
- See Also:
TableSchema.mapToItem(Map, boolean)
-
itemToMap
public Map<String,AttributeValue> itemToMap(T item, boolean ignoreNulls)
Description copied from interface:TableSchemaTakes a modelled object and converts it into a raw map ofAttributeValuethat the DynamoDb low-level SDK can work with.- Specified by:
itemToMapin interfaceTableSchema<T>- Parameters:
item- The modelled Java object to convert into a map of attributes.ignoreNulls- If set to true; any null values in the Java object will not be added to the output map. If set to false; null values in the Java object will be added asAttributeValueof type 'nul' to the output map.- Returns:
- A map of String to
AttributeValuerepresenting all the modelled attributes in the model object.
-
itemToMap
public Map<String,AttributeValue> itemToMap(T item, Collection<String> attributes)
Description copied from interface:TableSchemaTakes a modelled object and extracts a specific set of attributes which are then returned as a map ofAttributeValuethat the DynamoDb low-level SDK can work with. This method is typically used to extract just the key attributes of a modelled item and will not ignore nulls on the modelled object.- Specified by:
itemToMapin interfaceTableSchema<T>- Parameters:
item- The modelled Java object to extract the map of attributes from.attributes- A collection of attribute names to extract into the output map.- Returns:
- A map of String to
AttributeValuerepresenting the requested modelled attributes in the model object.
-
attributeValue
public AttributeValue attributeValue(T item, String key)
Description copied from interface:TableSchemaReturns a single attribute value from the modelled object.- Specified by:
attributeValuein interfaceTableSchema<T>- Parameters:
item- The modelled Java object to extract the attribute from.key- The attribute name describing which attribute to extract.- Returns:
- A single
AttributeValuerepresenting the requested modelled attribute in the model object or null if the attribute has not been set with a value in the modelled object.
-
itemType
public EnhancedType<T> itemType()
Description copied from interface:TableSchemaReturns theEnhancedTypethat represents the 'Type' of the Java object this table schema object maps to and from.- Specified by:
itemTypein interfaceTableSchema<T>- Returns:
- The
EnhancedTypeof the modelled item this TableSchema maps to.
-
attributeNames
public List<String> attributeNames()
Description copied from interface:TableSchemaReturns a complete list of attribute names that are mapped by thisTableSchema- Specified by:
attributeNamesin interfaceTableSchema<T>
-
isAbstract
public boolean isAbstract()
Description copied from interface:TableSchemaA boolean value that represents whether thisTableSchemais abstract which means that it cannot be used to directly create records as it is lacking required structural elements to map to a table, such as a primary key, but can be referred to and embedded by other schemata.- Specified by:
isAbstractin interfaceTableSchema<T>- Returns:
- true if it is abstract, and therefore cannot be used directly to create records but can be referred to by other schemata, and false if it is concrete and may be used to map records directly.
-
attributeConverterProvider
public AttributeConverterProvider attributeConverterProvider()
The table schemaAttributeConverterProvider.- See Also:
attributeConverterProvider
-
converterForAttribute
public AttributeConverter<T> converterForAttribute(Object key)
Description copied from interface:TableSchemaAttributeConverterthat is applied to the given key.- Specified by:
converterForAttributein interfaceTableSchema<T>- Parameters:
key- Attribute of the modelled item.- Returns:
- AttributeConverter defined for the given attribute key.
-
-