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