]> git.basschouten.com Git - openhab-addons.git/blob
98c43f34e052073d0f2fb7aa191f61b92f71441b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.wlanthermo.internal;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.types.HSBType;
20
21 /**
22  * The {@link WlanThermoUtil} class provides conversion functions for the WlanThermo
23  *
24  * @author Christian Schlipp - Initial contribution
25  */
26 @NonNullByDefault
27 public class WlanThermoUtil {
28
29     public static String toColorName(String colorHex, Map<String, String> colorMappings, String defaultColorName) {
30         if (!colorHex.startsWith("#")) {
31             colorHex = "#" + colorHex;
32         }
33
34         for (Map.Entry<String, String> entry : colorMappings.entrySet()) {
35             if (entry.getValue().equalsIgnoreCase(colorHex)) {
36                 return entry.getKey();
37             }
38         }
39
40         return defaultColorName;
41     }
42
43     public static String toHex(HSBType hsb) {
44         return "#" + String.format("%02X", hsb.getRed().intValue()) + String.format("%02X", hsb.getGreen().intValue())
45                 + String.format("%02X", hsb.getBlue().intValue());
46     }
47
48     public static <T> T requireNonNull(@Nullable T obj) throws WlanThermoInputException {
49         if (obj == null) {
50             throw new WlanThermoInputException();
51         }
52         return obj;
53     }
54 }