Class BigDecimalLongTranslatorFactory
- java.lang.Object
-
- com.googlecode.objectify.impl.translate.ValueTranslatorFactory<P,D>
-
- com.googlecode.objectify.impl.translate.SimpleTranslatorFactory<BigDecimal,Long>
-
- com.googlecode.objectify.impl.translate.opt.BigDecimalLongTranslatorFactory
-
- All Implemented Interfaces:
TranslatorFactory<BigDecimal,Long>
public class BigDecimalLongTranslatorFactory extends SimpleTranslatorFactory<BigDecimal,Long>
This a simple strategy for storing BigDecimal in the datastore. BigDecimalLongConverter multiplies by a fixed factor and stores the result as a Long. This is appropriate for monetary and other (relatively) small values with fixed decimal precision.
This is one possible strategy and not appropriate for all uses of BigDecimal - especially very large values which cannot fit in a Long. For this reason, the converter is not installed by default. You can Install this converter at the same time you perform registration:
ObjectifyService.factory().getTranslators().add(new BigDecimalLongTranslatorFactory());
The default factor of 1,000 is good for currency, which usually has 0-3 digits of precision past the decimal point. But you can pick any other factor appropriate to your application.
All custom translators must be registered *before* entity classes are registered.
- Author:
- Jeff Schnitzer
-
-
Field Summary
Fields Modifier and Type Field Description static longDEFAULT_FACTORDefault factor is 1000, which gives you three digits of precision past the decimal point
-
Constructor Summary
Constructors Constructor Description BigDecimalLongTranslatorFactory()Construct this converter with the default factor (1000), which can store three points of precision past the decimal point.BigDecimalLongTranslatorFactory(long factor)Construct this with an arbitrary factor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected com.google.cloud.datastore.Value<Long>toDatastore(BigDecimal value)protected BigDecimaltoPojo(com.google.cloud.datastore.Value<Long> value)-
Methods inherited from class com.googlecode.objectify.impl.translate.SimpleTranslatorFactory
createValueTranslator
-
Methods inherited from class com.googlecode.objectify.impl.translate.ValueTranslatorFactory
create
-
-
-
-
Field Detail
-
DEFAULT_FACTOR
public static final long DEFAULT_FACTOR
Default factor is 1000, which gives you three digits of precision past the decimal point- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BigDecimalLongTranslatorFactory
public BigDecimalLongTranslatorFactory()
Construct this converter with the default factor (1000), which can store three points of precision past the decimal point.
-
BigDecimalLongTranslatorFactory
public BigDecimalLongTranslatorFactory(long factor)
Construct this with an arbitrary factor. Powers of ten are highly recommended if you want to be able to interpret the numbers in the datastore viewer.- Parameters:
factor- number multiplied by before storage and divided by on retrieval.
-
-
Method Detail
-
toPojo
protected BigDecimal toPojo(com.google.cloud.datastore.Value<Long> value)
- Specified by:
toPojoin classSimpleTranslatorFactory<BigDecimal,Long>
-
toDatastore
protected com.google.cloud.datastore.Value<Long> toDatastore(BigDecimal value)
- Specified by:
toDatastorein classSimpleTranslatorFactory<BigDecimal,Long>
-
-