]> git.basschouten.com Git - openhab-addons.git/blob
2d295e4063fc41cb47be8f2b3f7242c92d5ca7d9
[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.transport.message;
14
15 import java.util.Comparator;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * Definition (layout) of an Insteon message. Says which bytes go where.
24  * For more info, see the public Insteon Developer's Guide, 2nd edition,
25  * and the Insteon Modem Developer's Guide.
26  *
27  * @author Daniel Pfrommer - Initial contribution
28  * @author Rob Nielsen - Port to openHAB 2 insteon binding
29  * @author Jeremy Setton - Rewrite insteon binding
30  */
31 @NonNullByDefault
32 public class MsgDefinition {
33     private Map<String, Field> fields = new HashMap<>();
34
35     MsgDefinition() {
36     }
37
38     MsgDefinition(MsgDefinition definition) {
39         fields = new HashMap<>(definition.fields);
40     }
41
42     public List<Field> getFields() {
43         return fields.values().stream().sorted(Comparator.comparing(Field::getOffset)).toList();
44     }
45
46     public boolean containsField(String name) {
47         return fields.containsKey(name);
48     }
49
50     public void addField(Field field) {
51         fields.put(field.getName(), field);
52     }
53
54     /**
55      * Finds field of a given name
56      *
57      * @param name name of the field to search for
58      * @return reference to field
59      * @throws FieldException if no such field can be found
60      */
61     public Field getField(String name) throws FieldException {
62         Field field = fields.get(name);
63         if (field == null) {
64             throw new FieldException("field " + name + " not found");
65         }
66         return field;
67     }
68 }