Skip to content

Commit 39b094c

Browse files
committed
added equals and hashCode to Messages
1 parent 4d5dbd1 commit 39b094c

File tree

75 files changed

+1553
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1553
-200
lines changed

de.ibapl.fhz4j.parser.cul/src/main/java/de/ibapl/fhz4j/api/Message.java

+35-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,45 @@
2121
*/
2222
package de.ibapl.fhz4j.api;
2323

24+
import java.util.Objects;
25+
2426
/**
2527
*
2628
* @author Arne Plöse
29+
* @param <T>
2730
*
2831
*/
29-
public abstract class Message {
32+
public abstract class Message<T extends Message<T>> {
33+
34+
protected final static int HASH_MULTIPLIER = 53;
35+
protected final static int INITIAL_HASH = 7;
36+
37+
@Override
38+
final public int hashCode() {
39+
return subClassHashCode(INITIAL_HASH);
40+
}
41+
42+
protected int subClassHashCode(int hash) {
43+
return HASH_MULTIPLIER * hash + Objects.hashCode(this.protocol);
44+
}
45+
46+
@Override
47+
final public boolean equals(Object obj) {
48+
if (this == obj) {
49+
return true;
50+
}
51+
if (obj == null) {
52+
return false;
53+
}
54+
if (getClass() != obj.getClass()) {
55+
return false;
56+
}
57+
return subClassEquals((T) obj);
58+
}
59+
60+
protected boolean subClassEquals(T other) {
61+
return this.protocol == other.protocol;
62+
}
3063

3164
public Protocol protocol;
3265

@@ -40,7 +73,7 @@ protected void addToJsonString(StringBuilder sb) {
4073
}
4174

4275
@Override
43-
public String toString() {
76+
final public String toString() {
4477
StringBuilder sb = new StringBuilder();
4578
sb.append("{");
4679
addToJsonString(sb);

de.ibapl.fhz4j.parser.cul/src/main/java/de/ibapl/fhz4j/cul/CulEobMessage.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
*/
2222
package de.ibapl.fhz4j.cul;
2323

24+
import java.util.Objects;
25+
2426
/**
2527
*
2628
* @author aploese
2729
*/
28-
public class CulEobMessage extends CulMessage {
30+
public final class CulEobMessage extends CulMessage<CulEobMessage> {
2931

3032
public final static CulMessage EOB = new CulEobMessage();
3133

@@ -42,4 +44,18 @@ protected void addToJsonString(StringBuilder sb) {
4244
sb.append('"');
4345
}
4446

47+
@Override
48+
protected int subClassHashCode(int hash) {
49+
hash = super.subClassHashCode(hash);
50+
return HASH_MULTIPLIER * hash + Objects.hashCode(this.message);
51+
}
52+
53+
@Override
54+
protected boolean subClassEquals(CulEobMessage other) {
55+
if (!super.subClassEquals(other)) {
56+
return false;
57+
}
58+
return Objects.equals(this.message, other.message);
59+
}
60+
4561
}

de.ibapl.fhz4j.parser.cul/src/main/java/de/ibapl/fhz4j/cul/CulLovfMessage.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
*/
2222
package de.ibapl.fhz4j.cul;
2323

24+
import java.util.Objects;
25+
2426
/**
2527
*
2628
* @author aploese
29+
* @param <T>
2730
*/
28-
public class CulLovfMessage extends CulMessage {
31+
public class CulLovfMessage<T extends CulLovfMessage<T>> extends CulMessage<T> {
2932

3033
public final static CulMessage LOVF = new CulLovfMessage();
3134

@@ -39,4 +42,18 @@ protected void addToJsonString(StringBuilder sb) {
3942
sb.append('"');
4043
}
4144

45+
@Override
46+
protected int subClassHashCode(int hash) {
47+
hash = super.subClassHashCode(hash);
48+
return HASH_MULTIPLIER * hash + Objects.hashCode(this.message);
49+
}
50+
51+
@Override
52+
protected boolean subClassEquals(T other) {
53+
if (!super.subClassEquals(other)) {
54+
return false;
55+
}
56+
return Objects.equals(this.message, other.message);
57+
}
58+
4259
}

de.ibapl.fhz4j.parser.cul/src/main/java/de/ibapl/fhz4j/cul/CulMessage.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
/**
2828
*
2929
* @author Arne Plöse
30+
* @param <T>
3031
*/
31-
public abstract class CulMessage extends Message {
32+
public abstract class CulMessage<T extends CulMessage<T>> extends Message<T> {
3233

3334
protected CulMessage() {
3435
super(Protocol.CUL);

de.ibapl.fhz4j.parser.cul/src/main/java/de/ibapl/fhz4j/parser/evohome/EvoHomeParser.java

+23-23
Original file line numberDiff line numberDiff line change
@@ -403,15 +403,15 @@ public void parse(byte b) {
403403
success();
404404
}
405405
case PARSE__RELAY_HEAT_DEMAND__DOMAIN_ID -> {
406-
((RelayHeatDemandInformationMessage) evoHomeMessage).domain_id = b;
406+
((RelayHeatDemandInformationMessage) evoHomeMessage).domainId = b;
407407
state = State.PARSE__RELAY_HEAT_DEMAND__DEMAND;
408408
}
409409
case PARSE__RELAY_HEAT_DEMAND__DEMAND -> {
410410
((RelayHeatDemandInformationMessage) evoHomeMessage).demand = (float) ((b & 0x00FF) / 2.0);
411411
success();
412412
}
413413
case PARSE__RELAY_FAILSAVE__DOMAIN_ID -> {
414-
((RelayFailsaveInformationMessage) evoHomeMessage).domain_id = b;
414+
((RelayFailsaveInformationMessage) evoHomeMessage).domainId = b;
415415
setStackSize(2);
416416
state = State.COLLECT__RELAY_FAILSAVE__VALUE;
417417
}
@@ -422,7 +422,7 @@ public void parse(byte b) {
422422
}
423423
}
424424
case PARSE__SYSTEM_SYNCHRONIZATION__PAYLOAD__DEVICE_ID -> {
425-
((SystemSynchronizationPayloadMessage) evoHomeMessage).device_id = b;
425+
((SystemSynchronizationPayloadMessage) evoHomeMessage).deviceId = b;
426426
setStackSize(2);
427427
state = State.PARSE__SYSTEM_SYNCHRONIZATION__PAYLOAD__COUNTDOWN;
428428
}
@@ -441,7 +441,7 @@ public void parse(byte b) {
441441
case ZONE_TEMPERATURE__PAYLOAD__TEMPERATURE -> {
442442
remainingDataLength--;
443443
if (push(b)) {
444-
((AbstractZoneTemperaturePayloadMessage) evoHomeMessage).zoneTemperatures.getLast().temperature = getTemperature();
444+
((AbstractZoneTemperaturePayloadMessage<?>) evoHomeMessage).zoneTemperatures.getLast().temperature = getTemperature();
445445
if (remainingDataLength == 0) {
446446
success();
447447
} else {
@@ -451,23 +451,23 @@ public void parse(byte b) {
451451
}
452452
case RF_BIND__PAYLOAD__ELEMENTS__ZONE_ID -> {
453453
remainingDataLength--;
454-
((AbstractRfBindPayloadMessage) evoHomeMessage).elements.add(new AbstractRfBindPayloadMessage.Data());
455-
((AbstractRfBindPayloadMessage) evoHomeMessage).elements.getLast().zoneId = b;
454+
((AbstractRfBindPayloadMessage<?>) evoHomeMessage).elements.add(new AbstractRfBindPayloadMessage.Data());
455+
((AbstractRfBindPayloadMessage<?>) evoHomeMessage).elements.getLast().zoneId = b;
456456
setStackSize(2);
457457
state = State.RF_BIND__PAYLOAD__ELEMENTS__COMMAND;
458458
}
459459
case RF_BIND__PAYLOAD__ELEMENTS__COMMAND -> {
460460
remainingDataLength--;
461461
if (push(b)) {
462-
((AbstractRfBindPayloadMessage) evoHomeMessage).elements.getLast().command = getShortValue();
462+
((AbstractRfBindPayloadMessage<?>) evoHomeMessage).elements.getLast().command = getShortValue();
463463
setStackSize(3);
464464
state = State.RF_BIND__PAYLOAD__ELEMENTS__DEVICE_ID;
465465
}
466466
}
467467
case RF_BIND__PAYLOAD__ELEMENTS__DEVICE_ID -> {
468468
remainingDataLength--;
469469
if (push(b)) {
470-
((AbstractRfBindPayloadMessage) evoHomeMessage).elements.getLast().deviceId = getIntValue();
470+
((AbstractRfBindPayloadMessage<?>) evoHomeMessage).elements.getLast().deviceId = getIntValue();
471471
if (remainingDataLength == 0) {
472472
success();
473473
} else {
@@ -496,7 +496,7 @@ public void parse(byte b) {
496496
}
497497
}
498498
case ZONE_HEAT_DEMAND__INFORMATION__ZONE_ID -> {
499-
((ZoneHeatDemandInformationMessage) evoHomeMessage).zone_id = b;
499+
((ZoneHeatDemandInformationMessage) evoHomeMessage).zoneId = b;
500500
state = State.ZONE_HEAT_DEMAND__INFORMATION__HEAT_DEMAND;
501501
}
502502
case ZONE_HEAT_DEMAND__INFORMATION__HEAT_DEMAND -> {
@@ -597,20 +597,20 @@ public void parse(byte b) {
597597
}
598598
case ZONE_ACTUATORS__INFORMATION__ZONE_IDX -> {
599599
remainingDataLength--;
600-
((ZoneActuatorsInformationMessage) evoHomeMessage).actuators.add(new ZoneActuatorsInformationMessage.ZoneActuator());
601-
((ZoneActuatorsInformationMessage) evoHomeMessage).actuators.getLast().zone_idx = b;
600+
((ZoneActuatorsInformationMessage<?>) evoHomeMessage).actuators.add(new ZoneActuatorsInformationMessage.ZoneActuator());
601+
((ZoneActuatorsInformationMessage<?>) evoHomeMessage).actuators.getLast().zoneIdx = b;
602602
state = State.ZONE_ACTUATORS__INFORMATION__UNKNOWN0;
603603
}
604604
case ZONE_ACTUATORS__INFORMATION__UNKNOWN0 -> {
605605
remainingDataLength--;
606-
((ZoneActuatorsInformationMessage) evoHomeMessage).actuators.getLast().unknown0 = b;
606+
((ZoneActuatorsInformationMessage<?>) evoHomeMessage).actuators.getLast().unknown0 = b;
607607
setStackSize(4);
608608
state = State.ZONE_ACTUATORS__INFORMATION__DEVICEID;
609609
}
610610
case ZONE_ACTUATORS__INFORMATION__DEVICEID -> {
611611
remainingDataLength--;
612612
if (push(b)) {
613-
((ZoneActuatorsInformationMessage) evoHomeMessage).actuators.getLast().deviceId = new DeviceId(getShortValue());
613+
((ZoneActuatorsInformationMessage<?>) evoHomeMessage).actuators.getLast().deviceId = new DeviceId(getShortValue());
614614
if (remainingDataLength == 0) {
615615
success();
616616
} else {
@@ -620,7 +620,7 @@ public void parse(byte b) {
620620
}
621621
case ZONE_ACTUATORS__REQUEST__ZONE_IDX -> {
622622
remainingDataLength--;
623-
((ZoneActuatorsRequestMessage) evoHomeMessage).zone_idx = b;
623+
((ZoneActuatorsRequestMessage) evoHomeMessage).zoneIdx = b;
624624
state = State.ZONE_ACTUATORS__REQUEST__UNKNOWN0;
625625
}
626626
case ZONE_ACTUATORS__REQUEST__UNKNOWN0 -> {
@@ -733,12 +733,12 @@ public void parse(byte b) {
733733
}
734734
case WINDOW_SENSOR__REQUEST__ZONE_ID -> {
735735
remainingDataLength--;
736-
((WindowSensorRequestMessage) evoHomeMessage).zone_id = b;
736+
((WindowSensorRequestMessage) evoHomeMessage).zoneId = b;
737737
success();
738738
}
739739
case WINDOW_SENSOR__PAYLOAD__ZONE_ID -> {
740740
remainingDataLength--;
741-
((AbstractWindowSensorPayloadMessage) evoHomeMessage).zone_id = b;
741+
((AbstractWindowSensorPayloadMessage) evoHomeMessage).zoneId = b;
742742
setStackSize(2);
743743
state = State.WINDOW_SENSOR__PAYLOAD__UNKNOWN0;
744744
}
@@ -751,7 +751,7 @@ public void parse(byte b) {
751751
}
752752
case ZONE_SETPOINT__REQUEST__ZONE_ID -> {
753753
remainingDataLength--;
754-
((ZoneSetpointRequestMessage) evoHomeMessage).zone_id = b;
754+
((ZoneSetpointRequestMessage) evoHomeMessage).zoneId = b;
755755
success();
756756
}
757757
case ZONE_SETPOINT__PAYLOAD__ZONE_TEMPERATURES__ZONE_ID -> {
@@ -764,9 +764,9 @@ public void parse(byte b) {
764764
remainingDataLength--;
765765
if (push(b)) {
766766
if (getShortValue() == 0x7FFF) {
767-
((AbstractZoneSetpointPayloadMessage) evoHomeMessage).zoneTemperatures.getLast().temperature = null;
767+
((AbstractZoneSetpointPayloadMessage<?>) evoHomeMessage).zoneTemperatures.getLast().temperature = null;
768768
} else {
769-
((AbstractZoneSetpointPayloadMessage) evoHomeMessage).zoneTemperatures.getLast().temperature = new BigDecimal(getShortValue()).divide(ONE_HUNDRED);
769+
((AbstractZoneSetpointPayloadMessage<?>) evoHomeMessage).zoneTemperatures.getLast().temperature = new BigDecimal(getShortValue()).divide(ONE_HUNDRED);
770770
}
771771
if (remainingDataLength == 0) {
772772
success();
@@ -815,12 +815,12 @@ public void parse(byte b) {
815815
}
816816
case SYSTEM_TIMESTAMP__REQUEST__ZONE_ID -> {
817817
remainingDataLength--;
818-
((SystemTimestampRequestMessage) evoHomeMessage).zone_id = b;
818+
((SystemTimestampRequestMessage) evoHomeMessage).zoneId = b;
819819
success();
820820
}
821821
case SYSTEM_TIMESTAMP__RESPONSE__ZONE_ID -> {
822822
remainingDataLength--;
823-
((SystemTimestampResponseMessage) evoHomeMessage).zone_id = b;
823+
((SystemTimestampResponseMessage) evoHomeMessage).zoneId = b;
824824
state = State.SYSTEM_TIMESTAMP__RESPONSE__DIRECTION;
825825
}
826826
case SYSTEM_TIMESTAMP__RESPONSE__DIRECTION -> {
@@ -846,7 +846,7 @@ public void parse(byte b) {
846846
}
847847
case ACTUATOR_SYNC__INFORMATION__DOMAIN_ID -> {
848848
remainingDataLength--;
849-
((ActuatorSyncInformationMessage) evoHomeMessage).domain_id = b;
849+
((ActuatorSyncInformationMessage) evoHomeMessage).domainId = b;
850850
state = State.ACTUATOR_SYNC__INFORMATION__STATE;
851851
}
852852
case ACTUATOR_SYNC__INFORMATION__STATE -> {
@@ -856,7 +856,7 @@ public void parse(byte b) {
856856
}
857857
case SYSTEM_SYNCHRONIZATION__REQUEST__DOMAIN_ID -> {
858858
remainingDataLength--;
859-
((SystemSynchronizationRequestMessage) evoHomeMessage).domain_id = b;
859+
((SystemSynchronizationRequestMessage) evoHomeMessage).domainId = b;
860860
success();
861861
}
862862
default -> // TODO

de.ibapl.fhz4j.parser.cul/src/main/java/de/ibapl/fhz4j/protocol/em/EmMessage.java

+39-4
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
* Copyright (C) 2009, 2017, Arne Plöse and individual contributors as indicated
3131
* by the @authors tag. See the copyright.txt in the distribution for a
3232
* full listing of individual contributors.
33-
*
33+
*
3434
* This is free software; you can redistribute it and/or modify it
3535
* under the terms of the GNU General Public License as
3636
* published by the Free Software Foundation; either version 3 of
3737
* the License, or (at your option) any later version.
38-
*
38+
*
3939
* This software is distributed in the hope that it will be useful,
4040
* but WITHOUT ANY WARRANTY; without even the implied warranty of
4141
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4242
* Lesser General Public License for more details.
43-
*
43+
*
4444
* You should have received a copy of the GNU Lesser General Public
4545
* License along with this software; if not, write to the Free
4646
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
@@ -49,12 +49,13 @@
4949
*/
5050
import de.ibapl.fhz4j.api.Message;
5151
import de.ibapl.fhz4j.api.Protocol;
52+
import java.util.Objects;
5253

5354
/**
5455
*
5556
* @author Arne Plöse
5657
*/
57-
public class EmMessage extends Message {
58+
public final class EmMessage extends Message<EmMessage> {
5859

5960
public final static double EM_1000_S_CORR_1 = 12.0 / 150.0;
6061
public final static double EM_1000_S_CORR_2 = 12.0 / 1800.0;
@@ -91,4 +92,38 @@ protected void addToJsonString(StringBuilder sb) {
9192
sb.append(", emDeviceType : ").append(emDeviceType);
9293
}
9394

95+
@Override
96+
protected int subClassHashCode(int hash) {
97+
hash = super.subClassHashCode(hash);
98+
hash = HASH_MULTIPLIER * hash + Objects.hashCode(this.address);
99+
hash = HASH_MULTIPLIER * hash + Objects.hashCode(this.counter);
100+
hash = HASH_MULTIPLIER * hash + Objects.hashCode(this.emDeviceType);
101+
hash = HASH_MULTIPLIER * hash + Objects.hashCode(this.valueCummulated);
102+
hash = HASH_MULTIPLIER * hash + Objects.hashCode(this.value5Min);
103+
return HASH_MULTIPLIER * hash + Objects.hashCode(this.value5MinPeak);
104+
}
105+
106+
@Override
107+
protected boolean subClassEquals(EmMessage other) {
108+
if (!super.subClassEquals(other)) {
109+
return false;
110+
}
111+
if (this.address != other.address) {
112+
return false;
113+
}
114+
if (this.counter != other.counter) {
115+
return false;
116+
}
117+
if (this.emDeviceType != other.emDeviceType) {
118+
return false;
119+
}
120+
if (this.valueCummulated != other.valueCummulated) {
121+
return false;
122+
}
123+
if (this.value5Min != other.value5Min) {
124+
return false;
125+
}
126+
return this.value5MinPeak == other.value5MinPeak;
127+
}
128+
94129
}

de.ibapl.fhz4j.parser.cul/src/main/java/de/ibapl/fhz4j/protocol/evohome/DeviceId.java

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public boolean equals(Object obj) {
4646
return false;
4747
}
4848
final DeviceId other = (DeviceId) obj;
49+
//type is generated in constructor so no need to check.
4950
return this.id == other.id;
5051
}
5152

0 commit comments

Comments
 (0)