]> git.basschouten.com Git - openhab-addons.git/blob
22e7f8b3b03b0c97ea48056cf8f72f0372c6ac54
[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.hpprinter.internal.api;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
19  * The {@link HPServerResult} is responsible for returning the
20  * reading of data from the HP Embedded Web Server.
21  *
22  * @author Stewart Cossey - Initial contribution
23  */
24 @NonNullByDefault
25 public class HPServerResult<result> {
26     private final RequestStatus status;
27     private final @Nullable result data;
28     private final String errorMessage;
29
30     public HPServerResult(RequestStatus status, @Nullable String errorMessage) {
31         this.status = status;
32         this.data = null;
33         this.errorMessage = errorMessage != null ? errorMessage : "";
34     }
35
36     public HPServerResult(result data) {
37         this.status = RequestStatus.SUCCESS;
38         this.data = data;
39         this.errorMessage = "";
40     }
41
42     @SuppressWarnings("null")
43     public result getData() {
44         final result localData = data;
45         if (status != RequestStatus.SUCCESS || localData == null) {
46             throw new IllegalStateException("No data available for result");
47         }
48         return localData;
49     }
50
51     public RequestStatus getStatus() {
52         return status;
53     }
54
55     public String getErrorMessage() {
56         return errorMessage;
57     }
58
59     public enum RequestStatus {
60         SUCCESS,
61         TIMEOUT,
62         ERROR
63     }
64 }