]> git.basschouten.com Git - openhab-addons.git/blob
eabb656aea7789606976d614cc70caef50ba8945
[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.rfxcom.internal.messages;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.rfxcom.internal.exceptions.RFXComUnsupportedValueException;
17
18 /**
19  * An Utility class to handle {@link ByteEnumWrapper} instances
20  *
21  * @author Martin van Wingerden - Initial contribution
22  */
23 @NonNullByDefault
24 public class ByteEnumUtil {
25     private ByteEnumUtil() {
26         // deliberately empty
27     }
28
29     public static <T extends ByteEnumWrapper> T fromByte(Class<T> typeClass, int input)
30             throws RFXComUnsupportedValueException {
31         for (T enumValue : typeClass.getEnumConstants()) {
32             if (enumValue.toByte() == input) {
33                 return enumValue;
34             }
35         }
36
37         throw new RFXComUnsupportedValueException(typeClass, input);
38     }
39
40     public static <T extends ByteEnumWrapper> T convertSubType(Class<T> typeClass, String subType)
41             throws RFXComUnsupportedValueException {
42         for (T enumValue : typeClass.getEnumConstants()) {
43             if (enumValue.toString().equals(subType)) {
44                 return enumValue;
45             }
46         }
47
48         try {
49             int byteValue = Integer.parseInt(subType);
50             return fromByte(typeClass, byteValue);
51         } catch (NumberFormatException e) {
52             throw new RFXComUnsupportedValueException(typeClass, subType);
53         }
54     }
55
56     public static <T extends ByteEnumWrapperWithSupportedSubTypes<?>> T fromByte(Class<T> typeClass, int input,
57             Object subType) throws RFXComUnsupportedValueException {
58         for (T enumValue : typeClass.getEnumConstants()) {
59             if (enumValue.toByte() == input && enumValue.supportedBySubTypes().contains(subType)) {
60                 return enumValue;
61             }
62         }
63
64         throw new RFXComUnsupportedValueException(RFXComLighting5Message.Commands.class, input, subType);
65     }
66 }