]> git.basschouten.com Git - openhab-addons.git/blob
989ca4a5701050fb7fb265879c5409e3e865586e
[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.mielecloud.internal.webservice.api.json;
14
15 import java.util.Objects;
16 import java.util.Optional;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 import com.google.gson.Gson;
22 import com.google.gson.JsonSyntaxException;
23
24 /**
25  * Immutable POJO representing an error message. Queried from the Miele REST API.
26  *
27  * @author Björn Lange - Initial contribution
28  */
29 @NonNullByDefault
30 public class ErrorMessage {
31     @Nullable
32     private String message;
33
34     /**
35      * Creates a new {@link ErrorMessage} from the given Json text.
36      *
37      * @param json The Json text.
38      * @return The created {@link ErrorMessage}.
39      * @throws MieleSyntaxException if parsing the data from {@code json} fails.
40      */
41     public static ErrorMessage fromJson(String json) {
42         try {
43             ErrorMessage errorMessage = new Gson().fromJson(json, ErrorMessage.class);
44             if (errorMessage == null) {
45                 throw new MieleSyntaxException("Failed to parse Json.");
46             }
47             return errorMessage;
48         } catch (JsonSyntaxException e) {
49             throw new MieleSyntaxException("Failed to parse Json.", e);
50         }
51     }
52
53     public Optional<String> getMessage() {
54         return Optional.ofNullable(message);
55     }
56
57     @Override
58     public int hashCode() {
59         return Objects.hash(message);
60     }
61
62     @Override
63     public boolean equals(@Nullable Object obj) {
64         if (this == obj) {
65             return true;
66         }
67         if (obj == null) {
68             return false;
69         }
70         if (getClass() != obj.getClass()) {
71             return false;
72         }
73         ErrorMessage other = (ErrorMessage) obj;
74         return Objects.equals(message, other.message);
75     }
76 }