private AtomicBoolean addInternal(PacketRequest request) { AtomicBoolean firstRequest = new AtomicBoolean(false); requests.compute( request.selector(), (s, existingRequests) -> { if (existingRequests == null) { firstRequest.set(true); return ImmutableSet.of(request); } else if (!existingRequests.contains(request)) { return ImmutableSet.<PacketRequest>builder() .addAll(existingRequests) .add(request) .build(); } else { return existingRequests; } }); return firstRequest; }
private AtomicBoolean removeInternal(PacketRequest request) { AtomicBoolean removedLast = new AtomicBoolean(false); requests.computeIfPresent( request.selector(), (s, existingRequests) -> { if (existingRequests.contains(request)) { Set<PacketRequest> newRequests = Sets.newHashSet(existingRequests); newRequests.remove(request); if (newRequests.size() > 0) { return ImmutableSet.copyOf(newRequests); } else { removedLast.set(true); return null; } } else { return existingRequests; } }); return removedLast; }