Example usage for com.google.common.collect ImmutableSortedMap naturalOrder

List of usage examples for com.google.common.collect ImmutableSortedMap naturalOrder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSortedMap naturalOrder.

Prototype

public static <K extends Comparable<?>, V> Builder<K, V> naturalOrder() 

Source Link

Usage

From source file:org.apache.calcite.sql.test.SqlTestFactory.java

public SqlTestFactory with(String name, Object value) {
    if (Objects.equals(value, options.get(name))) {
        return this;
    }/* ww  w  .j  a  va  2  s . c  o m*/
    ImmutableMap.Builder<String, Object> builder = ImmutableSortedMap.naturalOrder();
    // Protect from IllegalArgumentException: Multiple entries with same key
    for (Map.Entry<String, Object> entry : options.entrySet()) {
        if (name.equals(entry.getKey())) {
            continue;
        }
        builder.put(entry);
    }
    builder.put(name, value);
    return new SqlTestFactory(builder.build(), catalogReaderFactory, validatorFactory);
}

From source file:org.onosproject.onosjar.OnosJarDescription.java

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params,
        BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {

    SourcePathResolver pathResolver = new SourcePathResolver(resolver);
    BuildTarget target = params.getBuildTarget();

    // We know that the flavour we're being asked to create is valid, since the check is done when
    // creating the action graph from the target graph.

    ImmutableSortedSet<Flavor> flavors = target.getFlavors();
    BuildRuleParams paramsWithMavenFlavor = null;

    if (flavors.contains(JavaLibrary.MAVEN_JAR)) {
        paramsWithMavenFlavor = params;/*from   www . j  a v  a2 s  . co  m*/

        // Maven rules will depend upon their vanilla versions, so the latter have to be constructed
        // without the maven flavor to prevent output-path conflict
        params = params.copyWithBuildTarget(
                params.getBuildTarget().withoutFlavors(ImmutableSet.of(JavaLibrary.MAVEN_JAR)));
    }

    if (flavors.contains(JavaLibrary.SRC_JAR)) {
        args.mavenCoords = args.mavenCoords.transform(new Function<String, String>() {
            @Override
            public String apply(String input) {
                return AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_SOURCES);
            }
        });

        if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
            return new JavaSourceJar(params, pathResolver, args.srcs.get(), args.mavenCoords);
        } else {
            return MavenUberJar.SourceJar.create(Preconditions.checkNotNull(paramsWithMavenFlavor),
                    pathResolver, args.srcs.get(), args.mavenCoords, Optional.absent()); //FIXME
        }
    }

    if (flavors.contains(JavaLibrary.JAVADOC_JAR)) {
        args.mavenCoords = args.mavenCoords.transform(new Function<String, String>() {
            @Override
            public String apply(String input) {
                return AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_JAVADOC);
            }
        });

        JavadocJar.JavadocArgs.Builder javadocArgs = JavadocJar.JavadocArgs.builder()
                .addArg("-windowtitle", target.getShortName())
                .addArg("-link", "http://docs.oracle.com/javase/8/docs/api")
                .addArg("-tag", "onos.rsModel:a:\"onos model\""); //FIXME from buckconfig + rule

        final ImmutableSortedMap.Builder<SourcePath, Path> javadocFiles = ImmutableSortedMap.naturalOrder();
        if (args.javadocFiles.isPresent()) {
            for (SourcePath path : args.javadocFiles.get()) {
                javadocFiles.put(path,
                        JavadocJar.getDocfileWithPath(pathResolver, path, args.javadocFilesRoot.orNull()));
            }
        }

        if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
            return new JavadocJar(params, pathResolver, args.srcs.get(), javadocFiles.build(),
                    javadocArgs.build(), args.mavenCoords);
        } else {
            return MavenUberJar.MavenJavadocJar.create(Preconditions.checkNotNull(paramsWithMavenFlavor),
                    pathResolver, args.srcs.get(), javadocFiles.build(), javadocArgs.build(), args.mavenCoords,
                    Optional.absent()); //FIXME
        }
    }

    JavacOptions javacOptions = JavacOptionsFactory.create(defaultJavacOptions, params, resolver, pathResolver,
            args);

    BuildTarget abiJarTarget = params.getBuildTarget().withAppendedFlavors(CalculateAbi.FLAVOR);

    ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps.get());

    // Build the resources string
    List<String> resourceMappings = Lists.newArrayList();

    if (args.includeResources.isPresent()) {
        args.includeResources.get().entrySet()
                .forEach(p -> resourceMappings.add(String.format("%s=%s", p.getKey(), p.getValue())));
    }

    if (args.apiTitle.isPresent()) {
        resourceMappings.add("WEB-INF/classes/apidoc/swagger.json=swagger.json");
    }

    Optional<String> includedResourcesString = Optional.of(String.join(",", resourceMappings));
    final DefaultJavaLibrary defaultJavaLibrary;
    if (!flavors.contains(NON_OSGI_JAR)) {
        defaultJavaLibrary = resolver.addToIndex(new OnosJar(
                params.appendExtraDeps(Iterables.concat(
                        BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(),
                                exportedDeps, resolver.getAllRules(args.providedDeps.get()))),
                        pathResolver.filterBuildRuleInputs(javacOptions.getInputs(pathResolver)))),
                pathResolver, args.srcs.get(),
                validateResources(pathResolver, params.getProjectFilesystem(), args.resources.get()),
                javacOptions.getGeneratedSourceFolderName(),
                args.proguardConfig.transform(SourcePaths.toSourcePath(params.getProjectFilesystem())),
                args.postprocessClassesCommands.get(), // FIXME this should be forbidden
                exportedDeps, resolver.getAllRules(args.providedDeps.get()),
                new BuildTargetSourcePath(abiJarTarget), javacOptions.trackClassUsage(),
                /* additionalClasspathEntries */ ImmutableSet.<Path>of(),
                new OnosJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY, args.webContext,
                        args.apiTitle, args.apiVersion, args.apiPackage, args.apiDescription, args.resources,
                        args.groupId, args.bundleName, args.bundleVersion, args.bundleLicense,
                        args.bundleDescription, args.importPackages, args.exportPackages,
                        includedResourcesString, args.dynamicimportPackages),
                args.resourcesRoot, args.manifestFile, args.mavenCoords, args.tests.get(),
                javacOptions.getClassesToRemoveFromJar(), args.webContext, args.apiTitle, args.apiVersion,
                args.apiPackage, args.apiDescription, args.includeResources));
    } else {
        defaultJavaLibrary = resolver.addToIndex(new DefaultJavaLibrary(
                params.appendExtraDeps(Iterables.concat(
                        BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(),
                                exportedDeps, resolver.getAllRules(args.providedDeps.get()))),
                        pathResolver.filterBuildRuleInputs(javacOptions.getInputs(pathResolver)))),
                pathResolver, args.srcs.get(),
                validateResources(pathResolver, params.getProjectFilesystem(), args.resources.get()),
                javacOptions.getGeneratedSourceFolderName(),
                args.proguardConfig.transform(SourcePaths.toSourcePath(params.getProjectFilesystem())),
                args.postprocessClassesCommands.get(), exportedDeps,
                resolver.getAllRules(args.providedDeps.get()), new BuildTargetSourcePath(abiJarTarget),
                javacOptions.trackClassUsage(), /* additionalClasspathEntries */ ImmutableSet.<Path>of(),
                new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY), args.resourcesRoot,
                args.manifestFile, args.mavenCoords, args.tests.get(),
                javacOptions.getClassesToRemoveFromJar()));
    }

    resolver.addToIndex(CalculateAbi.of(abiJarTarget, pathResolver, params,
            new BuildTargetSourcePath(defaultJavaLibrary.getBuildTarget())));

    return defaultJavaLibrary;
}

