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.binding.awattar.internal;
15 import static org.openhab.binding.awattar.internal.AwattarUtil.formatDate;
16 import static org.openhab.binding.awattar.internal.AwattarUtil.getHourFrom;
18 import java.time.Instant;
19 import java.time.ZoneId;
20 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
25 * Stores a consecutive bestprice result
27 * @author Wolfgang Klimt - initial contribution
30 public class AwattarConsecutiveBestPriceResult extends AwattarBestPriceResult {
32 private double priceSum = 0;
33 private int length = 0;
35 private ZoneId zoneId;
37 public AwattarConsecutiveBestPriceResult(List<AwattarPrice> prices, ZoneId zoneId) {
40 StringBuilder hours = new StringBuilder();
41 boolean second = false;
42 for (AwattarPrice price : prices) {
43 priceSum += price.getPrice();
45 updateStart(price.getStartTimestamp());
46 updateEnd(price.getEndTimestamp());
50 hours.append(getHourFrom(price.getStartTimestamp(), zoneId));
53 this.hours = hours.toString();
57 public boolean isActive() {
58 return contains(Instant.now().toEpochMilli());
61 public boolean contains(long timestamp) {
62 return timestamp >= getStart() && timestamp < getEnd();
65 public double getPriceSum() {
70 public String toString() {
71 return String.format("{%s, %s, %.2f}", formatDate(getStart(), zoneId), formatDate(getEnd(), zoneId),
76 public String getHours() {