Search in sources :

Example 1 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

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.errorprone.annotations.CanIgnoreReturnValue)

Example 2 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

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.errorprone.annotations.CanIgnoreReturnValue)

Example 3 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class ConcurrentHashMultiset method remove.

/**
   * Removes a number of occurrences of the specified element from this multiset. If the multiset
   * contains fewer than this number of occurrences to begin with, all occurrences will be removed.
   *
   * @param element the element whose occurrences should be removed
   * @param occurrences the number of occurrences of the element to remove
   * @return the count of the element before the operation; possibly zero
   * @throws IllegalArgumentException if {@code occurrences} is negative
   */
/*
   * TODO(cpovirk): remove and removeExactly currently accept null inputs only
   * if occurrences == 0. This satisfies both NullPointerTester and
   * CollectionRemoveTester.testRemove_nullAllowed, but it's not clear that it's
   * a good policy, especially because, in order for the test to pass, the
   * parameter must be misleadingly annotated as @Nullable. I suspect that
   * we'll want to remove @Nullable, add an eager checkNotNull, and loosen up
   * testRemove_nullAllowed.
   */
@CanIgnoreReturnValue
@Override
public int remove(@Nullable Object element, int occurrences) {
    if (occurrences == 0) {
        return count(element);
    }
    CollectPreconditions.checkPositive(occurrences, "occurences");
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
        return 0;
    }
    while (true) {
        int oldValue = existingCounter.get();
        if (oldValue != 0) {
            int newValue = Math.max(0, 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 oldValue;
            }
        } else {
            return 0;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 4 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class ByteSink method writeFrom.

/**
   * Writes all the bytes from the given {@code InputStream} to this sink. Does not close
   * {@code input}.
   *
   * @return the number of bytes written
   * @throws IOException if an I/O occurs while reading from {@code input} or writing to this sink
   */
@CanIgnoreReturnValue
public long writeFrom(InputStream input) throws IOException {
    checkNotNull(input);
    Closer closer = Closer.create();
    try {
        OutputStream out = closer.register(openStream());
        long written = ByteStreams.copy(input, out);
        // https://code.google.com/p/guava-libraries/issues/detail?id=1330
        out.flush();
        return written;
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Example 5 with CanIgnoreReturnValue

use of com.google.errorprone.annotations.CanIgnoreReturnValue in project guava by google.

the class ByteSource method read.

/**
   * Reads the contents of this byte source using the given {@code processor} to process bytes as
   * they are read. Stops when all bytes have been read or the consumer returns {@code false}.
   * Returns the result produced by the processor.
   *
   * @throws IOException if an I/O error occurs while reading from this source or if
   *     {@code processor} throws an {@code IOException}
   * @since 16.0
   */
@Beta
// some processors won't return a useful result
@CanIgnoreReturnValue
public <T> T read(ByteProcessor<T> processor) throws IOException {
    checkNotNull(processor);
    Closer closer = Closer.create();
    try {
        InputStream in = closer.register(openStream());
        return ByteStreams.readBytes(in, processor);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue) Beta(com.google.common.annotations.Beta)

Aggregations

CanIgnoreReturnValue (com.google.errorprone.annotations.CanIgnoreReturnValue)25 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.common.annotations.Beta)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)2 State (com.google.common.util.concurrent.Service.State)2 OutputStream (java.io.OutputStream)2 Writer (java.io.Writer)2 CharBuffer (java.nio.CharBuffer)2 CancellationException (java.util.concurrent.CancellationException)2 GwtIncompatible (com.google.common.annotations.GwtIncompatible)1 REUSING_EDGE (com.google.common.graph.GraphConstants.REUSING_EDGE)1 Any (com.google.protobuf.Any)1 ChannelFuture (io.netty.channel.ChannelFuture)1 Query (io.spine.client.Query)1 Subscription (io.spine.client.Subscription)1