]> git.basschouten.com Git - openhab-addons.git/blob
3c375a6b1bd966b710f5d2a9bf8c8933598f6847
[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.io.neeo.internal.servletservices.models;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.io.neeo.internal.models.NeeoDevice;
20 import org.openhab.io.neeo.internal.models.NeeoDeviceChannel;
21
22 /**
23  * The class that encapsulates a result of a NEEO Brain call. THe result can be successful or not, include a message and
24  * a device
25  *
26  * @author Tim Roberts - Initial Contribution
27  */
28 @NonNullByDefault
29 public class ReturnStatus {
30
31     /** The static success helper */
32     public static final ReturnStatus SUCCESS = new ReturnStatus(true);
33
34     /** True if the call was successful, false otherwise */
35     private final boolean success;
36
37     /** The optional message if not successful */
38     private final @Nullable String message;
39
40     /** The optional device if successful */
41     private final @Nullable NeeoDevice device;
42
43     /** The optional channel if successful */
44     private final @Nullable List<NeeoDeviceChannel> channels;
45
46     /**
47      * Creates a return status of true or not (with no message or device)
48      *
49      * @param success whether the call was successful
50      */
51     public ReturnStatus(boolean success) {
52         this(success, null, null, null);
53     }
54
55     /**
56      * Creates a return status of SUCCESS with a device. If device is null, same result as calling 'constructor(true)'
57      *
58      * @param device the possibly null device
59      */
60     public ReturnStatus(NeeoDevice device) {
61         this(true, null, device, null);
62     }
63
64     /**
65      * Creates a return status of SUCCESS with a channel. If channel is null, same result as calling 'constructor(true)'
66      *
67      * @param channels the possibly null channel
68      */
69     public ReturnStatus(@Nullable List<NeeoDeviceChannel> channels) {
70         this(true, null, null, channels);
71     }
72
73     /**
74      * Creates the return status with the specified message. If the message is empty, same result as calling
75      * 'constructor(true)'. If not empty, success is set to false
76      *
77      * @param message the possibly null, possibly empty message
78      */
79     public ReturnStatus(@Nullable String message) {
80         this(message == null || message.isEmpty(), message, null, null);
81     }
82
83     /**
84      * Creates the return status with the specified message and success indicator
85      *
86      * @param success true if successful, false otherwise
87      * @param message the possibly null, possibly empty message
88      */
89     public ReturnStatus(boolean success, String message) {
90         this(success, message, null, null);
91     }
92
93     /**
94      * Creates the returns status for the success, message and optional device or channel.
95      *
96      * @param success whether the call was successful
97      * @param message the possibly null, possibly empty message
98      * @param device the possibly null device
99      * @param channels the possibly null channels
100      */
101     private ReturnStatus(boolean success, @Nullable String message, @Nullable NeeoDevice device,
102             @Nullable List<NeeoDeviceChannel> channels) {
103         this.success = success;
104         this.message = message;
105         this.device = device;
106         this.channels = channels;
107     }
108
109     /**
110      * Checks if the call was successful
111      *
112      * @return true if successful, false otherwise
113      */
114     public boolean isSuccess() {
115         return success;
116     }
117
118     /**
119      * Gets the related message
120      *
121      * @return the possibly empty, possibly null message
122      */
123     public @Nullable String getMessage() {
124         return message;
125     }
126
127     /**
128      * Gets the related device
129      *
130      * @return the possibly null device
131      */
132     public @Nullable NeeoDevice getDevice() {
133         return device;
134     }
135
136     /**
137      * Gets the related channel
138      *
139      * @return the possibly null channel
140      */
141     public @Nullable List<NeeoDeviceChannel> getChannels() {
142         return channels;
143     }
144
145     @Override
146     public String toString() {
147         return "ReturnStatus [success=" + success + ", message=" + message + ", device=" + device + ", channels="
148                 + channels + "]";
149     }
150 }