Skip to content

Commit

Permalink
AFSelector: Bring back Java 7 compatibility
Browse files Browse the repository at this point in the history
Fix errors only occurring with -Drelease

#145
  • Loading branch information
kohlschuetter committed Nov 12, 2023
1 parent 470e426 commit efacb5a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,16 +40,21 @@
*/
final class MapValueSet<T, V> implements Set<T> {
private final Map<T, V> map;
private final Supplier<@NonNull V> valueSupplier;
private final ValueSupplier<@NonNull V> valueSupplier;
private final V removedSentinel;

@SuppressWarnings("unchecked")
MapValueSet(Map<? extends T, V> map, Supplier<@NonNull V> valueSupplier, V removedSentinel) {
MapValueSet(Map<? extends T, V> map, ValueSupplier<@NonNull V> valueSupplier, V removedSentinel) {
this.valueSupplier = Objects.requireNonNull(valueSupplier);
this.removedSentinel = removedSentinel;
this.map = (Map<T, V>) map;
}

@FunctionalInterface
interface ValueSupplier<V> {
V supplyValue();
}

/**
* Marks the given element as "removed"; this may actually add an element to the underlying map.
* <p>
Expand Down Expand Up @@ -83,7 +87,7 @@ public void markAllRemoved() {
}

private @NonNull V getValue() {
return Objects.requireNonNull(valueSupplier.get());
return Objects.requireNonNull(valueSupplier.supplyValue());
}

@Override
Expand Down Expand Up @@ -220,22 +224,26 @@ public <E> 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;
}
}

/**
* Adds an entry to the set, adding it to the backing map if necessary.
*/
@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;
}
}
Expand Down

0 comments on commit efacb5a

Please sign in to comment.