]> git.basschouten.com Git - openhab-addons.git/blob
ecffe143139cdfcace334902ef47b7d382b6eb92
[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     @Override
56     public String toString() {
57         return String.format("NonConsecutiveBestpriceResult with %s", members.toString());
58     }
59
60     private void sort() {
61         if (!sorted) {
62             members.sort(new Comparator<AwattarPrice>() {
63                 @Override
64                 public int compare(AwattarPrice o1, AwattarPrice o2) {
65                     return Long.compare(o1.getStartTimestamp(), o2.getStartTimestamp());
66                 }
67             });
68         }
69     }
70
71     @Override
72     public String getHours() {
73         boolean second = false;
74         sort();
75         StringBuilder res = new StringBuilder();
76         for (AwattarPrice price : members) {
77             if (second) {
78                 res.append(',');
79             }
80             res.append(getHourFrom(price.getStartTimestamp(), zoneId));
81             second = true;
82         }
83         return res.toString();
84     }
85 }