Skip to content

Commit ce3dc37

Browse files
committed
Replace anonymous Runnables by by lambda expressions / method references
1 parent 7132f06 commit ce3dc37

File tree

20 files changed

+493
-662
lines changed

20 files changed

+493
-662
lines changed

com.zsmartsystems.zigbee.console.main/src/main/java/com/zsmartsystems/zigbee/console/main/ZigBeeConsole.java

Lines changed: 90 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -284,21 +284,18 @@ public void commandReceived(ZigBeeCommand command) {
284284
}
285285
});
286286

287-
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
288-
@Override
289-
public void run() {
290-
shutdown = true;
291-
try {
292-
System.in.close();
293-
} catch (IOException e) {
294-
e.printStackTrace();
295-
}
296-
try {
297-
mainThread.interrupt();
298-
mainThread.join();
299-
} catch (InterruptedException e) {
300-
e.printStackTrace();
301-
}
287+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
288+
shutdown = true;
289+
try {
290+
System.in.close();
291+
} catch (IOException e) {
292+
e.printStackTrace();
293+
}
294+
try {
295+
mainThread.interrupt();
296+
mainThread.join();
297+
} catch (InterruptedException e) {
298+
e.printStackTrace();
302299
}
303300
}));
304301
}
@@ -945,74 +942,62 @@ public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final Pri
945942
return false;
946943
}
947944

