]> git.basschouten.com Git - openhab-addons.git/blob
7db7fddd114d37f49a67bf20f6d915b5d0a23755
[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.binding.awattar.internal;
14
15 import static org.openhab.binding.awattar.internal.AwattarUtil.*;
16
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.util.ArrayList;
20 import java.util.Comparator;
21 import java.util.List;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24
25 /**
26  * Stores a non consecutive bestprice result
27  *
28  * @author Wolfgang Klimt - initial contribution
29  */
30 @NonNullByDefault
31 public class AwattarNonConsecutiveBestPriceResult extends AwattarBestPriceResult {
32
33     private List<AwattarPrice> members;
34     private ZoneId zoneId;
35     private boolean sorted = true;
36
37     public AwattarNonConsecutiveBestPriceResult(int size, ZoneId zoneId) {
38         super();
39         this.zoneId = zoneId;
40         members = new ArrayList<AwattarPrice>();
41     }
42
43     public void addMember(AwattarPrice member) {
44         sorted = false;
45         members.add(member);
46         updateStart(member.getStartTimestamp());
47         updateEnd(member.getEndTimestamp());
48     }
49
50     @Override
51     public boolean isActive() {
52         return members.stream().anyMatch(x -> x.contains(Instant.now().toEpochMilli()));
53     }
54
55     public String toString() {
56         return String.format("NonConsecutiveBestpriceResult with %s", members.toString());
57     }
58
59     private void sort() {
60         if (!sorted) {
61             members.sort(new Comparator<AwattarPrice>() {
62                 @Override
63                 public int compare(AwattarPrice o1, AwattarPrice o2) {
64                     return Long.compare(o1.getStartTimestamp(), o2.getStartTimestamp());
65                 }
66             });
67         }
68     }
69
70     public String getHours() {
71         boolean second = false;
72         sort();
73         StringBuilder res = new StringBuilder();
74         for (AwattarPrice price : members) {
75             if (second) {
76                 res.append(',');
77             }
78             res.append(getHourFrom(price.getStartTimestamp(), zoneId));
79             second = true;
80         }
81         return res.toString();
82     }
83 }