Example usage for com.google.common.collect ImmutableList.Builder add

List of usage examples for com.google.common.collect ImmutableList.Builder add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Appends the specified element to the end of this list (optional operation).

Usage

From source file:com.pingcap.tikv.predicates.AccessConditionNormalizer.java

static NormalizedCondition normalize(TiExpr expr) {
    if (!validate(expr)) {
        throw new TiClientInternalException("Not a valid access condition expression: " + expr);
    }/*from w  w  w .j a  va2  s  .c  o m*/

    if (expr instanceof TiFunctionExpression) {
        TiFunctionExpression func = (TiFunctionExpression) expr;
        func = normalizeComparision(func);

        if (func instanceof In) {
            ImmutableList.Builder<TiConstant> vals = ImmutableList.builder();
            for (int i = 1; i < func.getArgSize(); i++) {
                // Checked already
                vals.add((TiConstant) func.getArg(i));
            }
            return new NormalizedCondition((TiColumnRef) func.getArg(0), vals.build(), func);
        } else {
            return new NormalizedCondition((TiColumnRef) func.getArg(0),
                    ImmutableList.of((TiConstant) func.getArg(1)), func);
        }
    }
    throw new TiClientInternalException("Not a valid access condition expression: " + expr);
}

From source file:com.google.devtools.build.lib.rules.android.AndroidBuildViewTestCase.java

protected static void assertPrimaryResourceDirs(ConfiguredTarget target, List<String> expectedPaths,
        List<String> actualArgs) {
    assertThat(actualArgs).contains("--primaryData");

    String actualFlagValue = actualArgs.get(actualArgs.indexOf("--primaryData") + 1);
    if (target.getConfiguration().getFragment(AndroidConfiguration.class).useParallelResourceProcessing()) {
        assertThat(actualFlagValue).matches("[^;]*;[^;]*;.*");
        ImmutableList.Builder<String> actualPaths = ImmutableList.builder();
        actualPaths.add(actualFlagValue.split(";")[0].split("#"));
        Truth.assertThat(actualPaths.build()).containsAllIn(expectedPaths);
    } else {/*from w  ww  .ja  va 2s  . c o m*/
        assertThat(actualFlagValue).matches("[^:]*:[^:]*:.*");
        ImmutableList.Builder<String> actualPaths = ImmutableList.builder();
        actualPaths.add(actualFlagValue.split(":")[0].split("#"));
        Truth.assertThat(actualPaths.build()).containsAllIn(expectedPaths);
    }
}

From source file:io.prestosql.sql.QueryUtil.java

public static Select selectList(List<Expression> expressions) {
    ImmutableList.Builder<SelectItem> items = ImmutableList.builder();
    for (Expression expression : expressions) {
        items.add(new SingleColumn(expression));
    }/*from  w w  w  .j a  va  2s  . co  m*/
    return new Select(false, items.build());
}

From source file:io.prestosql.sql.gen.VarArgsToMapAdapterGenerator.java

/**
 * Generate byte code that/* www  .ja v a  2  s  .c o m*/
 * <p><ul>
 * <li>takes a specified number of variables as arguments (types of the arguments are provided in {@code javaTypes})
 * <li>put the variables in a map (keys of the map are provided in {@code names})
 * <li>invoke the provided {@code function} with the map
 * <li>return with the result of the function call (type must match {@code returnType})
 * </ul></p>
 */
public static MethodHandle generateVarArgsToMapAdapter(Class<?> returnType, List<Class<?>> javaTypes,
        List<String> names, Function<Map<String, Object>, Object> function) {
    checkCondition(javaTypes.size() <= 254, NOT_SUPPORTED, "Too many arguments for vararg function");
    CallSiteBinder callSiteBinder = new CallSiteBinder();

    ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL),
            makeClassName("VarArgsToMapAdapter"), type(Object.class));

    ImmutableList.Builder<Parameter> parameterListBuilder = ImmutableList.builder();
    for (int i = 0; i < javaTypes.size(); i++) {
        Class<?> javaType = javaTypes.get(i);
        parameterListBuilder.add(arg("input_" + i, javaType));
    }
    ImmutableList<Parameter> parameterList = parameterListBuilder.build();

    MethodDefinition methodDefinition = classDefinition.declareMethod(a(PUBLIC, STATIC), "varArgsToMap",
            type(returnType), parameterList);
    BytecodeBlock body = methodDefinition.getBody();

    // ImmutableMap.Builder can not be used here because it doesn't allow nulls.
    Variable map = methodDefinition.getScope().declareVariable("map", methodDefinition.getBody(), invokeStatic(
            Maps.class, "newHashMapWithExpectedSize", HashMap.class, constantInt(javaTypes.size())));
    for (int i = 0; i < javaTypes.size(); i++) {
        body.append(map.invoke("put", Object.class, constantString(names.get(i)).cast(Object.class),
                parameterList.get(i).cast(Object.class)));
    }
    body.append(loadConstant(callSiteBinder, function, Function.class)
            .invoke("apply", Object.class, map.cast(Object.class)).cast(returnType).ret());

    Class<?> generatedClass = defineClass(classDefinition, Object.class, callSiteBinder.getBindings(),
            new DynamicClassLoader(VarArgsToMapAdapterGenerator.class.getClassLoader()));
    return Reflection.methodHandle(generatedClass, "varArgsToMap",
            javaTypes.toArray(new Class<?>[javaTypes.size()]));
}

