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.energidataservice.internal.config;
15 import java.time.LocalDate;
16 import java.time.format.DateTimeParseException;
17 import java.util.Arrays;
18 import java.util.HashSet;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.energidataservice.internal.api.ChargeTypeCode;
24 import org.openhab.binding.energidataservice.internal.api.DateQueryParameter;
25 import org.openhab.binding.energidataservice.internal.api.DateQueryParameterType;
28 * The {@link DatahubPriceConfiguration} class contains fields mapping channel configuration parameters.
30 * @author Jacob Laursen - Initial contribution
33 public class DatahubPriceConfiguration {
36 * Comma-separated list of charge type codes, e.g. "CD,CD R".
38 public String chargeTypeCodes = "";
41 * Comma-separated list of notes, e.g. "Nettarif C".
43 public String notes = "";
46 * Query start date parameter expressed as either yyyy-mm-dd or one of StartOfDay, StartOfMonth or StartOfYear.
48 public String start = "";
51 * Check if any filter values are provided.
53 * @return true if either charge type codes, notes or query start date is provided.
55 public boolean hasAnyFilterOverrides() {
56 return !chargeTypeCodes.isBlank() || !notes.isBlank() || !start.isBlank();
60 * Get parsed set of charge type codes from comma-separated string.
62 * @return Set of charge type codes.
64 public Set<ChargeTypeCode> getChargeTypeCodes() {
65 return chargeTypeCodes.isBlank() ? new HashSet<>()
66 : new HashSet<ChargeTypeCode>(
67 Arrays.stream(chargeTypeCodes.split(",")).map(ChargeTypeCode::new).toList());
71 * Get parsed set of notes from comma-separated string.
73 * @return Set of notes.
75 public Set<String> getNotes() {
76 return notes.isBlank() ? new HashSet<>() : new HashSet<String>(Arrays.asList(notes.split(",")));
80 * Get query start parameter.
82 * @return null if invalid, otherwise an initialized {@link DateQueryParameter}.
84 public @Nullable DateQueryParameter getStart() {
85 if (start.isBlank()) {
86 return DateQueryParameter.EMPTY;
88 if (start.equals(DateQueryParameterType.START_OF_DAY.toString())) {
89 return DateQueryParameter.of(DateQueryParameterType.START_OF_DAY);
91 if (start.equals(DateQueryParameterType.START_OF_MONTH.toString())) {
92 return DateQueryParameter.of(DateQueryParameterType.START_OF_MONTH);
94 if (start.equals(DateQueryParameterType.START_OF_YEAR.toString())) {
95 return DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR);
98 return DateQueryParameter.of(LocalDate.parse(start));
99 } catch (DateTimeParseException e) {