2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.dynamodb.internal;
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17 import java.time.ZonedDateTime;
19 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
20 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
21 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
22 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
25 * DynamoDBItem for items that can be serialized as DynamoDB number
27 * @author Sami Salonen - Initial contribution
30 public class DynamoDBBigDecimalItem extends AbstractDynamoDBItem<BigDecimal> {
33 * We get the following error if the BigDecimal has too many digits
34 * "Attempting to store more than 38 significant digits in a Number"
36 * See "Data types" section in
37 * http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html
39 private static final int MAX_DIGITS_SUPPORTED_BY_AMAZON = 38;
41 public DynamoDBBigDecimalItem() {
42 this(null, null, null);
45 public DynamoDBBigDecimalItem(String name, BigDecimal state, ZonedDateTime time) {
46 super(name, state, time);
49 @DynamoDBAttribute(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMSTATE)
51 public BigDecimal getState() {
52 // When serializing this to the wire, we round the number in order to ensure
53 // that it is within the dynamodb limits
54 return loseDigits(state);
57 @DynamoDBHashKey(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMNAME)
59 public String getName() {
64 @DynamoDBRangeKey(attributeName = ATTRIBUTE_NAME_TIMEUTC)
65 public ZonedDateTime getTime() {
70 public void setName(String name) {
75 public void setState(BigDecimal state) {
80 public void setTime(ZonedDateTime time) {
85 public void accept(org.openhab.persistence.dynamodb.internal.DynamoDBItemVisitor visitor) {
89 static BigDecimal loseDigits(BigDecimal number) {
93 return number.round(new MathContext(MAX_DIGITS_SUPPORTED_BY_AMAZON));