Skip to content

Commit 2e87174

Browse files
authored
[insteon] Refactor transport message field type (openhab#18272)
Signed-off-by: Jeremy Setton <[email protected]>
1 parent 1b6e237 commit 2e87174

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/transport/message/Field.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
public final class Field {
3333
private final String name;
3434
private final int offset;
35-
private final DataType type;
35+
private final FieldType type;
3636

37-
public Field(String name, int offset, DataType type) {
37+
public Field(String name, int offset, FieldType type) {
3838
this.name = name;
3939
this.offset = offset;
4040
this.type = type;
@@ -48,7 +48,7 @@ public int getOffset() {
4848
return offset;
4949
}
5050

51-
private void check(int len, DataType t) throws FieldException {
51+
private void check(int len, FieldType t) throws FieldException {
5252
if (offset + type.getSize() > len) {
5353
throw new FieldException("field write beyond end of msg");
5454
}
@@ -79,7 +79,7 @@ public void set(byte[] data, String value) throws FieldException, IllegalArgumen
7979
* @throws FieldException
8080
*/
8181
public void setByte(byte[] data, byte b) throws FieldException {
82-
check(data.length, DataType.BYTE);
82+
check(data.length, FieldType.BYTE);
8383
data[offset] = b;
8484
}
8585

@@ -92,7 +92,7 @@ public void setByte(byte[] data, byte b) throws FieldException {
9292
* @throws FieldException
9393
*/
9494
public void setAddress(byte[] data, InsteonAddress address) throws FieldException {
95-
check(data.length, DataType.ADDRESS);
95+
check(data.length, FieldType.ADDRESS);
9696
System.arraycopy(address.getBytes(), 0, data, offset, type.getSize());
9797
}
9898

@@ -104,7 +104,7 @@ public void setAddress(byte[] data, InsteonAddress address) throws FieldExceptio
104104
* @throws FieldException
105105
*/
106106
public byte getByte(byte[] data) throws FieldException {
107-
check(data.length, DataType.BYTE);
107+
check(data.length, FieldType.BYTE);
108108
return data[offset];
109109
}
110110

@@ -116,7 +116,7 @@ public byte getByte(byte[] data) throws FieldException {
116116
* @throws FieldException
117117
*/
118118
public InsteonAddress getAddress(byte[] data) throws FieldException {
119-
check(data.length, DataType.ADDRESS);
119+
check(data.length, FieldType.ADDRESS);
120120
byte[] address = Arrays.copyOfRange(data, offset, offset + type.getSize());
121121
return new InsteonAddress(address);
122122
}

bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/transport/message/DataType.java bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/transport/message/FieldType.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@
2121
import org.eclipse.jdt.annotation.Nullable;
2222

2323
/**
24-
* Defines the data types that can be used in the fields of a message.
24+
* The {@link FieldType} represents a field type
2525
*
2626
* @author Daniel Pfrommer - Initial contribution
2727
* @author Rob Nielsen - Port to openHAB 2 insteon binding
2828
* @author Jeremy Setton - Rewrite insteon binding
2929
*/
3030
@NonNullByDefault
31-
public enum DataType {
31+
public enum FieldType {
3232
BYTE("byte", 1),
3333
ADDRESS("address", 3);
3434

35-
private static final Map<String, DataType> NAME_MAP = Arrays.stream(values())
35+
private static final Map<String, FieldType> NAME_MAP = Arrays.stream(values())
3636
.collect(Collectors.toUnmodifiableMap(type -> type.name, Function.identity()));
3737

3838
private final String name;
3939
private final int size;
4040

41-
private DataType(String name, int size) {
41+
private FieldType(String name, int size) {
4242
this.name = name;
4343
this.size = size;
4444
}
@@ -51,13 +51,7 @@ public int getSize() {
5151
return size;
5252
}
5353

54-
/**
55-
* Factory method for getting a DataType from the data type name
56-
*
57-
* @param name the data type name
58-
* @return the data type if defined, otherwise null
59-
*/
60-
public static @Nullable DataType get(String name) {
54+
public static @Nullable FieldType get(String name) {
6155
return NAME_MAP.get(name);
6256
}
6357
}

bundles/org.openhab.binding.insteon/src/main/java/org/openhab/binding/insteon/internal/transport/message/MsgDefinitionRegistry.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private void parseMsgDefinition(Element element) throws SAXException {
153153
Element child = (Element) node;
154154
String nodeName = child.getNodeName();
155155
if (!"header".equals(nodeName)) {
156-
// Increment the offset by the field data type length
156+
// Increment the offset by the field type length
157157
offset += parseField(child, offset, data, fields);
158158
} else if (offset == 0) {
159159
headerLength = parseHeader(child, data, fields);
@@ -196,7 +196,7 @@ private int parseHeader(Element element, byte[] data, Map<String, Field> fields)
196196
Node node = nodes.item(i);
197197
if (node.getNodeType() == Node.ELEMENT_NODE) {
198198
Element child = (Element) node;
199-
// Increment the offset by the field data type length
199+
// Increment the offset by the field type length
200200
offset += parseField(child, offset, data, fields);
201201
}
202202
}
@@ -213,16 +213,16 @@ private int parseHeader(Element element, byte[] data, Map<String, Field> fields)
213213
* @param offset msg offset
214214
* @param data msg data to update
215215
* @param fields fields map to update
216-
* @return field data type length
216+
* @return field type length
217217
* @throws SAXException
218218
*/
219219
private int parseField(Element element, int offset, byte[] data, Map<String, Field> fields) throws SAXException {
220220
String name = element.getAttribute("name");
221-
DataType dataType = DataType.get(element.getNodeName());
222-
if (dataType == null) {
223-
throw new SAXException("invalid field data type");
221+
FieldType fieldType = FieldType.get(element.getNodeName());
222+
if (fieldType == null) {
223+
throw new SAXException("invalid field type");
224224
}
225-
Field field = new Field(name, offset, dataType);
225+
Field field = new Field(name, offset, fieldType);
226226
try {
227227
field.set(data, element.getTextContent());
228228
} catch (FieldException | IllegalArgumentException e) {
@@ -231,7 +231,7 @@ private int parseField(Element element, int offset, byte[] data, Map<String, Fie
231231
if (!name.isEmpty()) {
232232
fields.put(name, field);
233233
}
234-
return dataType.getSize();
234+
return fieldType.getSize();
235235
}
236236

237237
/**

0 commit comments

Comments
 (0)