]> git.basschouten.com Git - openhab-addons.git/blob
36fda8dde5ba6861d6d69909b230f34a70ecace3
[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.lcn.internal.common;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Represents an LCN module address.
22  * Can be used as a key in maps.
23  * Hash codes are guaranteed to be unique as long as {@link #isValid()} is true.
24  *
25  * @author Tobias Jüttner - Initial Contribution
26  */
27 @NonNullByDefault
28 public class LcnAddrMod extends LcnAddr implements Comparable<LcnAddrMod> {
29     private final Logger logger = LoggerFactory.getLogger(LcnAddrMod.class);
30     private final int moduleId;
31
32     /**
33      * Constructs a module address with (logical) segment id and module id.
34      *
35      * @param segId the segment id
36      * @param modId the module id
37      */
38     public LcnAddrMod(int segId, int modId) {
39         super(segId);
40         this.moduleId = modId;
41     }
42
43     /**
44      * Gets the module id.
45      *
46      * @return the module id
47      */
48     public int getModuleId() {
49         return this.moduleId;
50     }
51
52     @Override
53     public boolean isValid() {
54         // segId:
55         // 0 = Local, 1..2 = Not allowed (but "seen in the wild")
56         // 3 = Broadcast, 4 = Status messages, 5..127, 128 = Segment-bus disabled (valid value)
57         // modId:
58         // 1 = LCN-PRO, 2 = LCN-GVS/LCN-W, 4 = PCHK, 5..254, 255 = Unprog. (valid, but irrelevant here)
59         return this.segmentId >= 0 && this.segmentId <= 128 && this.moduleId >= 1 && this.moduleId <= 254;
60     }
61
62     @Override
63     public boolean isGroup() {
64         return false;
65     }
66
67     @Override
68     public int getId() {
69         return this.moduleId;
70     }
71
72     @Override
73     public int hashCode() {
74         // Reversing the bits helps to generate better balanced trees as ids tend to be "user-sorted"
75         try {
76             if (this.isValid()) {
77                 return ReverseNumber.reverseUInt8(this.moduleId) << 8 + ReverseNumber.reverseUInt8(this.segmentId);
78             }
79         } catch (LcnException ex) {
80             logger.warn("Could not calculate hash code");
81         }
82         return -1;
83     }
84
85     @Override
86     public boolean equals(@Nullable Object obj) {
87         if (!(obj instanceof LcnAddrMod)) {
88             return false;
89         }
90         return this.segmentId == ((LcnAddrMod) obj).segmentId && this.moduleId == ((LcnAddrMod) obj).moduleId;
91     }
92
93     @Override
94     public int compareTo(LcnAddrMod other) {
95         return this.hashCode() - other.hashCode();
96     }
97
98     @Override
99     public String toString() {
100         return this.isValid() ? String.format("S%03dM%03d", this.segmentId, this.moduleId) : "Invalid";
101     }
102 }