only: refactoring: simplify `SerialPortCache.retainAll` method
This commit is contained in:
parent
42e2e75a9e
commit
a7688c58d7
|
@ -4,10 +4,8 @@ import com.devexperts.logging.Logging;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
@ -23,7 +21,6 @@ public class SerialPortCache {
|
||||||
if (cachedPort.isExpired()) {
|
if (cachedPort.isExpired()) {
|
||||||
cachedPorts.remove(serialPort);
|
cachedPorts.remove(serialPort);
|
||||||
log.info("Expired port is removed: " + serialPort);
|
log.info("Expired port is removed: " + serialPort);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return Optional.of(cachedPort.port);
|
return Optional.of(cachedPort.port);
|
||||||
}
|
}
|
||||||
|
@ -36,21 +33,10 @@ public class SerialPortCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
void retainAll(final Set<String> serialPortsToRetain) {
|
void retainAll(final Set<String> serialPortsToRetain) {
|
||||||
// Clean the port cache of any entries that no longer exist
|
final int cachedPortCount = cachedPorts.size();
|
||||||
// If the same port appears later, we want to re-probe it at that time
|
if (cachedPorts.keySet().retainAll(serialPortsToRetain)) {
|
||||||
// In any other scenario, auto could have unexpected behavior for the user
|
log.info(String.format("%d disappeared ports are removed", cachedPortCount - cachedPorts.size()));
|
||||||
List<String> toRemove = new ArrayList<>();
|
|
||||||
for (String x : cachedPorts.keySet()) {
|
|
||||||
if (!serialPortsToRetain.contains(x)) {
|
|
||||||
toRemove.add(x);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// two steps to avoid ConcurrentModificationException
|
|
||||||
toRemove.forEach(p -> {
|
|
||||||
cachedPorts.remove(p);
|
|
||||||
log.info("Disappeared port is removed: " + p);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class CachedPort {
|
private static class CachedPort {
|
||||||
|
|
Loading…
Reference in New Issue