2 * Copyright (c) 2010-2023 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.time.ZonedDateTime;
17 import javax.measure.Unit;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.items.Item;
22 import org.openhab.core.persistence.HistoricItem;
25 * Represents openHAB Item serialized in a suitable format for the database
27 * @param <T> Type of the state as accepted by the AWS SDK.
29 * @author Sami Salonen - Initial contribution
32 public interface DynamoDBItem<T> {
34 static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
36 static final String ATTRIBUTE_NAME_TIMEUTC_LEGACY = "timeutc";
37 static final String ATTRIBUTE_NAME_ITEMNAME_LEGACY = "itemname";
38 static final String ATTRIBUTE_NAME_ITEMSTATE_LEGACY = "itemstate";
39 static final String ATTRIBUTE_NAME_TIMEUTC = "t";
40 static final String ATTRIBUTE_NAME_ITEMNAME = "i";
41 static final String ATTRIBUTE_NAME_ITEMSTATE_STRING = "s";
42 static final String ATTRIBUTE_NAME_ITEMSTATE_NUMBER = "n";
43 static final String ATTRIBUTE_NAME_EXPIRY = "exp";
46 * Convert this AbstractDynamoItem as HistoricItem, i.e. converting serialized state back to openHAB state.
48 * Returns null when this instance has null state.
50 * If item is NumberItem and has an unit, the data is converted to QuantityType with item.getUnit().
52 * @param item Item representing this item. Used to determine item type.
53 * @return HistoricItem representing this DynamoDBItem.
56 HistoricItem asHistoricItem(Item item);
59 * Convert this AbstractDynamoItem as HistoricItem.
61 * Returns null when this instance has null state.
62 * The implementation can deal with legacy schema as well.
64 * Use this method when repeated calls are expected for same item (avoids the expensive call to item.getUnit())
66 * @param item Item representing this item. Used to determine item type.
67 * @param targetUnit unit to convert the data if item is with Dimension. Has only effect with NumberItems and with
68 * numeric DynamoDBItems.
69 * @return HistoricItem representing this DynamoDBItem.
72 HistoricItem asHistoricItem(Item item, @Nullable Unit<?> targetUnit);
82 * Get item state, in the serialized format
84 * @return item state as serialized format
90 * Get timestamp of this value
94 ZonedDateTime getTime();
97 * Get expire time for the DynamoDB item in days.
99 * Does not have any effect with legacy schema.
101 * Also known as time-to-live or TTL.
102 * Null means that expire is disabled
104 * @return expire time in days
107 Integer getExpireDays();
110 * Get expiry date for the DynamoDB item in epoch seconds
112 * This is used with DynamoDB Time to Live TTL feature.
114 * @return expiry date of the data. Equivalent to getTime() + getExpireDays() or null when expireDays is null.
117 Long getExpiryDate();
120 * Setter for item name
122 * @param name item name
124 void setName(String name);
127 * Setter for serialized state
129 * @param state serialized state
131 void setState(@Nullable T state);
134 * Set timestamp of the data
136 * @param time timestamp
138 void setTime(ZonedDateTime time);
141 * Set expire time for the DynamoDB item in days.
143 * Does not have any effect with legacy schema.
145 * Also known as time-to-live or TTL.
146 * Use null to disable expiration
148 * @param expireDays expire time in days. Should be positive or null.
151 void setExpireDays(@Nullable Integer expireDays);
153 <R> R accept(DynamoDBItemVisitor<R> visitor);