]> git.basschouten.com Git - openhab-addons.git/blob
bca4d0586d3376f2e207a178eff65603413d6b86
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.energidataservice.internal.api;
14
15 import java.time.Duration;
16 import java.time.LocalDate;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 /**
22  * This class represents a query parameter of type {@link LocalDate} or a
23  * dynamic date defined as {@link DateQueryParameterType} with an optional offset.
24  *
25  * @author Jacob Laursen - Initial contribution
26  */
27 @NonNullByDefault
28 public class DateQueryParameter {
29
30     public static final DateQueryParameter EMPTY = new DateQueryParameter();
31
32     private @Nullable LocalDate date;
33     private @Nullable Duration offset;
34     private @Nullable DateQueryParameterType dateType;
35
36     private DateQueryParameter() {
37     }
38
39     public DateQueryParameter(LocalDate date) {
40         this.date = date;
41     }
42
43     public DateQueryParameter(DateQueryParameterType dateType, Duration offset) {
44         this.dateType = dateType;
45         this.offset = offset;
46     }
47
48     public DateQueryParameter(DateQueryParameterType dateType) {
49         this.dateType = dateType;
50     }
51
52     @Override
53     public String toString() {
54         LocalDate date = this.date;
55         if (date != null) {
56             return date.toString();
57         }
58         DateQueryParameterType dateType = this.dateType;
59         if (dateType != null) {
60             Duration offset = this.offset;
61             if (offset == null || offset.isZero()) {
62                 return dateType.toString();
63             } else {
64                 return dateType.toString()
65                         + (offset.isNegative() ? "-" + offset.abs().toString() : "+" + offset.toString());
66             }
67         }
68         return "null";
69     }
70
71     public boolean isEmpty() {
72         return this == EMPTY;
73     }
74
75     public @Nullable DateQueryParameterType getDateType() {
76         return dateType;
77     }
78
79     public @Nullable LocalDate getDate() {
80         return date;
81     }
82
83     public static DateQueryParameter of(LocalDate localDate) {
84         return new DateQueryParameter(localDate);
85     }
86
87     public static DateQueryParameter of(DateQueryParameterType dateType, Duration offset) {
88         if (offset.isZero()) {
89             return new DateQueryParameter(dateType);
90         } else {
91             return new DateQueryParameter(dateType, offset);
92         }
93     }
94
95     public static DateQueryParameter of(DateQueryParameter parameter, Duration offset) {
96         DateQueryParameterType parameterType = parameter.dateType;
97         if (parameterType == null) {
98             return parameter;
99         }
100         Duration parameterOffset = parameter.offset;
101         if (parameterOffset == null || parameterOffset.isZero()) {
102             return of(parameterType, offset);
103         }
104         return of(parameterType, parameterOffset.plus(offset));
105     }
106
107     public static DateQueryParameter of(DateQueryParameterType dateType) {
108         return new DateQueryParameter(dateType);
109     }
110 }