From source file:com.moz.fiji.schema.impl.MaterializedFijiResult.java

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override/*  ww  w  . j  a va2 s .c  om*/
public <U extends T> FijiResult<U> narrowView(final FijiColumnName column) {
    final FijiDataRequest narrowRequest = DefaultFijiResult.narrowRequest(column, mDataRequest);
    if (narrowRequest.equals(mDataRequest)) {
        return (FijiResult<U>) this;
    }

    final ImmutableSortedMap.Builder<FijiColumnName, List<FijiCell<U>>> narrowedColumns = ImmutableSortedMap
            .naturalOrder();

    for (Column columnRequest : narrowRequest.getColumns()) {
        final FijiColumnName requestedColumn = columnRequest.getColumnName();

        // (Object) cast is necessary. Might be: http://bugs.java.com/view_bug.do?bug_id=6548436
        final List<FijiCell<U>> exactColumn = (List<FijiCell<U>>) (Object) mColumns.get(requestedColumn);
        if (exactColumn != null) {

            // We get here IF

            // `column` is a family, and `mDataRequest` contains a column request for the entire family.

            // OR

            // `column` is a family, and `mDataRequest` contains a column request for a qualified column
            // in the family.

            // OR

            // `column` is a qualified-column, and `mDataRequest` contains a request for the qualified
            // column.

            narrowedColumns.put(requestedColumn, exactColumn);
        } else {

            // `column` is a qualified-column, and `mDataRequest` contains a column request for the
            // column's family.

            final List<FijiCell<T>> familyCells = mColumns
                    .get(FijiColumnName.create(requestedColumn.getFamily(), null));
            final List<FijiCell<T>> qualifierCells = getQualifierCells(requestedColumn, familyCells);
            narrowedColumns.put(requestedColumn, (List<FijiCell<U>>) (Object) qualifierCells);
        }
    }

    return new MaterializedFijiResult<U>(mEntityId, narrowRequest, narrowedColumns.build());
}

