Example usage for com.google.common.collect ImmutableList builder

List of usage examples for com.google.common.collect ImmutableList builder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableList builder.

Prototype

public static <E> Builder<E> builder() 

Source Link

Usage

From source file:ru.adios.budgeter.util.Immutables.java

@SuppressWarnings("unchecked")
public static <T> Collector<T, ImmutableList.Builder<T>, ImmutableList<T>> getListCollector() {
    return Collectors.of((Supplier<ImmutableList.Builder<T>>) LIST_BUILDER_SUPPLIER,
            new BiConsumer<ImmutableList.Builder<T>, T>() {
                @Override//from   w ww  .java  2  s.  c o m
                public void accept(ImmutableList.Builder<T> b, T t) {
                    b.add(t);
                }
            }, new BinaryOperator<ImmutableList.Builder<T>>() {
                @Override
                public ImmutableList.Builder<T> apply(ImmutableList.Builder<T> builder1,
                        ImmutableList.Builder<T> builder2) {
                    return builder1.addAll(builder2.build());
                }
            }, new Function<ImmutableList.Builder<T>, ImmutableList<T>>() {
                @Override
                public ImmutableList<T> apply(ImmutableList.Builder<T> builder) {
                    return builder.build();
                }
            });
}

From source file:org.caleydo.core.util.ExtensionUtils.java

/**
 * simple wrapper for creating all implementation instances of a given extension point
 *
 * @param extensionPoint//from  w  ww. java 2s  .c  o  m
 * @param property
 *            the property which holds the class name, e.g. class
 * @param type
 *            the expected class type
 * @return
 */
public static <T> List<T> findImplementation(String extensionPoint, String property, Class<T> type) {
    if (noRegistry())
        return ImmutableList.of();
    Builder<T> factories = ImmutableList.builder();
    try {

        for (IConfigurationElement elem : RegistryFactory.getRegistry()
                .getConfigurationElementsFor(extensionPoint)) {
            final Object o = elem.createExecutableExtension(property);
            if (type.isInstance(o))
                factories.add(type.cast(o));
        }
    } catch (CoreException e) {
        log.error("can't find implementations of " + extensionPoint + " : " + property, e);
    }
    return factories.build();
}

From source file:com.spotify.heroic.suggest.WriteSuggest.java

public static Collector<WriteSuggest, WriteSuggest> reduce() {
    return requests -> {
        final ImmutableList.Builder<RequestError> errors = ImmutableList.builder();
        final ImmutableList.Builder<Long> times = ImmutableList.builder();

        for (final WriteSuggest r : requests) {
            errors.addAll(r.getErrors());
            times.addAll(r.getTimes());/*from w  w w.  ja  va 2s  .c o  m*/
        }

        return new WriteSuggest(errors.build(), times.build(), ImmutableList.of());
    };
}

From source file:fr.inria.eventcloud.utils.RDFReader.java

public static List<Quadruple> read(String uri, final boolean checkQuadrupleType,
        final boolean parseQuadrupleMetaInformation) {
    final Builder<Quadruple> quadruples = ImmutableList.builder();

    read(uri, new Callback<Quadruple>() {

        @Override/* w  w  w.j a v a 2  s  . c  o  m*/
        public void execute(Quadruple quadruple) {
            quadruples.add(quadruple);
        }

    }, checkQuadrupleType, parseQuadrupleMetaInformation);

    return quadruples.build();
}

From source file:com.google.testing.junit.runner.junit4.JUnit4Options.java

/**
 * Parses the given array of arguments and returns a JUnit4Options
 * object representing the parsed arguments.
 *///w w w  .j a v  a2s  .  c o  m
static JUnit4Options parse(Map<String, String> envVars, List<String> args) {
    ImmutableList.Builder<String> unparsedArgsBuilder = ImmutableList.builder();
    Map<String, String> optionsMap = Maps.newHashMap();

    optionsMap.put(TEST_INCLUDE_FILTER_OPTION, null);
    optionsMap.put(TEST_EXCLUDE_FILTER_OPTION, null);

    for (Iterator<String> it = args.iterator(); it.hasNext();) {
        String arg = it.next();
        int indexOfEquals = arg.indexOf("=");

        if (indexOfEquals > 0) {
            String optionName = arg.substring(0, indexOfEquals);
            if (optionsMap.containsKey(optionName)) {
                optionsMap.put(optionName, arg.substring(indexOfEquals + 1));
                continue;
            }
        } else if (optionsMap.containsKey(arg)) {
            // next argument is the regexp
            if (!it.hasNext()) {
                throw new RuntimeException("No filter expression specified after " + arg);
            }
            optionsMap.put(arg, it.next());
            continue;
        }
        unparsedArgsBuilder.add(arg);
    }
    // If TESTBRIDGE_TEST_ONLY is set in the environment, forward it to the
    // --test_filter flag.
    String testFilter = envVars.get(TESTBRIDGE_TEST_ONLY);
    if (testFilter != null && optionsMap.get(TEST_INCLUDE_FILTER_OPTION) == null) {
        optionsMap.put(TEST_INCLUDE_FILTER_OPTION, testFilter);
    }

    ImmutableList<String> unparsedArgs = unparsedArgsBuilder.build();
    return new JUnit4Options(optionsMap.get(TEST_INCLUDE_FILTER_OPTION),
            optionsMap.get(TEST_EXCLUDE_FILTER_OPTION), unparsedArgs.toArray(new String[unparsedArgs.size()]));
}

