]> git.basschouten.com Git - openhab-addons.git/blob
0c272fdad79af2563514b0c5a95c3b78a707a9d8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.mapdb.internal;
14
15 import java.text.DateFormat;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18 import java.util.Date;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.persistence.HistoricItem;
23 import org.openhab.core.persistence.PersistenceItemInfo;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.UnDefType;
26
27 /**
28  * This is a Java bean used to persist item states with timestamps in the database.
29  *
30  * @author Jens Viebig - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class MapDbItem implements HistoricItem, PersistenceItemInfo {
35
36     private String name = "";
37     private State state = UnDefType.NULL;
38     private Date timestamp = new Date(0);
39
40     @Override
41     public String getName() {
42         return name;
43     }
44
45     public void setName(String name) {
46         this.name = name;
47     }
48
49     @Override
50     public State getState() {
51         return state;
52     }
53
54     public void setState(State state) {
55         this.state = state;
56     }
57
58     @Override
59     public ZonedDateTime getTimestamp() {
60         return ZonedDateTime.ofInstant(timestamp.toInstant(), ZoneId.systemDefault());
61     }
62
63     public void setTimestamp(Date timestamp) {
64         this.timestamp = timestamp;
65     }
66
67     @Override
68     public String toString() {
69         return DateFormat.getDateTimeInstance().format(timestamp) + ": " + name + " -> " + state.toString();
70     }
71
72     @Override
73     public @Nullable Integer getCount() {
74         return Integer.valueOf(1);
75     }
76
77     @Override
78     public @Nullable Date getEarliest() {
79         return null;
80     }
81
82     @Override
83     public @Nullable Date getLatest() {
84         return null;
85     }
86
87     public boolean isValid() {
88         return name != null && state != null && timestamp != null;
89     }
90 }