From 46c8256fe545d1a860ac118774e3906cddf0ec20 Mon Sep 17 00:00:00 2001 From: "Peter A. Jonsson" Date: Sun, 5 Nov 2023 12:48:33 +0100 Subject: [PATCH] Fix UnnecessaryLambda warnings https://errorprone.info/bugpattern/UnnecessaryLambda The fixed things happen once per simulation, so performance differences are negligible. This should also help with getting more readable backtraces in case of exceptions being thrown. --- build.gradle | 1 - .../contikios/cooja/plugins/PowerTracker.java | 10 ++++++---- .../skins/AttributeVisualizerSkin.java | 20 ++++++++++--------- .../plugins/skins/TrafficVisualizerSkin.java | 7 +++---- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/build.gradle b/build.gradle index e02602d83..c6dcc4dae 100644 --- a/build.gradle +++ b/build.gradle @@ -132,7 +132,6 @@ tasks.withType(JavaCompile).configureEach { options.errorprone.disable('StaticAssignmentInConstructor') options.errorprone.disable('StringSplitter') options.errorprone.disable('ThreadPriorityCheck') - options.errorprone.disable('UnnecessaryLambda') options.errorprone.disable('UnusedMethod') options.errorprone.disable('UnusedVariable') options.compilerArgs += ['-Werror', '--enable-preview', diff --git a/java/org/contikios/cooja/plugins/PowerTracker.java b/java/org/contikios/cooja/plugins/PowerTracker.java index f0156668b..8085f21f8 100644 --- a/java/org/contikios/cooja/plugins/PowerTracker.java +++ b/java/org/contikios/cooja/plugins/PowerTracker.java @@ -40,7 +40,6 @@ import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Collection; -import java.util.function.BiConsumer; import javax.swing.AbstractAction; import javax.swing.Box; import javax.swing.JButton; @@ -306,7 +305,6 @@ public static class MoteTracker { private Mote mote; private Radio radio; - private final BiConsumer trigger = (event, radio) -> update(); MoteTracker(Mote mote) { this.simulation = mote.getSimulation(); this.mote = mote; @@ -323,7 +321,11 @@ public static class MoteTracker { lastRadioState = RadioState.IDLE; } lastUpdateTime = simulation.getSimulationTime(); - radio.getRadioEventTriggers().addTrigger(this, trigger); + radio.getRadioEventTriggers().addTrigger(this, this::trigger); + } + + private void trigger(Radio.RadioEvent event, Radio radio) { + update(); } void update() { @@ -398,7 +400,7 @@ Mote getMote() { } void dispose() { - radio.getRadioEventTriggers().removeTrigger(this, trigger); + radio.getRadioEventTriggers().removeTrigger(this, this::trigger); radio = null; mote = null; } diff --git a/java/org/contikios/cooja/plugins/skins/AttributeVisualizerSkin.java b/java/org/contikios/cooja/plugins/skins/AttributeVisualizerSkin.java index 900baf8f7..94d752ffd 100644 --- a/java/org/contikios/cooja/plugins/skins/AttributeVisualizerSkin.java +++ b/java/org/contikios/cooja/plugins/skins/AttributeVisualizerSkin.java @@ -34,7 +34,6 @@ import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Point; -import java.util.function.BiConsumer; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; import org.contikios.cooja.Simulation; @@ -60,34 +59,37 @@ public class AttributeVisualizerSkin implements VisualizerSkin { private Simulation simulation; private Visualizer visualizer; - private final BiConsumer attributesTrigger = (obs, obj) -> visualizer.repaint(); - private final BiConsumer newMotesListener = (event, mote) -> { + private void newMotesListener(EventTriggers.AddRemove event, Mote mote) { var intf = mote.getInterfaces().getInterfaceOfType(MoteAttributes.class); if (intf != null) { if (event == EventTriggers.AddRemove.ADD) { - intf.getAttributesTriggers().addTrigger(this, attributesTrigger); + intf.getAttributesTriggers().addTrigger(this, this::attributesTrigger); } else { - intf.getAttributesTriggers().removeTrigger(this, attributesTrigger); + intf.getAttributesTriggers().removeTrigger(this, this::attributesTrigger); } } }; + private void attributesTrigger(EventTriggers.AddRemoveUpdate obs, MoteAttributes.MoteAttributeUpdateData obj) { + visualizer.repaint(); + } + @Override public void setActive(Simulation simulation, Visualizer vis) { this.simulation = simulation; this.visualizer = vis; - simulation.getMoteTriggers().addTrigger(this, newMotesListener); + simulation.getMoteTriggers().addTrigger(this, this::newMotesListener); for (Mote m: simulation.getMotes()) { - newMotesListener.accept(EventTriggers.AddRemove.ADD, m); + newMotesListener(EventTriggers.AddRemove.ADD, m); } } @Override public void setInactive() { - simulation.getMoteTriggers().removeTrigger(this, newMotesListener); + simulation.getMoteTriggers().removeTrigger(this, this::newMotesListener); for (Mote m: simulation.getMotes()) { - newMotesListener.accept(EventTriggers.AddRemove.REMOVE, m); + newMotesListener(EventTriggers.AddRemove.REMOVE, m); } } diff --git a/java/org/contikios/cooja/plugins/skins/TrafficVisualizerSkin.java b/java/org/contikios/cooja/plugins/skins/TrafficVisualizerSkin.java index 60557ffc2..215fe19df 100644 --- a/java/org/contikios/cooja/plugins/skins/TrafficVisualizerSkin.java +++ b/java/org/contikios/cooja/plugins/skins/TrafficVisualizerSkin.java @@ -35,7 +35,6 @@ import java.awt.Polygon; import java.util.ArrayList; import java.util.List; -import java.util.function.BiConsumer; import org.contikios.cooja.ClassDescription; import org.contikios.cooja.Mote; import org.contikios.cooja.RadioConnection; @@ -67,7 +66,7 @@ public class TrafficVisualizerSkin implements VisualizerSkin { private final List historyList = new ArrayList<>(); - private final BiConsumer radioMediumObserver = (event, obj) -> { + private void radioMediumObserver(Radio.RadioEvent event, Object obj) { RadioConnection last = radioMedium.getLastConnection(); if (last != null && historyList.size() < MAX_HISTORY_SIZE) { synchronized(historyList) { @@ -109,7 +108,7 @@ public void setActive(final Simulation simulation, Visualizer vis) { simulation.invokeSimulationThread(() -> { historyList.clear(); /* Start observing radio medium for transmissions */ - radioMedium.getRadioTransmissionTriggers().addTrigger(this, radioMediumObserver); + radioMedium.getRadioTransmissionTriggers().addTrigger(this, this::radioMediumObserver); /* Fade away arrows */ simulation.scheduleEvent(ageArrowsTimeEvent, simulation.getSimulationTime() + 100*Simulation.MILLISECOND); }); @@ -124,7 +123,7 @@ public void setInactive() { } /* Stop observing radio medium */ - radioMedium.getRadioTransmissionTriggers().removeTrigger(this, radioMediumObserver); + radioMedium.getRadioTransmissionTriggers().removeTrigger(this, this::radioMediumObserver); } @Override