]> git.basschouten.com Git - openhab-addons.git/blob
2971c9f446d556c4970e48cfa5991a8fdcb44a15
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.insteon.internal.message;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19
20 /**
21  * Defines the data types that can be used in the fields of a message.
22  *
23  * @author Daniel Pfrommer - Initial contribution
24  * @author Rob Nielsen - Port to openHAB 2 insteon binding
25  */
26 @NonNullByDefault
27 public enum DataType {
28     BYTE("byte", 1),
29     INT("int", 4),
30     FLOAT("float", 4),
31     ADDRESS("address", 3),
32     INVALID("INVALID", -1);
33
34     private static Map<String, DataType> typeMap = new HashMap<>();
35
36     private int size;
37     private String name;
38
39     static {
40         typeMap.put(BYTE.getName(), BYTE);
41         typeMap.put(INT.getName(), INT);
42         typeMap.put(FLOAT.getName(), FLOAT);
43         typeMap.put(ADDRESS.getName(), ADDRESS);
44     }
45
46     /**
47      * Constructor
48      *
49      * @param name the name of the data type
50      * @param size the size (in bytes) of this data type
51      */
52     DataType(String name, int size) {
53         this.size = size;
54         this.name = name;
55     }
56
57     /**
58      * @return the size (in bytes) of this data type
59      */
60     public int getSize() {
61         return size;
62     }
63
64     /**
65      * @return clear text string with the name
66      */
67     public String getName() {
68         return name;
69     }
70
71     /**
72      * Turns a string into the corresponding data type
73      *
74      * @param name the string to translate to a type
75      * @return the data type corresponding to the name string, or null if not found
76      */
77     public static DataType getDataType(String name) {
78         DataType dataType = typeMap.get(name);
79         if (dataType != null) {
80             return dataType;
81         } else {
82             throw new IllegalArgumentException("Unable to find data type for " + name);
83         }
84     }
85 }