@Retention(value=RUNTIME) @Target(value={TYPE,FIELD,METHOD,ANNOTATION_TYPE}) public @interface DynamoDBTypeConverted
DynamoDBTypeConverter.
A minimal example using getter annotations,
@DynamoDBTable(tableName="TestTable")
public class TestClass {
private String key;
private Currency currency;
@DynamoDBHashKey
public String getKey() { return key; }
public void setKey(String key) { this.key = key; }
@CurrencyFormat(separator=" ") //<- user-defined annotation
public Currency getCurrency() { return currency; }
public void setCurrency(Currency currency) { this.currency = currency; }
}
With the complex type to convert:,
public class Currency {
private Double amount;
private String unit;
public Double getAmount() { return amount; }
public void setAmount(Double amount) { this.amount = amount; }
public String getUnit() { return unit; }
public void setUnit(String unit) { this.unit = unit; }
}
And user-defined annotation,
@DynamoDBTypeConverted(converter=CurrencyFormat.Converter.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface CurrencyFormat {
String separator() default " ";
public static final class Converter implements DynamoDBTypeConverter<String,Currency> {
private final String separator;
public Converter(final CurrencyFormat annotation) {
this.separator = annotation.separator();
}
public Converter() {
this.separator = "|";
}
@Override
public String convert(final Currency o) {
return String.valueOf(o.getAmount()) + separator + o.getUnit();
}
@Override
public Currency unconvert(final String o) {
final String[] strings = o.split(separator);
final Currency currency = new Currency();
currency.setAmount(Double.valueOf(strings[0]));
currency.setUnit(strings[1]);
return currency;
}
}
}
Alternatively, the property/field may be annotated directly (which requires
the converter to provide a default constructor),
@DynamoDBTypeConverted(converter=CurrencyFormat.Converter.class)
public Currency getCurrency() { return currency; }
Precedence for selecting a type-converter first goes to getter annotations,
then field, then finally type.| Modifier and Type | Required Element and Description |
|---|---|
Class<? extends DynamoDBTypeConverter> |
converter
The class of the converter for this property.
|
public abstract Class<? extends DynamoDBTypeConverter> converter
Copyright © 2016. All rights reserved.