]> git.basschouten.com Git - openhab-addons.git/blob
2d39a15cfeec2ef297f551f792f7828d910532ff
[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.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 static DateQueryParameter of(LocalDate localDate) {
76         return new DateQueryParameter(localDate);
77     }
78
79     public static DateQueryParameter of(DateQueryParameterType dateType, Duration offset) {
80         if (offset.isZero()) {
81             return new DateQueryParameter(dateType);
82         } else {
83             return new DateQueryParameter(dateType, offset);
84         }
85     }
86
87     public static DateQueryParameter of(DateQueryParameter parameter, Duration offset) {
88         DateQueryParameterType parameterType = parameter.dateType;
89         if (parameterType == null) {
90             return parameter;
91         }
92         Duration parameterOffset = parameter.offset;
93         if (parameterOffset == null || parameterOffset.isZero()) {
94             return of(parameterType, offset);
95         }
96         return of(parameterType, parameterOffset.plus(offset));
97     }
98
99     public static DateQueryParameter of(DateQueryParameterType dateType) {
100         return new DateQueryParameter(dateType);
101     }
102 }