2 * Copyright (c) 2010-2024 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.api;
15 import java.time.Duration;
16 import java.time.LocalDate;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * This class represents a query parameter of type {@link LocalDate} or a
23 * dynamic date defined as {@link DateQueryParameterType} with an optional offset.
25 * @author Jacob Laursen - Initial contribution
28 public class DateQueryParameter {
30 public static final DateQueryParameter EMPTY = new DateQueryParameter();
32 private @Nullable LocalDate date;
33 private @Nullable Duration offset;
34 private @Nullable DateQueryParameterType dateType;
36 private DateQueryParameter() {
39 public DateQueryParameter(LocalDate date) {
43 public DateQueryParameter(DateQueryParameterType dateType, Duration offset) {
44 this.dateType = dateType;
48 public DateQueryParameter(DateQueryParameterType dateType) {
49 this.dateType = dateType;
53 public String toString() {
54 LocalDate date = this.date;
56 return date.toString();
58 DateQueryParameterType dateType = this.dateType;
59 if (dateType != null) {
60 Duration offset = this.offset;
61 if (offset == null || offset.isZero()) {
62 return dateType.toString();
64 return dateType.toString()
65 + (offset.isNegative() ? "-" + offset.abs().toString() : "+" + offset.toString());
71 public boolean isEmpty() {
75 public @Nullable DateQueryParameterType getDateType() {
79 public @Nullable LocalDate getDate() {
83 public static DateQueryParameter of(LocalDate localDate) {
84 return new DateQueryParameter(localDate);
87 public static DateQueryParameter of(DateQueryParameterType dateType, Duration offset) {
88 if (offset.isZero()) {
89 return new DateQueryParameter(dateType);
91 return new DateQueryParameter(dateType, offset);
95 public static DateQueryParameter of(DateQueryParameter parameter, Duration offset) {
96 DateQueryParameterType parameterType = parameter.dateType;
97 if (parameterType == null) {
100 Duration parameterOffset = parameter.offset;
101 if (parameterOffset == null || parameterOffset.isZero()) {
102 return of(parameterType, offset);
104 return of(parameterType, parameterOffset.plus(offset));
107 public static DateQueryParameter of(DateQueryParameterType dateType) {
108 return new DateQueryParameter(dateType);