From source file:org.kiji.schema.impl.hbase.HBaseMaterializedKijiResult.java

/** {@inheritDoc} */
@Override//from   w ww.j  a  v  a  2 s .c  om
@SuppressWarnings("unchecked")
public <U extends T> HBaseMaterializedKijiResult<U> narrowView(final KijiColumnName column) {
    final KijiDataRequest narrowRequest = DefaultKijiResult.narrowRequest(column, mDataRequest);
    if (narrowRequest.equals(mDataRequest)) {
        return (HBaseMaterializedKijiResult<U>) this;
    }

    final ImmutableSortedMap.Builder<KijiColumnName, List<KeyValue>> narrowedResults = ImmutableSortedMap
            .naturalOrder();

    for (Column columnRequest : narrowRequest.getColumns()) {
        final KijiColumnName requestColumnName = columnRequest.getColumnName();

        // We get here IF

        // `column` is a family, and `mDataRequest` contains a column request for the entire family.

        // OR

        // `column` is a family, and `mDataRequest` contains a column request for a qualified column
        // in the family.

        // OR

        // `column` is a qualified-column, and `mDataRequest` contains a request for the qualified
        // column.

        final List<KeyValue> exactColumn = mColumnResults.get(requestColumnName);
        if (exactColumn != null) {
            narrowedResults.put(requestColumnName, exactColumn);
        } else {

            // `column` is a qualified-column, and `mDataRequest` contains a column request for the
            // column's family.

            final List<KeyValue> familyResults = mColumnResults
                    .get(KijiColumnName.create(requestColumnName.getFamily(), null));
            final List<KeyValue> qualifiedColumnResults = getQualifiedColumnKeyValues(columnRequest,
                    mColumnTranslator, familyResults);

            narrowedResults.put(requestColumnName, qualifiedColumnResults);
        }
    }

    return new HBaseMaterializedKijiResult<U>(mEntityId, mDataRequest, mLayout, mColumnTranslator,
            mDecoderProvider, narrowedResults.build());
}

