Skip to content

Commit efacb5a

Browse files
committed
AFSelector: Bring back Java 7 compatibility
Fix errors only occurring with -Drelease #145
1 parent 470e426 commit efacb5a

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

junixsocket-common/src/main/java/org/newsclub/net/unix/AFSelector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ private void setOpsReady(PollFd pfd, int selectId) {
199199
int rops = pfd.rops[i];
200200
AFSelectionKey key = pfd.keys[i];
201201
key.setOpsReady(rops);
202-
if (rops != 0) {
203-
keysRegistered.computeIfPresent(key, (k, v) -> selectId);
202+
if (rops != 0 && keysRegistered.containsKey(key)) {
203+
keysRegistered.put(key, selectId);
204204
}
205205
}
206206
}

junixsocket-common/src/main/java/org/newsclub/net/unix/MapValueSet.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.NoSuchElementException;
2626
import java.util.Objects;
2727
import java.util.Set;
28-
import java.util.function.Supplier;
2928

3029
import org.eclipse.jdt.annotation.NonNull;
3130

@@ -41,16 +40,21 @@
4140
*/
4241
final class MapValueSet<T, V> implements Set<T> {
4342
private final Map<T, V> map;
44-
private final Supplier<@NonNull V> valueSupplier;
43+
private final ValueSupplier<@NonNull V> valueSupplier;
4544
private final V removedSentinel;
4645

4746
@SuppressWarnings("unchecked")
48-
MapValueSet(Map<? extends T, V> map, Supplier<@NonNull V> valueSupplier, V removedSentinel) {
47+
MapValueSet(Map<? extends T, V> map, ValueSupplier<@NonNull V> valueSupplier, V removedSentinel) {
4948
this.valueSupplier = Objects.requireNonNull(valueSupplier);
5049
this.removedSentinel = removedSentinel;
5150
this.map = (Map<T, V>) map;
5251
}
5352

53+
@FunctionalInterface
54+
interface ValueSupplier<V> {
55+
V supplyValue();
56+
}
57+
5458
/**
5559
* Marks the given element as "removed"; this may actually add an element to the underlying map.
5660
* <p>
@@ -83,7 +87,7 @@ public void markAllRemoved() {
8387
}
8488

8589
private @NonNull V getValue() {
86-
return Objects.requireNonNull(valueSupplier.get());
90+
return Objects.requireNonNull(valueSupplier.supplyValue());
8791
}
8892

8993
@Override
@@ -220,22 +224,26 @@ public <E> E[] toArray(E[] a) {
220224
*
221225
* @param e The entry to update.
222226
*/
223-
public void update(T e) {
224-
map.computeIfPresent(e, (k, v) -> getValue());
227+
public boolean update(T e) {
228+
if (map.containsKey(e)) {
229+
map.put(e, getValue());
230+
return true;
231+
} else {
232+
return false;
233+
}
225234
}
226235

227236
/**
228237
* Adds an entry to the set, adding it to the backing map if necessary.
229238
*/
230239
@Override
231240
public boolean add(T e) {
232-
if (!map.containsKey(e)) {
233-
map.computeIfAbsent(e, (k) -> getValue());
234-
return true;
235-
} else if (contains(e)) {
241+
if (contains(e)) {
236242
return false;
243+
} else if (update(e)) {
244+
return true;
237245
} else {
238-
update(e);
246+
map.put(e, getValue());
239247
return true;
240248
}
241249
}

0 commit comments

Comments
 (0)