]> git.basschouten.com Git - openhab-addons.git/blob
531534450db1fb10113abb45802702b0ba35dc0a
[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 group 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 LcnAddrGrp extends LcnAddr implements Comparable<LcnAddrMod> {
29     private final Logger logger = LoggerFactory.getLogger(LcnAddrGrp.class);
30     private final int groupId;
31
32     /**
33      * Constructs a group address with (logical) segment id and group id.
34      *
35      * @param segId the segment id
36      * @param grpId the group id
37      */
38     public LcnAddrGrp(int segId, int grpId) {
39         super(segId);
40         this.groupId = grpId;
41     }
42
43     /**
44      * Gets the group id.
45      *
46      * @return the group id
47      */
48     public int getGroupId() {
49         return this.groupId;
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         // grpId:
58         // 3 = Broadcast, 4 = Status messages, 5..254
59         return this.segmentId >= 0 && this.segmentId <= 128 && this.groupId >= 3 && this.groupId <= 254;
60     }
61
62     @Override
63     public boolean isGroup() {
64         return true;
65     }
66
67     @Override
68     public int getId() {
69         return this.groupId;
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.groupId) << 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 LcnAddrGrp)) {
88             return false;
89         }
90         return this.segmentId == ((LcnAddrGrp) obj).segmentId && this.groupId == ((LcnAddrGrp) obj).groupId;
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%03dG%03d", this.segmentId, this.groupId) : "Invalid";
101     }
102 }