From source file:com.moz.fiji.schema.impl.hbase.HBaseMaterializedFijiResult.java

/** {@inheritDoc} */
@Override//from w ww  .  j  a va2  s  .  c o m
@SuppressWarnings("unchecked")
public <U extends T> HBaseMaterializedFijiResult<U> narrowView(final FijiColumnName column) {
    final FijiDataRequest narrowRequest = DefaultFijiResult.narrowRequest(column, mDataRequest);
    if (narrowRequest.equals(mDataRequest)) {
        return (HBaseMaterializedFijiResult<U>) this;
    }

    final ImmutableSortedMap.Builder<FijiColumnName, List<KeyValue>> narrowedResults = ImmutableSortedMap
            .naturalOrder();

    for (Column columnRequest : narrowRequest.getColumns()) {
        final FijiColumnName requestColumnName = columnRequest.getColumnName();

        // We get here IF

        // `column` is a family, and `mDataRequest` contains a column request for the entire family.

        // OR

        // `column` is a family, and `mDataRequest` contains a column request for a qualified column
        // in the family.

        // OR

        // `column` is a qualified-column, and `mDataRequest` contains a request for the qualified
        // column.

        final List<KeyValue> exactColumn = mColumnResults.get(requestColumnName);
        if (exactColumn != null) {
            narrowedResults.put(requestColumnName, exactColumn);
        } else {

            // `column` is a qualified-column, and `mDataRequest` contains a column request for the
            // column's family.

            final List<KeyValue> familyResults = mColumnResults
                    .get(FijiColumnName.create(requestColumnName.getFamily(), null));
            final List<KeyValue> qualifiedColumnResults = getQualifiedColumnKeyValues(columnRequest,
                    mColumnTranslator, familyResults);

            narrowedResults.put(requestColumnName, qualifiedColumnResults);
        }
    }

    return new HBaseMaterializedFijiResult<U>(mEntityId, mDataRequest, mLayout, mColumnTranslator,
            mDecoderProvider, narrowedResults.build());
}

From source file:se.sics.caracaldb.global.DefaultPolicy.java

private ImmutableSortedMap<Integer, Address> getIdsForJoins(LUTWorkingBuffer lut, ImmutableSet<Address> joins,
        ImmutableSortedMap<Integer, Address> failIds) {
    TreeSet<Address> remaining = new TreeSet<Address>(joins);
    ImmutableSortedMap.Builder<Integer, Address> ids = ImmutableSortedMap.naturalOrder();
    TreeSet<Integer> usedIds = new TreeSet<Integer>();
    // If nodes fail and rejoind try to assign the same id
    for (Entry<Integer, Address> e : failIds.entrySet()) {
        if (remaining.isEmpty()) {
            return ids.build();
        }/*www.  j av a2s.c  om*/
        if (remaining.contains(e.getValue())) {
            ids.put(e);
            remaining.remove(e.getValue());
            usedIds.add(e.getKey());
        }
    }
    // Assign all the other failed ids to new nodes
    for (Entry<Integer, Address> e : failIds.entrySet()) {
        if (remaining.isEmpty()) {
            return ids.build();
        }
        if (!usedIds.contains(e.getKey())) {
            Address j = remaining.pollFirst();
            ids.put(e.getKey(), j);
            usedIds.add(e.getKey());
        }
    }
    // Look for other empty slots in the LUT
    int index = 0;
    for (Address addr : lut.hosts()) {
        if (remaining.isEmpty()) {
            return ids.build();
        }
        if (addr == null) {
            if (usedIds.contains(index)) {
                LOG.warn("The node at index {} apparently failed twice. This is weird -.-");
                index++;
                continue;
            }
            Address j = remaining.pollFirst();
            ids.put(index, j);
            usedIds.add(index);
        }
        index++;
    }
    return ids.build();
}

From source file:com.facebook.buck.features.haskell.HaskellGhciRule.java

