2 * Copyright (c) 2010-2021 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;
23 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
26 * DynamoDBItem for items that can be serialized as DynamoDB number
28 * @author Sami Salonen - Initial contribution
31 public class DynamoDBBigDecimalItem extends AbstractDynamoDBItem<BigDecimal> {
34 * We get the following error if the BigDecimal has too many digits
35 * "Attempting to store more than 38 significant digits in a Number"
37 * See "Data types" section in
38 * http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html
40 private static final int MAX_DIGITS_SUPPORTED_BY_AMAZON = 38;
42 public DynamoDBBigDecimalItem() {
43 this(null, null, null);
46 public DynamoDBBigDecimalItem(String name, BigDecimal state, ZonedDateTime time) {
47 super(name, state, time);
50 @DynamoDBAttribute(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMSTATE)
52 public BigDecimal getState() {
53 // When serializing this to the wire, we round the number in order to ensure
54 // that it is within the dynamodb limits
55 return loseDigits(state);
58 @DynamoDBHashKey(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMNAME)
60 public String getName() {
65 @DynamoDBRangeKey(attributeName = ATTRIBUTE_NAME_TIMEUTC)
66 @DynamoDBTypeConverted(converter = ZonedDateTimeConverter.class)
67 public ZonedDateTime getTime() {
72 public void setName(String name) {
77 public void setState(BigDecimal state) {
82 public void setTime(ZonedDateTime time) {
87 public void accept(org.openhab.persistence.dynamodb.internal.DynamoDBItemVisitor visitor) {
91 static BigDecimal loseDigits(BigDecimal number) {
95 return number.round(new MathContext(MAX_DIGITS_SUPPORTED_BY_AMAZON));