Example usage for com.google.common.collect ImmutableMap.Builder put

List of usage examples for com.google.common.collect ImmutableMap.Builder put

Introduction

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

Prototype

public final V put(K k, V v) 

Source Link

Usage

From source file:org.apache.shindig.protocol.conversion.BeanDelegator.java

/**
 * Utility function to auto generate mapping between two enums that have same values (toString)
 * All values in the sourceEnum must have values in targetEnum,
 *  otherwise {@link RuntimeException} is thrown
 *///from w w  w .  j  a  v  a 2 s  . c om
public static Map<Enum<?>, Enum<?>> createDefaultEnumMap(Class<? extends Enum<?>> sourceEnum,
        Class<? extends Enum<?>> targetEnum) {
    Map<String, Enum<?>> values2Map = Maps.newHashMap();
    for (Object val2 : targetEnum.getEnumConstants()) {
        values2Map.put(val2.toString(), (Enum<?>) val2);
    }
    Enum<?>[] values1 = sourceEnum.getEnumConstants();
    ImmutableMap.Builder<Enum<?>, Enum<?>> mapBuilder = ImmutableMap.builder();
    for (Enum<?> val1 : sourceEnum.getEnumConstants()) {
        if (values2Map.containsKey(val1.toString())) {
            mapBuilder.put(val1, values2Map.get(val1.toString()));
        } else {
            throw new RuntimeException(
                    "Missing enum value " + val1.toString() + " for enum " + targetEnum.getName());
        }
    }
    return mapBuilder.build();
}

From source file:com.google.devtools.build.lib.runtime.commands.HelpCommand.java

/**
 * Returns a map that maps option categories to descriptive help strings for categories that
 * are not part of the Bazel core./*w w w .  j  a  v  a2  s  . c  o m*/
 */
private static ImmutableMap<String, String> getOptionCategories(BlazeRuntime runtime) {
    ImmutableMap.Builder<String, String> optionCategoriesBuilder = ImmutableMap.builder();
    String name = runtime.getProductName();
    optionCategoriesBuilder
            .put("checking",
                    String.format("Checking options, which control %s's error checking and/or warnings", name))
            .put("coverage",
                    String.format("Options that affect how %s generates code coverage information", name))
            .put("experimental",
                    "Experimental options, which control experimental (and potentially risky) features")
            .put("flags", "Flags options, for passing options to other tools").put("help", "Help options")
            .put("host jvm startup",
                    String.format("Options that affect the startup of the %s server's JVM", name))
            .put("misc", "Miscellaneous options")
            .put("package loading", "Options that specify how to locate packages")
            .put("query", String.format("Options affecting the '%s query' dependency query command", name))
            .put("run", String.format("Options specific to '%s run'", name))
            .put("semantics", "Semantics options, which affect the build commands and/or output file contents")
            .put("server startup",
                    String.format("Startup options, which affect the startup of the %s server", name))
            .put("strategy",
                    String.format("Strategy options, which affect how %s will execute the build", name))
            .put("testing", String.format("Options that affect how %s runs tests", name))
            .put("verbosity", String.format("Verbosity options, which control what %s prints", name))
            .put("version", "Version options, for selecting which version of other tools will be used")
            .put("what", "Output selection options, for determining what to build/test");
    return optionCategoriesBuilder.build();
}

From source file:com.google.devtools.build.lib.packages.WorkspaceFactory.java

private static ImmutableMap<String, BaseFunction> createWorkspaceFunctions(boolean allowOverride,
        RuleFactory ruleFactory) {/* w ww  .  j av a 2 s.  c o m*/
    ImmutableMap.Builder<String, BaseFunction> mapBuilder = ImmutableMap.builder();
    mapBuilder.put(BIND, newBindFunction(ruleFactory));
    for (String ruleClass : ruleFactory.getRuleClassNames()) {
        if (!ruleClass.equals(BIND)) {
            BaseFunction ruleFunction = newRuleFunction(ruleFactory, ruleClass, allowOverride);
            mapBuilder.put(ruleClass, ruleFunction);
        }
    }
    return mapBuilder.build();
}

From source file:com.google.firebase.messaging.WebpushNotification.java

private static void addNonNullNonEmpty(ImmutableMap.Builder<String, Object> fields, String key, String value) {
    if (!Strings.isNullOrEmpty(value)) {
        fields.put(key, value);
    }/*from   w w  w  .  j  a v  a2 s  . c o m*/
}

From source file:com.eucalyptus.compute.common.internal.tags.Filters.java

private static Map<String, Set<String>> toMap(final Iterable<com.eucalyptus.compute.common.Filter> filters) {
    final ImmutableMap.Builder<String, Set<String>> filterMapBuilder = ImmutableMap.builder();

    for (final com.eucalyptus.compute.common.Filter filter : filters) {
        final Set<String> values = ImmutableSet
                .copyOf(Iterables.transform(filter.getValueSet(), NullToEmptyString.INSTANCE));
        filterMapBuilder.put(filter.getName(), values);
    }/*from w  ww  . j a va 2s  . co m*/

    return filterMapBuilder.build();
}

From source file:com.querydsl.core.types.QBean.java

private static ImmutableMap<String, Expression<?>> createBindings(Expression<?>... args) {
    ImmutableMap.Builder<String, Expression<?>> rv = ImmutableMap.builder();
    for (Expression<?> expr : args) {
        if (expr instanceof Path<?>) {
            Path<?> path = (Path<?>) expr;
            rv.put(path.getMetadata().getName(), expr);
        } else if (expr instanceof Operation<?>) {
            Operation<?> operation = (Operation<?>) expr;
            if (operation.getOperator() == Ops.ALIAS && operation.getArg(1) instanceof Path<?>) {
                Path<?> path = (Path<?>) operation.getArg(1);
                if (isCompoundExpression(operation.getArg(0))) {
                    rv.put(path.getMetadata().getName(), operation.getArg(0));
                } else {
                    rv.put(path.getMetadata().getName(), operation);
                }/*from  w  ww . j a va  2 s.co  m*/
            } else {
                throw new IllegalArgumentException("Unsupported expression " + expr);
            }

        } else {
            throw new IllegalArgumentException("Unsupported expression " + expr);
        }
    }
    return rv.build();
}

