Search in sources :

Example 1 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

the class ConcurrentHashMultiset method add.

// Modification Operations
/**
   * Adds a number of occurrences of the specified element to this multiset.
   *
   * @param element the element to add
   * @param occurrences the number of occurrences to add
   * @return the previous count of the element before the operation; possibly zero
   * @throws IllegalArgumentException if {@code occurrences} is negative, or if
   *     the resulting amount would exceed {@link Integer#MAX_VALUE}
   */
@CanIgnoreReturnValue
@Override
public int add(E element, int occurrences) {
    checkNotNull(element);
    if (occurrences == 0) {
        return count(element);
    }
    CollectPreconditions.checkPositive(occurrences, "occurences");
    while (true) {
        AtomicInteger existingCounter = Maps.safeGet(countMap, element);
        if (existingCounter == null) {
            existingCounter = countMap.putIfAbsent(element, new AtomicInteger(occurrences));
            if (existingCounter == null) {
                return 0;
            }
        // existingCounter != null: fall through to operate against the existing AtomicInteger
        }
        while (true) {
            int oldValue = existingCounter.get();
            if (oldValue != 0) {
                try {
                    int newValue = IntMath.checkedAdd(oldValue, occurrences);
                    if (existingCounter.compareAndSet(oldValue, newValue)) {
                        // newValue can't == 0, so no need to check & remove
                        return oldValue;
                    }
                } catch (ArithmeticException overflow) {
                    throw new IllegalArgumentException("Overflow adding " + occurrences + " occurrences to a count of " + oldValue);
                }
            } else {
                // In the case of a concurrent remove, we might observe a zero value, which means another
                // thread is about to remove (element, existingCounter) from the map. Rather than wait,
                // we can just do that work here.
                AtomicInteger newCounter = new AtomicInteger(occurrences);
                if ((countMap.putIfAbsent(element, newCounter) == null) || countMap.replace(element, existingCounter, newCounter)) {
                    return 0;
                }
                break;
            }
        }
    // If we're still here, there was a race, so just try again.
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 2 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

the class AbstractFuture method cancel.

/**
   * {@inheritDoc}
   *
   * <p>If a cancellation attempt succeeds on a {@code Future} that had previously been {@linkplain
   * #setFuture set asynchronously}, then the cancellation will also be propagated to the delegate
   * {@code Future} that was supplied in the {@code setFuture} call.
   */
@CanIgnoreReturnValue
@Override
public boolean cancel(boolean mayInterruptIfRunning) {
    Object localValue = value;
    boolean rValue = false;
    if (localValue == null | localValue instanceof AbstractFuture.SetFuture) {
        // Try to delay allocating the exception. At this point we may still lose the CAS, but it is
        // certainly less likely.
        Throwable cause = GENERATE_CANCELLATION_CAUSES ? new CancellationException("Future.cancel() was called.") : null;
        Object valueToSet = new Cancellation(mayInterruptIfRunning, cause);
        AbstractFuture<?> abstractFuture = this;
        while (true) {
            if (ATOMIC_HELPER.casValue(abstractFuture, localValue, valueToSet)) {
                rValue = true;
                // FutureTask
                if (mayInterruptIfRunning) {
                    abstractFuture.interruptTask();
                }
                complete(abstractFuture);
                if (localValue instanceof AbstractFuture.SetFuture) {
                    // propagate cancellation to the future set in setfuture, this is racy, and we don't
                    // care if we are successful or not.
                    ListenableFuture<?> futureToPropagateTo = ((AbstractFuture.SetFuture) localValue).future;
                    if (futureToPropagateTo instanceof TrustedFuture) {
                        // If the future is a TrustedFuture then we specifically avoid calling cancel()
                        // this has 2 benefits
                        // 1. for long chains of futures strung together with setFuture we consume less stack
                        // 2. we avoid allocating Cancellation objects at every level of the cancellation
                        //    chain
                        // We can only do this for TrustedFuture, because TrustedFuture.cancel is final and
                        // does nothing but delegate to this method.
                        AbstractFuture<?> trusted = (AbstractFuture<?>) futureToPropagateTo;
                        localValue = trusted.value;
                        if (localValue == null | localValue instanceof AbstractFuture.SetFuture) {
                            abstractFuture = trusted;
                            // loop back up and try to complete the new future
                            continue;
                        }
                    } else {
                        // not a TrustedFuture, call cancel directly.
                        futureToPropagateTo.cancel(mayInterruptIfRunning);
                    }
                }
                break;
            }
            // obj changed, reread
            localValue = abstractFuture.value;
            if (!(localValue instanceof AbstractFuture.SetFuture)) {
                // since it isn't a SetFuture, then the future must be done and we should exit the loop
                break;
            }
        }
    }
    return rValue;
}
Also used : CancellationException(java.util.concurrent.CancellationException) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 3 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

the class AbstractService method stopAsync.

@CanIgnoreReturnValue
@Override
public final Service stopAsync() {
    if (monitor.enterIf(isStoppable)) {
        try {
            State previous = state();
            switch(previous) {
                case NEW:
                    snapshot = new StateSnapshot(TERMINATED);
                    terminated(NEW);
                    break;
                case STARTING:
                    snapshot = new StateSnapshot(STARTING, true, null);
                    stopping(STARTING);
                    break;
                case RUNNING:
                    snapshot = new StateSnapshot(STOPPING);
                    stopping(RUNNING);
                    doStop();
                    break;
                case STOPPING:
                case TERMINATED:
                case FAILED:
                    // These cases are impossible due to the if statement above.
                    throw new AssertionError("isStoppable is incorrectly implemented, saw: " + previous);
                default:
                    throw new AssertionError("Unexpected state: " + previous);
            }
        } catch (Throwable shutdownFailure) {
            notifyFailed(shutdownFailure);
        } finally {
            monitor.leave();
            executeListeners();
        }
    }
    return this;
}
Also used : Preconditions.checkState(com.google_voltpatches.common.base.Preconditions.checkState) State(com.google_voltpatches.common.util.concurrent.Service.State) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 4 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

the class Resources method getResource.

/**
   * Returns a {@code URL} pointing to {@code resourceName} if the resource is found using the
   * {@linkplain Thread#getContextClassLoader() context class loader}. In simple environments, the
   * context class loader will find resources from the class path. In environments where different
   * threads can have different class loaders, for example app servers, the context class loader
   * will typically have been set to an appropriate loader for the current thread.
   *
   * <p>In the unusual case where the context class loader is null, the class loader that loaded
   * this class ({@code Resources}) will be used instead.
   * 
   * @throws IllegalArgumentException if the resource is not found
   */
// being used to check if a resource exists
@CanIgnoreReturnValue
public static // e.g. Optional<URL> tryGetResource or boolean resourceExists
URL getResource(String resourceName) {
    ClassLoader loader = MoreObjects.firstNonNull(Thread.currentThread().getContextClassLoader(), Resources.class.getClassLoader());
    URL url = loader.getResource(resourceName);
    checkArgument(url != null, "resource %s not found.", resourceName);
    return url;
}
Also used : URL(java.net.URL) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 5 with CanIgnoreReturnValue

use of com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue in project voltdb by VoltDB.

the class ConfigurableMutableNetwork method removeNode.

@Override
@CanIgnoreReturnValue
public boolean removeNode(Object node) {
    checkNotNull(node, "node");
    NetworkConnections<N, E> connections = nodeConnections.get(node);
    if (connections == null) {
        return false;
    }
    // Thus we avoid modifying the underlying view while iterating over it.
    for (E edge : ImmutableList.copyOf(connections.incidentEdges())) {
        removeEdge(edge);
    }
    nodeConnections.remove(node);
    return true;
}
Also used : REUSING_EDGE(com.google_voltpatches.common.graph.GraphConstants.REUSING_EDGE) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Aggregations

CanIgnoreReturnValue (com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BufferedInputStream (java.io.BufferedInputStream)3 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 Beta (com.google_voltpatches.common.annotations.Beta)2 Preconditions.checkState (com.google_voltpatches.common.base.Preconditions.checkState)2 State (com.google_voltpatches.common.util.concurrent.Service.State)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 CharBuffer (java.nio.CharBuffer)2 REUSING_EDGE (com.google_voltpatches.common.graph.GraphConstants.REUSING_EDGE)1 BufferedOutputStream (java.io.BufferedOutputStream)1 BufferedWriter (java.io.BufferedWriter)1 URL (java.net.URL)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 CancellationException (java.util.concurrent.CancellationException)1