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.bticinosmarther.internal.api.dto;
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.NAME_SEPARATOR;
17 import java.util.List;
18 import java.util.Optional;
19 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.bticinosmarther.internal.util.StringUtil;
26 * The {@code Location} class defines the dto for Smarther API location object.
28 * @author Fabio Possieri - Initial contribution
31 public class Location {
33 private String plantId;
35 private @Nullable String subscriptionId;
36 private @Nullable String endpointUrl;
39 * Constructs a new {@code Location} with the given plant and subscription.
42 * the location plant to use
44 * the notification subscription endpoint to use, may be {@code null}
46 private Location(Plant plant, @Nullable Subscription subscription) {
48 this.plantId = plant.getId();
49 this.name = plant.getName();
50 if (subscription != null) {
51 this.subscriptionId = subscription.getSubscriptionId();
52 this.endpointUrl = subscription.getEndpointUrl();
57 * Returns a new {@code Location} with the given plant and subscription.
60 * the location plant to use
62 * the notification subscription endpoint to use, may be {@code null}
64 * @return the newly created Location object
66 public static Location fromPlant(Plant plant, @Nullable Subscription subscription) {
67 return new Location(plant, subscription);
71 * Returns a new {@code Location} with the given plant and no subscription.
74 * the location plant to use
76 * @return the newly created Location object
78 public static Location fromPlant(Plant plant) {
79 return new Location(plant, null);
83 * Returns a new {@code Location} with the given plant and optional subscription.
86 * the location plant to use
88 * the optional notification subscription endpoint to use, may contain no subscription
90 * @return the newly created Location object
92 public static Location fromPlant(Plant plant, Optional<Subscription> subscription) {
93 return (subscription.isPresent()) ? new Location(plant, subscription.get()) : new Location(plant, null);
97 * Returns the plant identifier associated with this location.
99 * @return a string containing the plant identifier
101 public String getPlantId() {
106 * Returns the plant name associated with this location.
108 * @return a string containing the plant name
110 public String getName() {
115 * Tells whether the location has an associated subscription.
117 * @return {@code true} if the location has a subscription, {@code false} otherwise
119 public boolean hasSubscription() {
120 return !StringUtil.isBlank(subscriptionId);
124 * Sets the notification subscription details for the location.
126 * @param subscriptionId
127 * the subscription identifier to use
129 * the notification endpoint to use
131 public void setSubscription(String subscriptionId, String endpointUrl) {
132 this.subscriptionId = subscriptionId;
133 this.endpointUrl = endpointUrl;
137 * Unsets the notification subscription details for the location.
138 * I.e. resets all of its details to {@code null}.
140 public void unsetSubscription() {
141 this.subscriptionId = null;
142 this.endpointUrl = null;
146 * Returns the notification subscription identifier for this location.
148 * @return a string containing the subscription identifier, may be {@code null}
150 public @Nullable String getSubscriptionId() {
151 return subscriptionId;
155 * Returns the notification endpoint for this location.
157 * @return a string containing the notification endpoint, may be {@code null}
159 public @Nullable String getEndpointUrl() {
164 * Converts a list of {@link Location} objects into a string containing the location names, comma separated.
167 * the list of location objects to be converted, may be {@code null}
169 * @return a string containing the comma separated location names, or {@code null} if the list is {@code null} or
172 public static @Nullable String toNameString(@Nullable List<Location> locations) {
173 if (locations == null || locations.isEmpty()) {
176 return locations.stream().map(a -> a.getName()).collect(Collectors.joining(NAME_SEPARATOR));
180 public String toString() {
181 return String.format("plantId=%s, name=%s, subscriptionId=%s, endpointUrl=%s", plantId, name, subscriptionId,