public final class StaticImmutableTableSchema<T,B> extends Object implements TableSchema<T>
TableSchema
that builds a schema for immutable data objects based on directly declared
attributes. Just like StaticTableSchema
which is the equivalent implementation for mutable objects, this is
the most direct, and thus fastest, implementation of TableSchema
.
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();
Modifier and Type | Class and Description |
---|---|
static class |
StaticImmutableTableSchema.Builder<T,B>
Builder for a
StaticImmutableTableSchema |
Modifier and Type | Method and Description |
---|---|
AttributeConverterProvider |
attributeConverterProvider()
The table schema
AttributeConverterProvider . |
List<String> |
attributeNames()
Returns a complete list of attribute names that are mapped by this
TableSchema |
AttributeValue |
attributeValue(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 a
StaticImmutableTableSchema typed to specific immutable data item class. |
boolean |
isAbstract()
A boolean value that represents whether this
TableSchema is 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 of
AttributeValue that 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 of
AttributeValue that the DynamoDb low-level SDK can work with. |
EnhancedType<T> |
itemType()
Returns the
EnhancedType that represents the 'Type' of the Java object this table schema object maps to
and from. |
T |
mapToItem(Map<String,AttributeValue> attributeMap)
Takes a raw DynamoDb SDK representation of a record in a table and maps it to a Java object.
|
T |
mapToItem(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.
|
StaticTableMetadata |
tableMetadata()
Returns the object that describes the structure of the table being modelled by the mapper.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
builder, fromBean, fromClass, fromImmutableClass
public static <T,B> StaticImmutableTableSchema.Builder<T,B> builder(Class<T> itemClass, Class<B> builderClass)
StaticImmutableTableSchema
typed to specific immutable data item class.builder
in interface TableSchema<T>
T
- The type of the immutable item this TableSchema
will map records to.B
- The type of the builder used by this TableSchema
to construct immutable items with.itemClass
- The immutable data item class object that the StaticImmutableTableSchema
is to map to.builderClass
- The builder class object that can be used to construct instances of the immutable data item.public StaticTableMetadata tableMetadata()
TableSchema
tableMetadata
in interface TableSchema<T>
TableMetadata
object that contains structural information about the table being modelled.public T mapToItem(Map<String,AttributeValue> attributeMap, boolean preserveEmptyObject)
TableSchema
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 preserveEmptyObject
is true. If it's false, an empty object will be returned.
Note that preserveEmptyObject
only applies to the top level Java object, if it has nested "empty" objects, they
will be mapped as null. You can use DynamoDbPreserveEmptyObject
to configure this behavior for nested objects.
API Implementors Note:
This method must be implemented if preserveEmptyObject
behavior is to be supported
mapToItem
in interface TableSchema<T>
attributeMap
- A map of String to AttributeValue
that contains all the raw attributes to map.preserveEmptyObject
- whether to initialize this Java object as empty class if all fields are nullTableSchema.mapToItem(Map)
public T mapToItem(Map<String,AttributeValue> attributeMap)
TableSchema
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 if preserveEmptyObject
behavior is desired.
mapToItem
in interface TableSchema<T>
attributeMap
- A map of String to AttributeValue
that contains all the raw attributes to map.TableSchema.mapToItem(Map, boolean)
public Map<String,AttributeValue> itemToMap(T item, boolean ignoreNulls)
TableSchema
AttributeValue
that the DynamoDb low-level
SDK can work with.itemToMap
in interface TableSchema<T>
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 as AttributeValue
of
type 'nul' to the output map.AttributeValue
representing all the modelled attributes in the model object.public Map<String,AttributeValue> itemToMap(T item, Collection<String> attributes)
TableSchema
AttributeValue
that 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.itemToMap
in interface TableSchema<T>
item
- The modelled Java object to extract the map of attributes from.attributes
- A collection of attribute names to extract into the output map.AttributeValue
representing the requested modelled attributes in the model
object.public AttributeValue attributeValue(T item, String key)
TableSchema
attributeValue
in interface TableSchema<T>
item
- The modelled Java object to extract the attribute from.key
- The attribute name describing which attribute to extract.AttributeValue
representing the requested modelled attribute in the model object or
null if the attribute has not been set with a value in the modelled object.public EnhancedType<T> itemType()
TableSchema
EnhancedType
that represents the 'Type' of the Java object this table schema object maps to
and from.itemType
in interface TableSchema<T>
EnhancedType
of the modelled item this TableSchema maps to.public List<String> attributeNames()
TableSchema
TableSchema
attributeNames
in interface TableSchema<T>
public boolean isAbstract()
TableSchema
TableSchema
is 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.isAbstract
in interface TableSchema<T>
public AttributeConverterProvider attributeConverterProvider()
AttributeConverterProvider
.attributeConverterProvider
Copyright © 2021. All rights reserved.