Search in sources :

Example 1 with Beta

use of com.google.common.annotations.Beta in project guava by google.

the class Futures method inCompletionOrder.

/**
   * Returns a list of delegate futures that correspond to the futures received in the order that
   * they complete. Delegate futures return the same value or throw the same exception as the
   * corresponding input future returns/throws.
   *
   * <p>"In the order that they complete" means, for practical purposes, about what you would
   * expect, but there are some subtleties. First, we do guarantee that, if the output future at
   * index n is done, the output future at index n-1 is also done. (But as usual with futures, some
   * listeners for future n may complete before some for future n-1.) However, it is possible, if
   * one input completes with result X and another later with result Y, for Y to come before X in
   * the output future list. (Such races are impossible to solve without global synchronization of
   * all future completions. And they should have little practical impact.)
   *
   * <p>Cancelling a delegate future has no effect on any input future, since the delegate future
   * does not correspond to a specific input future until the appropriate number of input futures
   * have completed. At that point, it is too late to cancel the input future. The input future's
   * result, which cannot be stored into the cancelled delegate future, is ignored.
   *
   * @since 17.0
   */
@Beta
public static <T> ImmutableList<ListenableFuture<T>> inCompletionOrder(Iterable<? extends ListenableFuture<? extends T>> futures) {
    ImmutableList<ListenableFuture<? extends T>> copy = ImmutableList.copyOf(futures);
    ImmutableList.Builder<SettableFuture<T>> delegatesBuilder = ImmutableList.builder();
    for (int i = 0; i < copy.size(); i++) {
        delegatesBuilder.add(SettableFuture.create());
    }
    final ImmutableList<SettableFuture<T>> delegates = delegatesBuilder.build();
    final AtomicInteger delegateIndex = new AtomicInteger();
    for (final ListenableFuture<? extends T> future : copy) {
        future.addListener(new Runnable() {

            @Override
            public void run() {
                for (int i = delegateIndex.get(); i < delegates.size(); i++) {
                    if (delegates.get(i).setFuture(future)) {
                        // this is technically unnecessary, but should speed up later accesses
                        delegateIndex.set(i + 1);
                        return;
                    }
                }
            // if we get here it means that one of the output futures was cancelled
            // nothing we can do
            }
        }, directExecutor());
    }
    @SuppressWarnings("unchecked") ImmutableList<ListenableFuture<T>> delegatesCast = (ImmutableList) delegates;
    return delegatesCast;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Beta(com.google.common.annotations.Beta)

Example 2 with Beta

use of com.google.common.annotations.Beta in project spf4j by zolyfarkas.

the class SchemaUtils method writeIdl.

@Beta
public static void writeIdl(final Appendable appendable, final Set<String> alreadyDeclared, final String protocolNameSpace, final Schema... pschemas) throws IOException {
    JsonGenerator jsonGen;
    if (appendable instanceof Writer) {
        jsonGen = JSON_FACT.createJsonGenerator((Writer) appendable);
    } else {
        jsonGen = JSON_FACT.createJsonGenerator(new AppendableWriter(appendable));
    }
    final Set<Schema> toDeclare = new HashSet<>(4);
    toDeclare.addAll(Arrays.asList(pschemas));
    while (!toDeclare.isEmpty()) {
        Iterator<Schema> iterator = toDeclare.iterator();
        Schema schema = iterator.next();
        iterator.remove();
        writeSchema(schema, appendable, jsonGen, protocolNameSpace, alreadyDeclared, toDeclare);
        appendable.append('\n');
    }
}
Also used : AppendableWriter(org.spf4j.io.AppendableWriter) Schema(org.apache.avro.Schema) JsonGenerator(org.codehaus.jackson.JsonGenerator) StringWriter(java.io.StringWriter) AppendableWriter(org.spf4j.io.AppendableWriter) Writer(java.io.Writer) HashSet(java.util.HashSet) Beta(com.google.common.annotations.Beta)

Example 3 with Beta

use of com.google.common.annotations.Beta in project spf4j by zolyfarkas.

the class SchemaUtils method writeIdlLegacy.

@Beta
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
public static void writeIdlLegacy(final Appendable appendable, final Set<String> alreadyDeclared, final String protocolNameSpace, final Schema... pschemas) throws IOException {
    MutableGraph<Schema> schemaDeps = GraphBuilder.directed().allowsSelfLoops(false).expectedNodeCount(4).build();
    Map<Schema, String> idlRepresentation = new HashMap<>(4);
    final Set<Schema> toDeclare = new HashSet<>(4);
    toDeclare.addAll(Arrays.asList(pschemas));
    while (!toDeclare.isEmpty()) {
        Iterator<Schema> iterator = toDeclare.iterator();
        Schema schema = iterator.next();
        iterator.remove();
        StringWriter schemaIdrStr = new StringWriter();
        JsonGenerator jsonGen = JSON_FACT.createJsonGenerator(schemaIdrStr);
        Set<Schema> orig = new HashSet<>(toDeclare);
        writeSchema(schema, schemaIdrStr, jsonGen, protocolNameSpace, alreadyDeclared, toDeclare);
        idlRepresentation.put(schema, schemaIdrStr.toString());
        schemaDeps.addNode(schema);
        Set<Schema> dependencies = Sets.difference(toDeclare, orig);
        for (Schema dep : dependencies) {
            schemaDeps.putEdge(schema, dep);
        }
    }
    // Now process the nodes from leaves onward.
    MutableGraph<Schema> traverseGraph = Graphs.clone(schemaDeps);
    Set<Schema> nodes = traverseGraph.nodes();
    List<Schema> nodesToRemove = new ArrayList<>();
    do {
        for (Schema token : nodes) {
            if (traverseGraph.outDegree(token) == 0) {
                nodesToRemove.add(token);
                appendable.append(idlRepresentation.get(token));
                appendable.append('\n');
            }
        }
        if (nodesToRemove.isEmpty() && !nodes.isEmpty()) {
            throw new IllegalArgumentException("Schema definition cycle for" + nodes);
        }
        for (Schema token : nodesToRemove) {
            traverseGraph.removeNode(token);
        }
        nodesToRemove.clear();
        nodes = traverseGraph.nodes();
    } while (!nodes.isEmpty());
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) HashSet(java.util.HashSet) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Beta(com.google.common.annotations.Beta)

Example 4 with Beta

use of com.google.common.annotations.Beta in project guava by google.

the class CharSource method length.

/**
 * Returns the length of this source in chars, even if doing so requires opening and traversing an
 * entire stream. To avoid a potentially expensive operation, see {@link #lengthIfKnown}.
 *
 * <p>The default implementation calls {@link #lengthIfKnown} and returns the value if present. If
 * absent, it will fall back to a heavyweight operation that will open a stream, {@link
 * Reader#skip(long) skip} to the end of the stream, and return the total number of chars that
 * were skipped.
 *
 * <p>Note that for sources that implement {@link #lengthIfKnown} to provide a more efficient
 * implementation, it is <i>possible</i> that this method will return a different number of chars
 * than would be returned by reading all of the chars.
 *
 * <p>In either case, for mutable sources such as files, a subsequent read may return a different
 * number of chars if the contents are changed.
 *
 * @throws IOException if an I/O error occurs while reading the length of this source
 * @since 19.0
 */
@Beta
public long length() throws IOException {
    Optional<Long> lengthIfKnown = lengthIfKnown();
    if (lengthIfKnown.isPresent()) {
        return lengthIfKnown.get();
    }
    Closer closer = Closer.create();
    try {
        Reader reader = closer.register(openStream());
        return countBySkipping(reader);
    } catch (Throwable e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
}
Also used : Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) Beta(com.google.common.annotations.Beta)

Example 5 with Beta

use of com.google.common.annotations.Beta in project guava by google.

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#readLine()}, this method considers a line to be a sequence of
 * text that is terminated by (but does not include) one of {@code \r\n}, {@code \r} or {@code
 * \n}. If the source's content does not end in a line termination sequence, it is treated as if
 * it does.
 *
 * @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
@ParametricNullness
public <T extends @Nullable Object> 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) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue) Beta(com.google.common.annotations.Beta)

Aggregations

Beta (com.google.common.annotations.Beta)17 CanIgnoreReturnValue (com.google.errorprone.annotations.CanIgnoreReturnValue)3 HashSet (java.util.HashSet)3 BufferedReader (java.io.BufferedReader)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 TimeUnit (java.util.concurrent.TimeUnit)2 Schema (org.apache.avro.Schema)2 JsonGenerator (org.codehaus.jackson.JsonGenerator)2 PivotTable (tech.tablesaw.aggregate.PivotTable)2 GwtIncompatible (com.google.common.annotations.GwtIncompatible)1 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 Streams (com.google.common.collect.Streams)1 Ints (com.google.common.primitives.Ints)1