diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSelector.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSelector.java index ba3f01a3f..0a09ff71f 100644 --- a/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSelector.java +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/AFSelector.java @@ -199,8 +199,8 @@ private void setOpsReady(PollFd pfd, int selectId) { int rops = pfd.rops[i]; AFSelectionKey key = pfd.keys[i]; key.setOpsReady(rops); - if (rops != 0) { - keysRegistered.computeIfPresent(key, (k, v) -> selectId); + if (rops != 0 && keysRegistered.containsKey(key)) { + keysRegistered.put(key, selectId); } } } diff --git a/junixsocket-common/src/main/java/org/newsclub/net/unix/MapValueSet.java b/junixsocket-common/src/main/java/org/newsclub/net/unix/MapValueSet.java index 11adfeb55..7558f5504 100644 --- a/junixsocket-common/src/main/java/org/newsclub/net/unix/MapValueSet.java +++ b/junixsocket-common/src/main/java/org/newsclub/net/unix/MapValueSet.java @@ -25,7 +25,6 @@ import java.util.NoSuchElementException; import java.util.Objects; import java.util.Set; -import java.util.function.Supplier; import org.eclipse.jdt.annotation.NonNull; @@ -41,16 +40,21 @@ */ final class MapValueSet implements Set { private final Map map; - private final Supplier<@NonNull V> valueSupplier; + private final ValueSupplier<@NonNull V> valueSupplier; private final V removedSentinel; @SuppressWarnings("unchecked") - MapValueSet(Map map, Supplier<@NonNull V> valueSupplier, V removedSentinel) { + MapValueSet(Map map, ValueSupplier<@NonNull V> valueSupplier, V removedSentinel) { this.valueSupplier = Objects.requireNonNull(valueSupplier); this.removedSentinel = removedSentinel; this.map = (Map) map; } + @FunctionalInterface + interface ValueSupplier { + V supplyValue(); + } + /** * Marks the given element as "removed"; this may actually add an element to the underlying map. *

@@ -83,7 +87,7 @@ public void markAllRemoved() { } private @NonNull V getValue() { - return Objects.requireNonNull(valueSupplier.get()); + return Objects.requireNonNull(valueSupplier.supplyValue()); } @Override @@ -220,8 +224,13 @@ public E[] toArray(E[] a) { * * @param e The entry to update. */ - public void update(T e) { - map.computeIfPresent(e, (k, v) -> getValue()); + public boolean update(T e) { + if (map.containsKey(e)) { + map.put(e, getValue()); + return true; + } else { + return false; + } } /** @@ -229,13 +238,12 @@ public void update(T e) { */ @Override public boolean add(T e) { - if (!map.containsKey(e)) { - map.computeIfAbsent(e, (k) -> getValue()); - return true; - } else if (contains(e)) { + if (contains(e)) { return false; + } else if (update(e)) { + return true; } else { - update(e); + map.put(e, getValue()); return true; } }