From source file:org.tensorics.core.tensor.operations.OngoingMapOut.java

private static <C, C1 extends C, T> Map<C1, T> mapByDimension(Collection<Entry<Position, T>> entries,
        Class<? extends C1> dimension) {
    ImmutableMap.Builder<C1, T> valuesBuilder = ImmutableMap.builder();
    for (Entry<Position, T> entry : entries) {
        C1 coordinateFor = entry.getKey().coordinateFor(dimension);
        if (coordinateFor == null) {
            throw new IllegalArgumentException(
                    "Cannot operate with [" + dimension + "] while having only " + entry.getKey());
        }/*www  .  jav  a2  s  . c  o m*/
        valuesBuilder.put(coordinateFor, entry.getValue());
    }
    return valuesBuilder.build();
}

From source file:org.zanata.model.tm.TMXMetadataHelper.java

/**
 * Gets all the entity's metadata in a single Map.
 *
 * @param tu//from w w  w  .ja v a2s. com
 * @return
 */
@Nonnull
public static ImmutableMap<String, String> getAttributes(TransMemoryUnitVariant fromTuv) {
    ImmutableMap.Builder<String, String> m = ImmutableMap.builder();
    m.putAll(getSharedMetadata(fromTuv));
    String lang = fromTuv.getLanguage();
    assert lang != null;
    m.put(XML_LANG, lang);
    return m.build();
}

From source file:com.github.christofluyten.experiment.ResultWriter.java

static void addSimOutputs(ImmutableMap.Builder<Enum<?>, Object> map, SimulationResult sr,
        Gendreau06ObjectiveFunction objFunc) {
    if (sr.getResultObject() instanceof PostProcessor.FailureStrategy) {
        map.put(OutputFields.COST, -1).put(OutputFields.TRAVEL_TIME, -1).put(OutputFields.TARDINESS, -1)
                .put(OutputFields.OVER_TIME, -1).put(OutputFields.IS_VALID, false)
                .put(OutputFields.COMP_TIME, -1).put(OutputFields.NUM_REAUCTIONS, -1)
                .put(OutputFields.NUM_UNSUC_REAUCTIONS, -1).put(OutputFields.NUM_FAILED_REAUCTIONS, -1);
    } else {//w  ww .  jav a2s .co  m
        final NycExperiment.ExperimentInfo ei = (NycExperiment.ExperimentInfo) sr.getResultObject();
        final StatisticsDTO stats = ei.getStats();
        map.put(OutputFields.COST, objFunc.computeCost(stats))
                .put(OutputFields.TRAVEL_TIME, objFunc.travelTime(stats))
                .put(OutputFields.TRAVEL_DISTANCE, stats.totalDistance)
                .put(OutputFields.PICKUP_TARDINESS, objFunc.pickupTardiness(stats))
                .put(OutputFields.DELIVERY_TARDINESS, objFunc.deliveryTardiness(stats))
                .put(OutputFields.TARDINESS, objFunc.tardiness(stats))
                .put(OutputFields.OVER_TIME, objFunc.overTime(stats))
                .put(OutputFields.IS_VALID, objFunc.isValidResult(stats))
                .put(OutputFields.COMP_TIME, stats.computationTime)
                .put(OutputFields.NUM_VEHICLES, stats.totalVehicles)
                .put(OutputFields.MOVED_VEHICLES, stats.movedVehicles)
                .put(OutputFields.NUM_ORDERS, stats.totalParcels)
                .put(OutputFields.NUM_ACCEPTED_ORDERS, stats.acceptedParcels)
                .put(OutputFields.NUM_DELIVERED_ORDERS, stats.totalDeliveries);

        if (ei.getAuctionStats().isPresent()) {
            final NycExperiment.AuctionStats aStats = ei.getAuctionStats().get();
            map.put(OutputFields.NUM_REAUCTIONS, aStats.getNumReauctions())
                    .put(OutputFields.NUM_UNSUC_REAUCTIONS, aStats.getNumUnsuccesfulReauctions())
                    .put(OutputFields.NUM_FAILED_REAUCTIONS, aStats.getNumFailedReauctions());
        } else {
            map.put(OutputFields.NUM_REAUCTIONS, 0).put(OutputFields.NUM_UNSUC_REAUCTIONS, 0)
                    .put(OutputFields.NUM_FAILED_REAUCTIONS, 0);
        }

        if (!objFunc.isValidResult(stats)) {
            System.err.println("WARNING: FOUND AN INVALID RESULT: ");
            System.err.println(map.build());
        }
    }
}

From source file:com.palantir.typescript.TypeScriptBuilder.java

private static Map<String, Object> createProblemMarkerAttributes(DiagnosticEx diagnostic) {
    ImmutableMap.Builder<String, Object> attributes = ImmutableMap.builder();

    attributes.put(IMarker.CHAR_START, diagnostic.getStart());
    attributes.put(IMarker.CHAR_END, diagnostic.getStart() + diagnostic.getLength());
    attributes.put(IMarker.LINE_NUMBER, diagnostic.getLine());
    attributes.put(IMarker.MESSAGE, diagnostic.getText());
    attributes.put(IMarker.PRIORITY, IMarker.PRIORITY_NORMAL);
    attributes.put(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);

    return attributes.build();
}