Search in sources :

Example 11 with CanIgnoreReturnValue

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

the class CharSource method readLines.

/**
   * Reads lines of text from this source, processing each line as it is read using the given
   * {@link LineProcessor processor}. Stops when all lines have been processed or the processor
   * returns {@code false} and returns the result produced by the processor.
   *
   * <p>Like {@link BufferedReader}, this method breaks lines on any of {@code \n}, {@code \r} or
   * {@code \r\n}, does not include the line separator in the lines passed to the {@code processor}
   * and does not consider there to be an extra empty line at the end if the content is terminated
   * with a line separator.
   *
   * @throws IOException if an I/O error occurs in the process of 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 readLines(LineProcessor<T> processor) throws IOException {
    checkNotNull(processor);
    Closer closer = Closer.create();
    try {
        Reader reader = closer.register(openStream());
        return CharStreams.readLines(reader, processor);
    } 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) Beta(com.google_voltpatches.common.annotations.Beta)

Example 12 with CanIgnoreReturnValue

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

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

Example 13 with CanIgnoreReturnValue

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

the class ConcurrentHashMultiset method setCount.

/**
   * Sets the number of occurrences of {@code element} to {@code newCount}, but only if
   * the count is currently {@code expectedOldCount}. If {@code element} does not appear
   * in the multiset exactly {@code expectedOldCount} times, no changes will be made.
   *
   * @return {@code true} if the change was successful. This usually indicates
   *     that the multiset has been modified, but not always: in the case that
   *     {@code expectedOldCount == newCount}, the method will return {@code true} if
   *     the condition was met.
   * @throws IllegalArgumentException if {@code expectedOldCount} or {@code newCount} is negative
   */
@CanIgnoreReturnValue
@Override
public boolean setCount(E element, int expectedOldCount, int newCount) {
    checkNotNull(element);
    checkNonnegative(expectedOldCount, "oldCount");
    checkNonnegative(newCount, "newCount");
    AtomicInteger existingCounter = Maps.safeGet(countMap, element);
    if (existingCounter == null) {
        if (expectedOldCount != 0) {
            return false;
        } else if (newCount == 0) {
            return true;
        } else {
            // if our write lost the race, it must have lost to a nonzero value, so we can stop
            return countMap.putIfAbsent(element, new AtomicInteger(newCount)) == null;
        }
    }
    int oldValue = existingCounter.get();
    if (oldValue == expectedOldCount) {
        if (oldValue == 0) {
            if (newCount == 0) {
                // Just observed a 0; try to remove the entry to clean up the map
                countMap.remove(element, existingCounter);
                return true;
            } else {
                AtomicInteger newCounter = new AtomicInteger(newCount);
                return (countMap.putIfAbsent(element, newCounter) == null) || countMap.replace(element, existingCounter, newCounter);
            }
        } else {
            if (existingCounter.compareAndSet(oldValue, newCount)) {
                if (newCount == 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;
            }
        }
    }
    return false;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CanIgnoreReturnValue(com.google_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 14 with CanIgnoreReturnValue

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

the class ByteSource method copyTo.

/**
   * Copies the contents of this byte source to the given {@code OutputStream}. Does not close
   * {@code output}.
   *
   * @return the number of bytes copied
   * @throws IOException if an I/O error occurs in the process of reading from this source or
   *     writing to {@code output}
   */
@CanIgnoreReturnValue
public long copyTo(OutputStream output) throws IOException {
    checkNotNull(output);
    Closer closer = Closer.create();
    try {
        InputStream in = closer.register(openStream());
        return ByteStreams.copy(in, output);
    } 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_voltpatches.errorprone.annotations.CanIgnoreReturnValue)

Example 15 with CanIgnoreReturnValue

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

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 in the process of 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_voltpatches.errorprone.annotations.CanIgnoreReturnValue) Beta(com.google_voltpatches.common.annotations.Beta)

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