private ImmutableSortedMap<String, NonHashableSourcePathContainer> solibsForRuleKey(
        ImmutableSortedMap<String, SourcePath> libs) {
    ImmutableSortedMap.Builder<String, NonHashableSourcePathContainer> solibMap = ImmutableSortedMap
            .naturalOrder();//from   ww  w  . j a  v  a2s .c  om
    for (Map.Entry<String, SourcePath> entry : libs.entrySet()) {
        solibMap.put(entry.getKey(), new NonHashableSourcePathContainer(entry.getValue()));
    }

    return solibMap.build();
}

From source file:org.gradle.api.internal.changedetection.state.LazyTaskExecution.java

private ImmutableSortedMap<String, Long> storeSnapshot(Map<String, FileCollectionSnapshot> snapshot) {
    ImmutableSortedMap.Builder<String, Long> builder = ImmutableSortedMap.naturalOrder();
    for (Map.Entry<String, FileCollectionSnapshot> entry : snapshot.entrySet()) {
        String propertyName = entry.getKey();
        FileCollectionSnapshot fileCollectionSnapshot = entry.getValue();
        Long snapshotId = snapshotRepository.add(fileCollectionSnapshot);
        builder.put(propertyName, snapshotId);
    }/*from www.  j  a  v  a  2 s  .  co m*/
    return builder.build();
}

From source file:com.facebook.buck.util.config.Config.java

private HashCode computeOrderIndependentHashCode() {
    ImmutableMap<String, ImmutableMap<String, String>> rawValues = rawConfig.getValues();
    ImmutableSortedMap.Builder<String, ImmutableSortedMap<String, String>> expanded = ImmutableSortedMap
            .naturalOrder();//ww w  .  ja va 2  s.  co  m
    for (String section : rawValues.keySet()) {
        expanded.put(section, ImmutableSortedMap.copyOf(get(section)));
    }

    ImmutableSortedMap<String, ImmutableSortedMap<String, String>> sortedConfigMap = expanded.build();

    Hasher hasher = Hashing.sha256().newHasher();
    for (Entry<String, ImmutableSortedMap<String, String>> entry : sortedConfigMap.entrySet()) {
        hasher.putString(entry.getKey(), StandardCharsets.UTF_8);
        for (Entry<String, String> nestedEntry : entry.getValue().entrySet()) {
            hasher.putString(nestedEntry.getKey(), StandardCharsets.UTF_8);
            hasher.putString(nestedEntry.getValue(), StandardCharsets.UTF_8);
        }
    }

    return hasher.hash();
}

From source file:com.facebook.buck.apple.AppleDescriptions.java

/**
 * Convert {@link SourcePath} to a mapping of {@code include path -> file path}.
 * <p/>//  w w w. j a v a2  s  .co  m
 * {@code include path} is the path that can be referenced in {@code #include} directives.
 * {@code file path} is the actual path to the file on disk.
 *
 * @throws HumanReadableException when two {@code SourcePath} yields the same IncludePath.
 */
@VisibleForTesting
static ImmutableSortedMap<String, SourcePath> convertToFlatCxxHeaders(Path headerPathPrefix,
        Function<SourcePath, Path> sourcePathResolver, Set<SourcePath> headerPaths) {
    Set<String> includeToFile = new HashSet<String>(headerPaths.size());
    ImmutableSortedMap.Builder<String, SourcePath> builder = ImmutableSortedMap.naturalOrder();
    for (SourcePath headerPath : headerPaths) {
        Path fileName = sourcePathResolver.apply(headerPath).getFileName();
        String key = headerPathPrefix.resolve(fileName).toString();
        if (includeToFile.contains(key)) {
            ImmutableSortedMap<String, SourcePath> result = builder.build();
            throw new HumanReadableException(
                    "The same include path maps to multiple files:\n" + "  Include path: %s\n"
                            + "  Conflicting files:\n" + "    %s\n" + "    %s",
                    key, headerPath, result.get(key));
        }
        includeToFile.add(key);
        builder.put(key, headerPath);
    }
    return builder.build();
}