From source file:io.prometheus.client.metrics.histogram.buckets.Distributions.java

public static List<Float> logarithmicSizedBucketsFor(final float lower, final float upper) {
    final ImmutableList.Builder<Float> builder = ImmutableList.builder();
    final int bucketCount = (int) Math.ceil(Math.log(upper) / Math.log(2));

    for (int i = 0; i < bucketCount; i++) {
        final float j = (float) Math.pow(2, i + 1);
        builder.add(j);
    }/*from   w w w . j a  v  a  2s. c  om*/

    return builder.build();
}

From source file:net.hydromatic.optiq.impl.ReflectiveFunctionBase.java

public static ImmutableList<FunctionParameter> toFunctionParameters(Iterable<? extends Class> types) {
    final ImmutableList.Builder<FunctionParameter> res = ImmutableList.builder();
    int i = 0;// w ww  . j ava2s .  c  o m
    for (final Class type : types) {
        final int ordinal = i;
        res.add(new FunctionParameter() {
            public int getOrdinal() {
                return ordinal;
            }

            public String getName() {
                return "arg" + ordinal;
            }

            public RelDataType getType(RelDataTypeFactory typeFactory) {
                return typeFactory.createJavaType(type);
            }
        });
        i++;
    }
    return res.build();
}

From source file:com.github.rinde.logistics.pdptw.solver.CheapestInsertionHeuristic.java

static ImmutableList<Double> decomposedCost(GlobalStateObject state,
        ImmutableList<ImmutableList<Parcel>> schedule, ObjectiveFunction objFunc) {
    final ImmutableList.Builder<Double> builder = ImmutableList.builder();
    for (int i = 0; i < schedule.size(); i++) {
        builder.add(objFunc.computeCost(
                Solvers.computeStats(state.withSingleVehicle(i), ImmutableList.of(schedule.get(i)))));
    }//  w  ww  .j a v a2s.  c o m
    return builder.build();
}

From source file:com.twitter.distributedlog.client.serverset.DLZkServerSet.java

private static Iterable<InetSocketAddress> getZkAddresses(URI uri) {
    String zkServers = getZKServersFromDLUri(uri);
    String[] zkServerList = StringUtils.split(zkServers, ',');
    ImmutableList.Builder<InetSocketAddress> builder = ImmutableList.builder();
    for (String zkServer : zkServerList) {
        HostAndPort hostAndPort = HostAndPort.fromString(zkServer).withDefaultPort(2181);
        builder.add(InetSocketAddress.createUnresolved(hostAndPort.getHostText(), hostAndPort.getPort()));
    }/* w  ww.java 2 s  . c  o  m*/
    return builder.build();
}

From source file:com.palantir.stash.disapprove.persistence.DisapprovalMode.java

/**
 * Helper method for populating a dropdown option box with metadata
 * //from www . j a va 2s  .  co  m
 * @param selected
 * @return
 */
public static ImmutableList<ImmutableMap<String, String>> getSelectList(DisapprovalMode selected) {
    ImmutableList.Builder<ImmutableMap<String, String>> builder = ImmutableList.builder();
    for (DisapprovalMode ae : DisapprovalMode.values()) {
        if (selected != null && selected.equals(ae)) {
            builder.add(ae.getSelectListEntry(true));
        } else {
            builder.add(ae.getSelectListEntry(false));
        }
    }
    return builder.build();
}

From source file:io.leishvl.core.util.LocaleUtils.java

/**
 * Obtains an unmodifiable list of installed locales. This method is a wrapper around
 * {@link Locale#getAvailableLocales()}, which caches the locales and uses generics.
 * @return country names.// w  w  w . ja v  a2 s.  c o m
 */
public static ImmutableList<Locale> availableLocaleList() {
    if (availableLocaleList == null) {
        synchronized (LocaleUtils.class) {
            if (availableLocaleList == null) {
                final ImmutableList.Builder<Locale> builder = new ImmutableList.Builder<>();
                final String[] locales = getISOCountries();
                for (final String countryCode : locales) {
                    builder.add(new Locale("", countryCode));
                }
                availableLocaleList = builder.build();
            }
        }
    }
    return availableLocaleList;
}