From source file:org.prebake.channel.Commands.java

public static Commands fromJson(Path clientRoot, JsonSource src, @Nullable OutputStream response)
        throws IOException {
    ImmutableList.Builder<Command> cmds = ImmutableList.builder();
    src.expect("[");
    if (!src.check("]")) {
        do {/*w w w  .  j  ava  2  s  .c om*/
            cmds.add(Command.fromJson(src, clientRoot));
        } while (src.check(","));
        src.expect("]");
    }
    return new Commands(cmds.build(), response);
}

From source file:net.techcable.pineapple.collect.ImmutableLists.java

@Nonnull
public static <T, U> ImmutableList<U> transform(List<T> list, Function<T, U> transformer) {
    ImmutableList.Builder<U> resultBuilder = builder(checkNotNull(list, "Null list").size());
    list.forEach((oldElement) -> {//from  w  ww .  j  a va 2 s .co m
        U newElement = checkNotNull(transformer, "Null transformer").apply(oldElement);
        if (newElement == null)
            throw new NullPointerException(
                    "Transformer  " + transformer.getClass().getTypeName() + " returned null.");
        resultBuilder.add(newElement);
    });
    return resultBuilder.build();
}

From source file:com.facebook.presto.sql.relational.Expressions.java

public static List<RowExpression> subExpressions(Iterable<RowExpression> expressions) {
    final ImmutableList.Builder<RowExpression> builder = ImmutableList.builder();

    for (RowExpression expression : expressions) {
        expression.accept(new RowExpressionVisitor<Void, Void>() {
            @Override// www. jav a2  s  . c o m
            public Void visitCall(CallExpression call, Void context) {
                builder.add(call);
                for (RowExpression argument : call.getArguments()) {
                    argument.accept(this, context);
                }
                return null;
            }

            @Override
            public Void visitInputReference(InputReferenceExpression reference, Void context) {
                builder.add(reference);
                return null;
            }

            @Override
            public Void visitConstant(ConstantExpression literal, Void context) {
                builder.add(literal);
                return null;
            }
        }, null);
    }

    return builder.build();
}

From source file:rx.transformer.GuavaTransformers.java

/**
 * Returns a Transformer&lt;T,ImmutableList&lt;T&gt;&gt that maps an Observable&lt;T&gt; to an Observable&lt;ImmutableList&lt;T&gt;&gt;
 *///from   ww  w.ja v  a  2  s .com
public static <T> Observable.Transformer<T, ImmutableList<T>> toImmutableList() {
    return new Observable.Transformer<T, ImmutableList<T>>() {
        @Override
        public Observable<ImmutableList<T>> call(Observable<T> source) {
            return source.collect(new Func0<ImmutableList.Builder<T>>() {
                @Override
                public ImmutableList.Builder<T> call() {
                    return ImmutableList.builder();
                }
            }, new Action2<ImmutableList.Builder<T>, T>() {
                @Override
                public void call(ImmutableList.Builder<T> builder, T t) {
                    builder.add(t);
                }
            }).map(new Func1<ImmutableList.Builder<T>, ImmutableList<T>>() {
                @Override
                public ImmutableList<T> call(ImmutableList.Builder<T> builder) {
                    return builder.build();
                }
            });
        }
    };
}

From source file:io.prestosql.sql.relational.Expressions.java

public static List<RowExpression> subExpressions(Iterable<RowExpression> expressions) {
    final ImmutableList.Builder<RowExpression> builder = ImmutableList.builder();

    for (RowExpression expression : expressions) {
        expression.accept(new RowExpressionVisitor<Void, Void>() {
            @Override// w w  w . j av a  2 s. com
            public Void visitCall(CallExpression call, Void context) {
                builder.add(call);
                for (RowExpression argument : call.getArguments()) {
                    argument.accept(this, context);
                }
                return null;
            }

            @Override
            public Void visitInputReference(InputReferenceExpression reference, Void context) {
                builder.add(reference);
                return null;
            }

            @Override
            public Void visitConstant(ConstantExpression literal, Void context) {
                builder.add(literal);
                return null;
            }

            @Override
            public Void visitLambda(LambdaDefinitionExpression lambda, Void context) {
                builder.add(lambda);
                lambda.getBody().accept(this, context);
                return null;
            }

            @Override
            public Void visitVariableReference(VariableReferenceExpression reference, Void context) {
                builder.add(reference);
                return null;
            }
        }, null);
    }

    return builder.build();
}