948-
new Thread(new Runnable() {
949-
@Override
950-
public void run() {
951-
int cnt = 0;
952-
while (true) {
953-
print("STRESSING 1 CNT: " + cnt++, out);
954-
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
955-
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
956-
cluster.onCommand();
957-
try {
958-
Thread.sleep(167);
959-
} catch (InterruptedException e) {
960-
e.printStackTrace();
961-
}
945+
new Thread(() -> {
946+
int cnt = 0;
947+
while (true) {
948+
print("STRESSING 1 CNT: " + cnt++, out);
949+
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
950+
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
951+
cluster.onCommand();
952+
try {
953+
Thread.sleep(167);
954+
} catch (InterruptedException e) {
955+
e.printStackTrace();
962956
}
963957
}
964958
}).start();
965959

966-
new Thread(new Runnable() {
967-
@Override
968-
public void run() {
969-
int cnt = 0;
970-
while (true) {
971-
print("STRESSING 2 CNT: " + cnt++, out);
972-
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
973-
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
974-
cluster.onCommand();
975-
try {
976-
Thread.sleep(107);
977-
} catch (InterruptedException e) {
978-
e.printStackTrace();
979-
}
960+
new Thread(() -> {
961+
int cnt = 0;
962+
while (true) {
963+
print("STRESSING 2 CNT: " + cnt++, out);
964+
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
965+
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
966+
cluster.onCommand();
967+
try {
968+
Thread.sleep(107);
969+
} catch (InterruptedException e) {
970+
e.printStackTrace();
980971
}
981972
}
982973
}).start();
983974

984-
new Thread(new Runnable() {
985-
@Override
986-
public void run() {
987-
int cnt = 0;
988-
while (true) {
989-
print("STRESSING 3 CNT: " + cnt++, out);
990-
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
991-
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
992-
cluster.onCommand();
993-
try {
994-
Thread.sleep(131);
995-
} catch (InterruptedException e) {
996-
e.printStackTrace();
997-
}
975+
new Thread(() -> {
976+
int cnt = 0;
977+
while (true) {
978+
print("STRESSING 3 CNT: " + cnt++, out);
979+
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
980+
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
981+
cluster.onCommand();
982+
try {
983+
Thread.sleep(131);
984+
} catch (InterruptedException e) {
985+
e.printStackTrace();
998986
}
999987
}
1000988
}).start();
1001989

1002-
new Thread(new Runnable() {
1003-
@Override
1004-
public void run() {
1005-
int cnt = 0;
1006-
while (true) {
1007-
print("STRESSING 4 CNT: " + cnt++, out);
1008-
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
1009-
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
1010-
cluster.onCommand();
1011-
try {
1012-
Thread.sleep(187);
1013-
} catch (InterruptedException e) {
1014-
e.printStackTrace();
1015-
}
990+
new Thread(() -> {
991+
int cnt = 0;
992+
while (true) {
993+
print("STRESSING 4 CNT: " + cnt++, out);
994+
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
995+
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
996+
cluster.onCommand();
997+
try {
998+
Thread.sleep(187);
999+
} catch (InterruptedException e) {
1000+
e.printStackTrace();
10161001
}
10171002
}
10181003
}).start();
@@ -1089,46 +1074,42 @@ public String getSyntax() {
10891074
*/
10901075
@Override
10911076
public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final PrintStream out) throws Exception {
1092-
new Thread(new Runnable() {
1093-
@Override
1094-
public void run() {
1095-
int cnts = 1000;
1096-
int errors = 0;
1097-
int cnt = 0;
1098-
List<Integer> lqi = new ArrayList<>();
1099-
while (cnt < cnts) {
1100-
print("LQI Poll CNT: " + cnt++, out);
1101-
final ManagementLqiRequest neighborRequest = new ManagementLqiRequest(0);
1102-
neighborRequest.setDestinationAddress(new ZigBeeEndpointAddress(0));
1103-
1104-
CommandResult response;
1105-
try {
1106-
response = networkManager.sendTransaction(neighborRequest, neighborRequest).get();
1107-
final ManagementLqiResponse neighborResponse = response.getResponse();
1108-
1109-
if (neighborResponse == null || neighborResponse.getStatus() != ZdoStatus.SUCCESS) {
1110-
errors++;
1111-
continue;
1112-
}
1113-
if (neighborResponse.getNeighborTableList().isEmpty()) {
1114-
print("No neighbors", out);
1115-
continue;
1116-
}
1117-
lqi.add(neighborResponse.getNeighborTableList().get(0).getLqi());
1118-
} catch (InterruptedException | ExecutionException e) {
1119-
// TODO Auto-generated catch block
1120-
e.printStackTrace();
1077+
new Thread(() -> {
1078+
int cnts = 1000;
1079+
int errors = 0;
1080+
int cnt = 0;
1081+
List<Integer> lqi = new ArrayList<>();
1082+
while (cnt < cnts) {
1083+
print("LQI Poll CNT: " + cnt++, out);
1084+
final ManagementLqiRequest neighborRequest = new ManagementLqiRequest(0);
1085+
neighborRequest.setDestinationAddress(new ZigBeeEndpointAddress(0));
1086+
1087+
CommandResult response;
1088+
try {
1089+
response = networkManager.sendTransaction(neighborRequest, neighborRequest).get();
1090+
final ManagementLqiResponse neighborResponse = response.getResponse();
1091+
1092+
if (neighborResponse == null || neighborResponse.getStatus() != ZdoStatus.SUCCESS) {
1093+
errors++;
1094+
continue;
11211095
}
1096+
if (neighborResponse.getNeighborTableList().isEmpty()) {
1097+
print("No neighbors", out);
1098+
continue;
1099+
}
1100+
lqi.add(neighborResponse.getNeighborTableList().get(0).getLqi());
1101+
} catch (InterruptedException | ExecutionException e) {
1102+
// TODO Auto-generated catch block
1103+
e.printStackTrace();
11221104
}
1123-
IntSummaryStatistics stats = lqi.stream().mapToInt((x) -> x).summaryStatistics();
1124-
1125-
print("LQI Polling Complete", out);
1126-
print("Errors: " + errors, out);
1127-
print("Min : " + stats.getMin(), out);
1128-
print("Max : " + stats.getMax(), out);
1129-
print("Avg : " + stats.getAverage(), out);
1130-
11311105
}
1106+
IntSummaryStatistics stats = lqi.stream().mapToInt((x) -> x).summaryStatistics();
1107+
1108+
print("LQI Polling Complete", out);
1109+
print("Errors: " + errors, out);
1110+
print("Min : " + stats.getMin(), out);
1111+
print("Max : " + stats.getMax(), out);
1112+
print("Avg : " + stats.getAverage(), out);
11321113
}).start();
11331114

11341115
return true;
@@ -1157,12 +1138,7 @@ public String getSyntax() {
11571138
*/
11581139
@Override
11591140
public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final PrintStream out) throws Exception {
1160-
new Thread(new Runnable() {
1161-
@Override
1162-
public void run() {
1163-
networkManager.reinitialize();
1164-
}
1165-
}).start();
1141+
new Thread(networkManager::reinitialize).start();
11661142

11671143
return true;
11681144
}

com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/ZigBeeDongleEzsp.java

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -630,17 +630,14 @@ private void scheduleNetworkStatePolling() {
630630
return;
631631
}
632632

633-
pollingTimer = executorService.scheduleWithFixedDelay(new Runnable() {
634-
@Override
635-
public void run() {
636-
// Don't poll the state if the network is down
637-
// or we've sent a command to the dongle within the pollRate
638-
if (!networkStateUp || (lastSendCommand + pollRate > System.currentTimeMillis())) {
639-
return;
640-
}
641-
// Don't wait for the response. This is running in a single thread scheduler
642-
frameHandler.queueFrame(new EzspNetworkStateRequest());
633+
pollingTimer = executorService.scheduleWithFixedDelay(() -> {
634+
// Don't poll the state if the network is down
635+
// or we've sent a command to the dongle within the pollRate
636+
if (!networkStateUp || (lastSendCommand + pollRate > System.currentTimeMillis())) {
637+
return;
643638
}
639+
// Don't wait for the response. This is running in a single thread scheduler
640+
frameHandler.queueFrame(new EzspNetworkStateRequest());
644641
}, pollRate, pollRate, TimeUnit.MILLISECONDS);
645642
}
646643

@@ -808,40 +805,37 @@ public void sendCommand(final int msgTag, final ZigBeeApsFrame apsFrame) {
808805

809806
// The response from the SendXxxcast messages returns the network layer sequence number
810807
// We need to correlate this with the messageTag
811-
executorService.execute(new Runnable() {
812-
@Override
813-
public void run() {
814-
synchronized (isConfiguredSync) {
815-
if (!isConfigured) {
816-
logger.debug("EZSP Dongle is not configured. Frame not sent: {}", apsFrame);
817-
return;
818-
}
808+
executorService.execute(() -> {
809+
synchronized (isConfiguredSync) {
810+
if (!isConfigured) {
811+
logger.debug("EZSP Dongle is not configured. Frame not sent: {}", apsFrame);
812+
return;
813+
}
819814

820-
lastSendCommand = System.currentTimeMillis();
821-
frameHandler.sendEzspTransaction(transaction);
822-
823-
EmberStatus status = null;
824-
if (transaction.getResponse() instanceof EzspSendUnicastResponse) {
825-
fragmentationApsCounters.put(msgTag,
826-
((EzspSendUnicastResponse) transaction.getResponse()).getSequence());
827-
status = ((EzspSendUnicastResponse) transaction.getResponse()).getStatus();
828-
} else if (transaction.getResponse() instanceof EzspSendBroadcastResponse) {
829-
status = ((EzspSendBroadcastResponse) transaction.getResponse()).getStatus();
830-
} else if (transaction.getResponse() instanceof EzspSendMulticastResponse) {
831-
status = ((EzspSendMulticastResponse) transaction.getResponse()).getStatus();
832-
} else {
833-
logger.debug("Unable to get response from {} :: {}", transaction.getRequest(),
834-
transaction.getResponse());
835-
return;
836-
}
815+
lastSendCommand = System.currentTimeMillis();
816+
frameHandler.sendEzspTransaction(transaction);
817+
818+
EmberStatus status = null;
819+
if (transaction.getResponse() instanceof EzspSendUnicastResponse) {
820+
fragmentationApsCounters.put(msgTag,
821+
((EzspSendUnicastResponse) transaction.getResponse()).getSequence());
822+
status = ((EzspSendUnicastResponse) transaction.getResponse()).getStatus();
823+
} else if (transaction.getResponse() instanceof EzspSendBroadcastResponse) {
824+
status = ((EzspSendBroadcastResponse) transaction.getResponse()).getStatus();
825+
} else if (transaction.getResponse() instanceof EzspSendMulticastResponse) {
826+
status = ((EzspSendMulticastResponse) transaction.getResponse()).getStatus();
827+
} else {
828+
logger.debug("Unable to get response from {} :: {}", transaction.getRequest(),
829+
transaction.getResponse());
830+
return;
831+
}
837832

838-
// If this is EMBER_SUCCESS, then do nothing as the command is still not transmitted.
839-
// If there was an error, then we let the system know we've failed already!
840-
if (status == EmberStatus.EMBER_SUCCESS) {
841-
return;
842-
}
843-
zigbeeTransportReceive.receiveCommandState(msgTag, ZigBeeTransportProgressState.TX_NAK);
833+
// If this is EMBER_SUCCESS, then do nothing as the command is still not transmitted.
834+
// If there was an error, then we let the system know we've failed already!
835+
if (status == EmberStatus.EMBER_SUCCESS) {
836+
return;
844837
}
838+
zigbeeTransportReceive.receiveCommandState(msgTag, ZigBeeTransportProgressState.TX_NAK);
845839
}
846840
});
847841
}
@@ -941,18 +935,15 @@ public void handlePacket(EzspFrame response) {
941935

942936
// Message has been completed by the NCP
943937
if (response instanceof EzspMessageSentHandler) {
944-
executorService.execute(new Runnable() {
945-
@Override
946-
public void run() {
947-
EzspMessageSentHandler sentHandler = (EzspMessageSentHandler) response;
948-
ZigBeeTransportProgressState sentHandlerState;
949-
if (sentHandler.getStatus() == EmberStatus.EMBER_SUCCESS) {
950-
sentHandlerState = ZigBeeTransportProgressState.RX_ACK;
951-
} else {
952-
sentHandlerState = ZigBeeTransportProgressState.RX_NAK;
953-
}
954-
zigbeeTransportReceive.receiveCommandState(sentHandler.getMessageTag(), sentHandlerState);
938+
executorService.execute(() -> {
939+
EzspMessageSentHandler sentHandler = (EzspMessageSentHandler) response;
940+
ZigBeeTransportProgressState sentHandlerState;
941+
if (sentHandler.getStatus() == EmberStatus.EMBER_SUCCESS) {
942+
sentHandlerState = ZigBeeTransportProgressState.RX_ACK;
943+
} else {
944+
sentHandlerState = ZigBeeTransportProgressState.RX_NAK;
955945
}
946+
zigbeeTransportReceive.receiveCommandState(sentHandler.getMessageTag(), sentHandlerState);
956947
});
957948
return;
958949
}

com.zsmartsystems.zigbee.dongle.ember/src/main/java/com/zsmartsystems/zigbee/dongle/ember/internal/EzspNeighborTable.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,9 @@ public class EzspNeighborTable {
5555
public EzspNeighborTable(AshFrameHandler ashHandler, int updatePeriod) {
5656
this.ashHandler = ashHandler;
5757

58-
Runnable neighborUpdateThread = new Runnable() {
59-
@Override
60-
public void run() {
61-
updateNeighbors();
62-
}
63-
};
64-
6558
routingEntries = getConfiguration(EzspConfigId.EZSP_CONFIG_SOURCE_ROUTE_TABLE_SIZE);
6659

67-
scheduler.scheduleAtFixedRate(neighborUpdateThread, updatePeriod, updatePeriod, TimeUnit.SECONDS);
60+
scheduler.scheduleAtFixedRate(this::updateNeighbors, updatePeriod, updatePeriod, TimeUnit.SECONDS);
6861
}
6962

7063
private void updateNeighbors() {

0 commit comments

Comments
 (0)