]> git.basschouten.com Git - openhab-addons.git/blob
dd4b084952a89de3de9e068e5f203b9dc24d5bdb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.persistence.dynamodb.internal;
14
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17 import java.time.ZonedDateTime;
18
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
24 /**
25  * DynamoDBItem for items that can be serialized as DynamoDB number
26  *
27  * @author Sami Salonen - Initial contribution
28  */
29 @DynamoDBDocument
30 public class DynamoDBBigDecimalItem extends AbstractDynamoDBItem<BigDecimal> {
31
32     /**
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"
35      *
36      * See "Data types" section in
37      * http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html
38      */
39     private static final int MAX_DIGITS_SUPPORTED_BY_AMAZON = 38;
40
41     public DynamoDBBigDecimalItem() {
42         this(null, null, null);
43     }
44
45     public DynamoDBBigDecimalItem(String name, BigDecimal state, ZonedDateTime time) {
46         super(name, state, time);
47     }
48
49     @DynamoDBAttribute(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMSTATE)
50     @Override
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);
55     }
56
57     @DynamoDBHashKey(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMNAME)
58     @Override
59     public String getName() {
60         return name;
61     }
62
63     @Override
64     @DynamoDBRangeKey(attributeName = ATTRIBUTE_NAME_TIMEUTC)
65     public ZonedDateTime getTime() {
66         return time;
67     }
68
69     @Override
70     public void setName(String name) {
71         this.name = name;
72     }
73
74     @Override
75     public void setState(BigDecimal state) {
76         this.state = state;
77     }
78
79     @Override
80     public void setTime(ZonedDateTime time) {
81         this.time = time;
82     }
83
84     @Override
85     public void accept(org.openhab.persistence.dynamodb.internal.DynamoDBItemVisitor visitor) {
86         visitor.visit(this);
87     }
88
89     static BigDecimal loseDigits(BigDecimal number) {
90         if (number == null) {
91             return null;
92         }
93         return number.round(new MathContext(MAX_DIGITS_SUPPORTED_BY_AMAZON));
94     }
95 }