]> git.basschouten.com Git - openhab-addons.git/blob
7e7698809d3938cbf761169d438f691dc4346c6f
[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.satel.internal.event;
14
15 import java.util.BitSet;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * Event class describing changes in Integra state since last state read.
21  *
22  * @author Krzysztof Goworek - Initial contribution
23  */
24 @NonNullByDefault
25 public class NewStatesEvent implements SatelEvent {
26
27     private BitSet newStates;
28
29     /**
30      * Constructs event class from given {@link BitSet}.
31      *
32      * @param newStates changed states as {@link BitSet}
33      */
34     public NewStatesEvent(BitSet newStates) {
35         this.newStates = newStates;
36     }
37
38     /**
39      * Constructs event class from given byte array.
40      *
41      * @param newStates changed states as byte array
42      */
43     public NewStatesEvent(byte[] newStates) {
44         this(BitSet.valueOf(newStates));
45     }
46
47     /**
48      * Checks if specified state has changed since last read.
49      *
50      * @param nbr state number to check
51      * @return <code>true</code> if state has changed
52      */
53     public boolean isNew(int nbr) {
54         return newStates.get(nbr);
55     }
56
57     @Override
58     public String toString() {
59         StringBuilder newStatesStr = new StringBuilder();
60         for (int i = this.newStates.nextSetBit(0); i >= 0; i = this.newStates.nextSetBit(i + 1)) {
61             if (newStatesStr.length() > 0) {
62                 newStatesStr.append(",");
63             }
64             newStatesStr.append(String.format("%02X", i));
65         }
66         return String.format("NewStatesEvent: changed = [%s]", newStatesStr);
67     }
68 }