]> git.basschouten.com Git - openhab-addons.git/blob
fb9c1e2bb6056b519c959fdc83aa914a91a5aa1c
[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.bticinosmarther.internal.api.dto;
14
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.NAME_SEPARATOR;
16
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.bticinosmarther.internal.util.StringUtil;
24
25 /**
26  * The {@code Location} class defines the dto for Smarther API location object.
27  *
28  * @author Fabio Possieri - Initial contribution
29  */
30 @NonNullByDefault
31 public class Location {
32
33     private String plantId;
34     private String name;
35     private @Nullable String subscriptionId;
36     private @Nullable String endpointUrl;
37
38     /**
39      * Constructs a new {@code Location} with the given plant and subscription.
40      *
41      * @param plant
42      *            the location plant to use
43      * @param subscription
44      *            the notification subscription endpoint to use, may be {@code null}
45      */
46     private Location(Plant plant, @Nullable Subscription subscription) {
47         super();
48         this.plantId = plant.getId();
49         this.name = plant.getName();
50         if (subscription != null) {
51             this.subscriptionId = subscription.getSubscriptionId();
52             this.endpointUrl = subscription.getEndpointUrl();
53         }
54     }
55
56     /**
57      * Returns a new {@code Location} with the given plant and subscription.
58      *
59      * @param plant
60      *            the location plant to use
61      * @param subscription
62      *            the notification subscription endpoint to use, may be {@code null}
63      *
64      * @return the newly created Location object
65      */
66     public static Location fromPlant(Plant plant, @Nullable Subscription subscription) {
67         return new Location(plant, subscription);
68     }
69
70     /**
71      * Returns a new {@code Location} with the given plant and no subscription.
72      *
73      * @param plant
74      *            the location plant to use
75      *
76      * @return the newly created Location object
77      */
78     public static Location fromPlant(Plant plant) {
79         return new Location(plant, null);
80     }
81
82     /**
83      * Returns a new {@code Location} with the given plant and optional subscription.
84      *
85      * @param plant
86      *            the location plant to use
87      * @param subscription
88      *            the optional notification subscription endpoint to use, may contain no subscription
89      *
90      * @return the newly created Location object
91      */
92     public static Location fromPlant(Plant plant, Optional<Subscription> subscription) {
93         return (subscription.isPresent()) ? new Location(plant, subscription.get()) : new Location(plant, null);
94     }
95
96     /**
97      * Returns the plant identifier associated with this location.
98      *
99      * @return a string containing the plant identifier
100      */
101     public String getPlantId() {
102         return plantId;
103     }
104
105     /**
106      * Returns the plant name associated with this location.
107      *
108      * @return a string containing the plant name
109      */
110     public String getName() {
111         return name;
112     }
113
114     /**
115      * Tells whether the location has an associated subscription.
116      *
117      * @return {@code true} if the location has a subscription, {@code false} otherwise
118      */
119     public boolean hasSubscription() {
120         return !StringUtil.isBlank(subscriptionId);
121     }
122
123     /**
124      * Sets the notification subscription details for the location.
125      *
126      * @param subscriptionId
127      *            the subscription identifier to use
128      * @param endpointUrl
129      *            the notification endpoint to use
130      */
131     public void setSubscription(String subscriptionId, String endpointUrl) {
132         this.subscriptionId = subscriptionId;
133         this.endpointUrl = endpointUrl;
134     }
135
136     /**
137      * Unsets the notification subscription details for the location.
138      * I.e. resets all of its details to {@code null}.
139      */
140     public void unsetSubscription() {
141         this.subscriptionId = null;
142         this.endpointUrl = null;
143     }
144
145     /**
146      * Returns the notification subscription identifier for this location.
147      *
148      * @return a string containing the subscription identifier, may be {@code null}
149      */
150     public @Nullable String getSubscriptionId() {
151         return subscriptionId;
152     }
153
154     /**
155      * Returns the notification endpoint for this location.
156      *
157      * @return a string containing the notification endpoint, may be {@code null}
158      */
159     public @Nullable String getEndpointUrl() {
160         return endpointUrl;
161     }
162
163     /**
164      * Converts a list of {@link Location} objects into a string containing the location names, comma separated.
165      *
166      * @param locations
167      *            the list of location objects to be converted, may be {@code null}
168      *
169      * @return a string containing the comma separated location names, or {@code null} if the list is {@code null} or
170      *         empty.
171      */
172     public static @Nullable String toNameString(@Nullable List<Location> locations) {
173         if (locations == null || locations.isEmpty()) {
174             return null;
175         }
176         return locations.stream().map(a -> a.getName()).collect(Collectors.joining(NAME_SEPARATOR));
177     }
178
179     @Override
180     public String toString() {
181         return String.format("plantId=%s, name=%s, subscriptionId=%s, endpointUrl=%s", plantId, name, subscriptionId,
182                 endpointUrl);
183     }
184 }