]> git.basschouten.com Git - openhab-addons.git/blob
41fd05b77254b7632845dbf5159721104cdd6428
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.binding.awattar.internal;
14
15 import java.time.Instant;
16 import java.time.ZoneId;
17 import java.time.ZonedDateTime;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 /**
22  * Class to store hourly price data.
23  *
24  * @author Wolfgang Klimt - initial contribution
25  */
26 @NonNullByDefault
27 public class AwattarPrice implements Comparable<AwattarPrice> {
28     private final Double price;
29     private final long endTimestamp;
30     private final long startTimestamp;
31
32     private final int hour;
33
34     public AwattarPrice(double price, long startTimestamp, long endTimestamp, ZoneId zoneId) {
35         this.price = price;
36         this.endTimestamp = endTimestamp;
37         this.startTimestamp = startTimestamp;
38         this.hour = ZonedDateTime.ofInstant(Instant.ofEpochMilli(startTimestamp), zoneId).getHour();
39     }
40
41     public long getStartTimestamp() {
42         return startTimestamp;
43     }
44
45     public long getEndTimestamp() {
46         return endTimestamp;
47     }
48
49     public double getPrice() {
50         return price;
51     }
52
53     @Override
54     public String toString() {
55         return String.format("(%1$tF %1$tR - %2$tR: %3$.3f)", startTimestamp, endTimestamp, getPrice());
56     }
57
58     public int getHour() {
59         return hour;
60     }
61
62     @Override
63     public int compareTo(AwattarPrice o) {
64         return price.compareTo(o.price);
65     }
66
67     public boolean isBetween(long start, long end) {
68         return startTimestamp >= start && endTimestamp <= end;
69     }
70
71     public boolean contains(long timestamp) {
72         return startTimestamp <= timestamp && endTimestamp > timestamp;
73     }
74 }