]> git.basschouten.com Git - openhab-addons.git/blob
13979e4d9d9152dbe4401c62a64fc1afa1fac471
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
24
25 /**
26  * DynamoDBItem for items that can be serialized as DynamoDB number
27  *
28  * @author Sami Salonen - Initial contribution
29  */
30 @DynamoDBDocument
31 public class DynamoDBBigDecimalItem extends AbstractDynamoDBItem<BigDecimal> {
32
33     /**
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"
36      *
37      * See "Data types" section in
38      * http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Limits.html
39      */
40     private static final int MAX_DIGITS_SUPPORTED_BY_AMAZON = 38;
41
42     public DynamoDBBigDecimalItem() {
43         this(null, null, null);
44     }
45
46     public DynamoDBBigDecimalItem(String name, BigDecimal state, ZonedDateTime time) {
47         super(name, state, time);
48     }
49
50     @DynamoDBAttribute(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMSTATE)
51     @Override
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);
56     }
57
58     @DynamoDBHashKey(attributeName = DynamoDBItem.ATTRIBUTE_NAME_ITEMNAME)
59     @Override
60     public String getName() {
61         return name;
62     }
63
64     @Override
65     @DynamoDBRangeKey(attributeName = ATTRIBUTE_NAME_TIMEUTC)
66     @DynamoDBTypeConverted(converter = ZonedDateTimeConverter.class)
67     public ZonedDateTime getTime() {
68         return time;
69     }
70
71     @Override
72     public void setName(String name) {
73         this.name = name;
74     }
75
76     @Override
77     public void setState(BigDecimal state) {
78         this.state = state;
79     }
80
81     @Override
82     public void setTime(ZonedDateTime time) {
83         this.time = time;
84     }
85
86     @Override
87     public void accept(org.openhab.persistence.dynamodb.internal.DynamoDBItemVisitor visitor) {
88         visitor.visit(this);
89     }
90
91     static BigDecimal loseDigits(BigDecimal number) {
92         if (number == null) {
93             return null;
94         }
95         return number.round(new MathContext(MAX_DIGITS_SUPPORTED_BY_AMAZON));
96     }
97 }