Search in sources :

Example 16 with CanIgnoreReturnValue

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

the class ConcurrentHashMultiset method removeExactly.

/**
   * Removes exactly the specified number of occurrences of {@code element}, or makes no
   * change if this is not possible.
   *
   * <p>This method, in contrast to {@link #remove(Object, int)}, has no effect when the
   * element count is smaller than {@code occurrences}.
   *
   * @param element the element to remove
   * @param occurrences the number of occurrences of {@code element} to remove
   * @return {@code true} if the removal was possible (including if {@code occurrences} is zero)
   * @throws IllegalArgumentException if {@code occurrences} is negative
   */
@CanIgnoreReturnValue
public boolean removeExactly(@Nullable Object element, int occurrences) {
    if (occurrences == 0) {
        return true;
    }
    CollectPreconditions.checkPositive(occurrences, "occurences");
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
        return false;
    }
    while (true) {
        int oldValue = existingCounter.get();
        if (oldValue < occurrences) {
            return false;
        }
        int newValue = oldValue - occurrences;
        if (existingCounter.compareAndSet(oldValue, newValue)) {
            if (newValue == 0) {
                // Just CASed to 0; remove the entry to clean up the map. If the removal fails,
                // another thread has already replaced it with a new counter, which is fine.
                countMap.remove(element, existingCounter);
            }
            return true;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 17 with CanIgnoreReturnValue

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

the class CharSource method copyTo.

/**
   * Copies the contents of this source to the given sink.
   *
   * @return the number of characters copied
   * @throws IOException if an I/O error occurs in the process of reading from this source or
   *     writing to {@code sink}
   */
@CanIgnoreReturnValue
public long copyTo(CharSink sink) throws IOException {
    checkNotNull(sink);
    Closer closer = Closer.create();
    try {
        Reader reader = closer.register(openStream());
        Writer writer = closer.register(sink.openStream());
        return CharStreams.copy(reader, writer);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) Writer(java.io.Writer) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 18 with CanIgnoreReturnValue

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

the class CharSource method copyTo.

/**
   * Appends the contents of this source to the given {@link Appendable} (such as a {@link Writer}).
   * Does not close {@code appendable} if it is {@code Closeable}.
   *
   * @return the number of characters copied
   * @throws IOException if an I/O error occurs in the process of reading from this source or
   *     writing to {@code appendable}
   */
@CanIgnoreReturnValue
public long copyTo(Appendable appendable) throws IOException {
    checkNotNull(appendable);
    Closer closer = Closer.create();
    try {
        Reader reader = closer.register(openStream());
        return CharStreams.copy(reader, appendable);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 19 with CanIgnoreReturnValue

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

the class CharStreams method copy.

/**
   * Copies all characters between the {@link Readable} and {@link Appendable} objects. Does not
   * close or flush either object.
   *
   * @param from the object to read from
   * @param to the object to write to
   * @return the number of characters copied
   * @throws IOException if an I/O error occurs
   */
@CanIgnoreReturnValue
public static long copy(Readable from, Appendable to) throws IOException {
    checkNotNull(from);
    checkNotNull(to);
    CharBuffer buf = createBuffer();
    long total = 0;
    while (from.read(buf) != -1) {
        buf.flip();
        to.append(buf);
        total += buf.remaining();
        buf.clear();
    }
    return total;
}
Also used : CharBuffer(java.nio.CharBuffer) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 20 with CanIgnoreReturnValue

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

the class ServiceManager method startAsync.

/**
   * Initiates service {@linkplain Service#startAsync startup} on all the services being managed. It
   * is only valid to call this method if all of the services are {@linkplain State#NEW new}.
   *
   * @return this
   * @throws IllegalStateException if any of the Services are not {@link State#NEW new} when the
   *     method is called.
   */
@CanIgnoreReturnValue
public ServiceManager startAsync() {
    for (Service service : services) {
        State state = service.state();
        checkState(state == NEW, "Service %s is %s, cannot start it.", service, state);
    }
    for (Service service : services) {
        try {
            state.tryStartTiming(service);
            service.startAsync();
        } catch (IllegalStateException e) {
            // This can happen if the service has already been started or stopped (e.g. by another
            // service or listener). Our contract says it is safe to call this method if
            // all services were NEW when it was called, and this has already been verified above, so we
            // don't propagate the exception.
            logger.log(Level.WARNING, "Unable to start Service " + service, e);
        }
    }
    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)

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