public Object increment(String key, Object obj, BinaryOperator mergeFunction) { return objectCounters.computeIfAbsent(key, k -> new AtomicReference()).accumulateAndGet(obj, mergeFunction); }
private String getCurrentKnownLeader(final boolean cached) throws IOException { final String leader = currentKnownLeader.accumulateAndGet( null, (current, given) -> current == null || !cached ? pickOneHost() : current ); if (leader == null) { throw new IOE("No known server"); } else { return leader; } }
@SafeVarargs public static <T> CompletableFuture<T> allOf( T defaultValue, BinaryOperator<T> merger, CompletableFuture<T>... futures) { @SuppressWarnings("unchecked") final CompletableFuture<Void>[] accumulators = (CompletableFuture<Void>[]) Array.newInstance( CompletableFuture.class, futures.length ); final AtomicReference<T> result = new AtomicReference<>(defaultValue); for (int i = 0; i < futures.length; i++) { final CompletableFuture<T> future = futures[i]; accumulators[i] = future.thenAcceptAsync(r -> result.accumulateAndGet(r, merger)); } return CompletableFuture.allOf(accumulators) .thenApplyAsync(v -> result.get()); }
error.accumulateAndGet( e, Exceptions::chain );
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") private void accumulateCachedValue(Optional<ValueAndBound<T>> valueAndBound) { valueAndBound.ifPresent(presentValue -> cache.accumulateAndGet(presentValue, this::chooseValueWithGreaterBound) ); }
@Override protected T loadValue() { try { return previousValue.accumulateAndGet(supplier.get(), reducer); } catch (Exception e) { T existingValue = previousValue.get(); logger.info("An exception occurred when trying to update the {} gauge for tracking purposes." + " Returning the last known value of {}.", SafeArg.of("gaugeName", shortName), SafeArg.of("existingValue", existingValue), e); return existingValue; } } };
@Override public void onFailure(Exception e) { RemoteTransportException exception = new RemoteTransportException("error while communicating with remote cluster [" + clusterName + "]", e); if (transportException.compareAndSet(null, exception) == false) { exception = transportException.accumulateAndGet(exception, (previous, current) -> { current.addSuppressed(previous); return current; }); } if (responsesCountDown.countDown()) { listener.onFailure(exception); } } });
public static String updateLongestString(AtomicReference<String> observed, String x) { final String longestString = observed.accumulateAndGet(x, maxBy((str1, str2) -> { LOGGER.info("Received str1: {}, str2: {}", str1, str2); return str1.length() - str2.length(); })); LOGGER.info("New observed: {}.", longestString); return longestString; }
@SuppressWarnings("OptionalUsedAsFieldOrParameterType") private void accumulateCachedValue(Optional<ValueAndBound<T>> valueAndBound) { valueAndBound.ifPresent(presentValue -> cache.accumulateAndGet(presentValue, this::chooseValueWithGreaterBound) ); }
public Object increment(String key, Object obj, BinaryOperator mergeFunction) { return objectCounters.computeIfAbsent(key, k -> new AtomicReference()).accumulateAndGet(obj, mergeFunction); }
public static void main(String[] args) { AtomicReference<String> longest = new AtomicReference<>(); List<String> words = Arrays.asList("him", "he", "thanks", "strings", "congratulations", "platform"); words.parallelStream().forEach(next -> { longest.accumulateAndGet(next, (a,b) -> a != null && a.length() > b.length() ? a : b ); }); System.out.println(longest.get()); }
@Override public void accept(T next) { if(accRef == null) { acc = op.apply(acc, next); } else { acc = accRef.accumulateAndGet(next, op); } } }
class Data { long v1, v2; Data add(Data another){ ... } } AtomicReference<Data> aData = ...; public void outTick(long elapsedMsgTime) { Data delta = new Data(1, elapsedMsgTime); aData.accumulateAndGet( delta, Data:add ); }
import java.math.BigInteger; import java.util.Objects; import java.util.concurrent.atomic.AtomicReference; public final class AtomicBigInteger { private final AtomicReference<BigInteger> bigInteger; public AtomicBigInteger(final BigInteger bigInteger) { this.bigInteger = new AtomicReference<>(Objects.requireNonNull(bigInteger)); } // Method references left out for demonstration purposes public BigInteger incrementAndGet() { return bigInteger.accumulateAndGet(BigInteger.ONE, (previous, x) -> previous.add(x)); } public BigInteger getAndIncrement() { return bigInteger.getAndAccumulate(BigInteger.ONE, (previous, x) -> previous.add(x)); } public BigInteger get() { return bigInteger.get(); } }
@Override protected T loadValue() { try { return previousValue.accumulateAndGet(supplier.get(), reducer); } catch (Exception e) { T existingValue = previousValue.get(); logger.info("An exception occurred when trying to update the {} gauge for tracking purposes." + " Returning the last known value of {}.", SafeArg.of("gaugeName", shortName), SafeArg.of("existingValue", existingValue), e); return existingValue; } } };
private String getCurrentKnownLeader(final boolean cached) throws IOException { final String leader = currentKnownLeader.accumulateAndGet( null, (current, given) -> current == null || !cached ? pickOneHost() : current ); if (leader == null) { throw new IOE("No known server"); } else { return leader; } }
private String getCurrentKnownLeader(final boolean cached) throws IOException { final String leader = currentKnownLeader.accumulateAndGet( null, (current, given) -> current == null || !cached ? pickOneHost() : current ); if (leader == null) { throw new IOE("No known server"); } else { return leader; } }
@Override public void onFailure(Exception e) { RemoteTransportException exception = new RemoteTransportException("error while communicating with remote cluster [" + clusterName + "]", e); if (transportException.compareAndSet(null, exception) == false) { exception = transportException.accumulateAndGet(exception, (previous, current) -> { current.addSuppressed(previous); return current; }); } if (responsesCountDown.countDown()) { listener.onFailure(exception); } } });
@Override public void onFailure(Exception e) { TransportException exception = new TransportException("unable to communicate with remote cluster [" + clusterName + "]", e); if (transportException.compareAndSet(null, exception) == false) { exception = transportException.accumulateAndGet(exception, (previous, current) -> { current.addSuppressed(previous); return current; }); } if (responsesCountDown.countDown()) { listener.onFailure(exception); } } });
@Override public Promise<T> reduce(T identity, BinaryOperator<T> accumulator) { Deferred<T> d = promiseFactory.deferred(); AtomicReference<T> iden = new AtomicReference<T>(identity); updateNext(event -> { try { switch(event.getType()) { case DATA: iden.accumulateAndGet(event.getData(), accumulator); return CONTINUE; case CLOSE: d.resolve(iden.get()); break; case ERROR: d.fail(event.getFailure()); break; } close(event.nodata()); return ABORT; } catch (Exception e) { close(PushEvent.error(e)); return ABORT; } }); begin(); return d.getPromise(); }