Skip to content

Commit

Permalink
Pattern matching code updates for Java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
retrodaredevil committed Dec 18, 2024
1 parent 7d04d38 commit 415c6d3
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ protected void onUpdate() {
for (VersionedPacket<StoredAlterPacket> versionedPacket : alterPackets) {

AlterPacket packet = versionedPacket.getPacket().getPacket();
if (packet instanceof ScheduledCommandPacket) {
ScheduledCommandPacket scheduledCommandPacket = (ScheduledCommandPacket) packet;

if (packet instanceof ScheduledCommandPacket scheduledCommandPacket) {
ScheduledCommandData data = scheduledCommandPacket.getData();
if (data.getScheduledTimeMillis() <= now.toEpochMilli()) {
if (now.toEpochMilli() - data.getScheduledTimeMillis() > Duration.ofMinutes(5).toMillis()) {
Expand All @@ -155,12 +153,10 @@ protected void onUpdate() {
doSendCommand(versionedPacket, scheduledCommandPacket);
}
}
} else if (packet instanceof FlagPacket) {
FlagPacket flagPacket = (FlagPacket) packet;
} else if (packet instanceof FlagPacket flagPacket) {
FlagData data = flagPacket.getFlagData();
ActivePeriod activePeriod = data.getActivePeriod();
if (activePeriod instanceof TimeRangeActivePeriod) { // We only try to "manage" flags that use this type of ActivePeriod
TimeRangeActivePeriod period = (TimeRangeActivePeriod) activePeriod;
if (activePeriod instanceof TimeRangeActivePeriod period) { // We only try to "manage" flags that use this type of ActivePeriod
TimeRange timeRange = period.getTimeRange();
Instant endTime = timeRange.getEndTime();
if (endTime != null && endTime.compareTo(now) < 0) {
Expand Down Expand Up @@ -192,8 +188,7 @@ private void receivePacketWithIntegrity(String sender, TargetPacketGroup packetG
List<StoredAlterPacket> storedAlterPacketsToUpload = new ArrayList<>();
List<DeleteAlterPacket> deleteAlterPackets = new ArrayList<>();
for (Packet packet : packetGroup.getPackets()) {
if (packet instanceof ScheduleCommandPacket) {
ScheduleCommandPacket scheduleCommandPacket = (ScheduleCommandPacket) packet;
if (packet instanceof ScheduleCommandPacket scheduleCommandPacket) {
ScheduledCommandData data = scheduleCommandPacket.getData();
ExecutionReason executionReason = new OpenSourceExecutionReason(new OpenSource(
sender,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public Action createAction(ActionEnvironment actionEnvironment) {
int fragmentId = packetGroup.getFragmentId(packet);
String sourceId = packetGroup.getSourceId(packet);
String nameBase = "solarthing_" + sourceId + "_" + fragmentId + "_";
if (packet instanceof FXStatusPacket) {
FXStatusPacket fx = (FXStatusPacket) packet;
if (packet instanceof FXStatusPacket fx) {
nameBase += "fx_" + fx.getAddress() + "_";
calls.add(service.updateSensor(
nameBase + "acmode",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void listFlags(MessageSender messageSender) {
.map(versionedPacket -> versionedPacket.getPacket().getPacket())
.filter(packet -> packet instanceof FlagPacket)
.map(packet -> ((FlagPacket) packet).getFlagData())
.collect(Collectors.toList());
.toList();
if (flagDataList.isEmpty()) {
messageSender.sendMessage("There are no flags set.");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ private void displayHeartbeats(MessageSender messageSender) {
.forEach(instancePacketGroup -> {
long dateMillis = instancePacketGroup.getDateMillis();
for (Packet packet : instancePacketGroup.getPackets()) {
if (packet instanceof HeartbeatPacket) {
HeartbeatPacket heartbeatPacket = (HeartbeatPacket) packet;
if (packet instanceof HeartbeatPacket heartbeatPacket) {
HeartbeatIdentifier identifier = new HeartbeatIdentifier(heartbeatPacket.getData().getIdentifier(), instancePacketGroup.getFragmentId());
HeartbeatNode node = new HeartbeatNode(dateMillis, heartbeatPacket);
map.put(identifier, node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ public ExecutionFeedbackEvent() {
@Override
public void runForEvent(MessageSender sender, InstancePacketGroup packetGroup) {
for (Packet packet : packetGroup.getPackets()) {
if (packet instanceof ExecutionFeedbackPacket) {
ExecutionFeedbackPacket executionFeedbackPacket = (ExecutionFeedbackPacket) packet;
if (packet instanceof ExecutionFeedbackPacket executionFeedbackPacket) {
sender.sendMessage(executionFeedbackPacket.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public GeneratorUnusedAlertEvent(
// We use a for loop here instead of just the "master" FX packet because it's possible for the voltage
// on one FX to be greater than another, so we want to check to see if this applied to *any* FXs
for (Packet packet : current.getPackets()) {
if (packet instanceof FXStatusPacket) {
FXStatusPacket fx = (FXStatusPacket) packet;
if (packet instanceof FXStatusPacket fx) {
boolean is230 = fx.is230V();
int lowThreshold = is230 && lowRaw ? 2 * lowThresholdVoltage : lowThresholdVoltage;
int inputVoltage = fx.getInputVoltage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public MXFloatModeStuckEvent(
@Override
public void run(MessageSender sender, FragmentedPacketGroup previous, FragmentedPacketGroup current) {
for (Packet previousPacket : previous.getPackets()) {
if (previousPacket instanceof MXStatusPacket) {
MXStatusPacket previousMX = (MXStatusPacket) previousPacket;
if (previousPacket instanceof MXStatusPacket previousMX) {
KnownIdentifierFragment<OutbackIdentifier> identifierFragment = IdentifierFragment.create(previous.getFragmentId(previousPacket), previousMX.getIdentifier());
MXStatusPacket currentMX = null;
for (Packet currentPacket : current.getPackets()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,8 @@ public SolarThingStatusQuery(PacketGetter packetGetter, List<? extends Fragmente
}
int temperatureCelsius = Math.round(batteryTemperatureCelsius.floatValue()) + (fxChargingTemperatureAdjustPacket == null ? 0 : fxChargingTemperatureAdjustPacket.getTemperatureAdjustCelsius());
for (Packet packet : packetGroup.getPackets()) {
if (packet instanceof BatteryVoltage) {
if (packet instanceof BatteryVoltage batteryVoltagePacket) {
int fragmentId = packetGroup.getFragmentId(packet);
BatteryVoltage batteryVoltagePacket = (BatteryVoltage) packet;
float batteryVoltage = batteryVoltagePacket.getBatteryVoltage();
float compensated = BatteryUtil.getOutbackCompensatedBatteryVoltage(batteryVoltage, temperatureCelsius);
long dateMillis = packetGroup.getDateMillisOrKnown(packet);
Expand Down

0 comments on commit 415c6d3

Please sign in to comment.