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

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

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Adds the specified element to this set if it is not already present (optional operation).

Usage

From source file:com.facebook.buck.python.PythonLibrary.java

@Override
public List<Step> getBuildSteps(BuildContext context, BuildableContext buildableContext) {
    ImmutableList.Builder<Step> commands = ImmutableList.builder();

    // Copy all of the sources to a generated directory so that the generated directory can be
    // included as a $PYTHONPATH element. TODO(mbolin): Symlinks would be more efficient, but we
    // need to include this structure in the artifact, which is not guaranteed to be zip-friendly.
    commands.add(new MakeCleanDirectoryStep(pythonPathDirectory));

    ImmutableSortedSet.Builder<Path> directories = ImmutableSortedSet.naturalOrder();
    ImmutableList.Builder<Step> symlinkSteps = ImmutableList.builder();

    for (SourcePath src : srcs) {
        Path srcPath = src.resolve();
        Path targetPath = pythonPathDirectory.resolve(srcPath);

        directories.add(targetPath.getParent());
        symlinkSteps.add(new SymlinkFileStep(srcPath, targetPath, /* useAbsolutePaths */ false));

        buildableContext.recordArtifact(targetPath);
    }/*from  w ww.  j a v a 2s.c o  m*/

    for (Path path : directories.build()) {
        commands.add(new MkdirStep(path));
    }

    commands.addAll(symlinkSteps.build());

    return commands.build();
}

From source file:com.facebook.buck.rules.DescribedRuleBuilder.java

@Override
public DescribedRule build(BuildRuleResolver ruleResolver) {
    ImmutableSortedSet.Builder<BuildRule> declaredRules = ImmutableSortedSet.naturalOrder();
    for (BuildTarget dep : declaredDeps) {
        BuildRule rule = ruleResolver.get(dep);
        Preconditions.checkNotNull(rule, dep.toString());
        declaredRules.add(rule);
    }//from w  w  w  .  j av  a 2  s.c  o m
    ImmutableSortedSet.Builder<BuildRule> paramRules = ImmutableSortedSet.naturalOrder();
    for (BuildTarget dep : extraDeps) {
        paramRules.add(ruleResolver.get(dep));
    }

    ConstructorArgMarshaller inspector = new ConstructorArgMarshaller(Paths.get(target.getBasePath()));
    T arg = description.createUnpopulatedConstructorArg();
    inspector.populate(ruleResolver, ruleFactoryParams.getProjectFilesystem(), ruleFactoryParams, arg);

    // The params used for the Buildable only contain the declared parameters. However, the deps of
    // the rule include not only those, but also any that were picked up through the deps declared
    // via a SourcePath.
    BuildRuleParams params = new BuildRuleParams(target, declaredRules.build(), getVisibilityPatterns(),
            ruleFactoryParams.getProjectFilesystem(), ruleFactoryParams.getRuleKeyBuilderFactory());
    Buildable buildable = description.createBuildable(params, arg);

    // Check for graph enhancement.
    ImmutableSortedSet<BuildRule> enhancedDeps = buildable.getEnhancedDeps(ruleResolver);
    if (enhancedDeps != null) {
        paramRules.addAll(enhancedDeps);
    } else {
        paramRules.addAll(declaredRules.build());
    }

    // These are the params used by the rule, but not the buildable. Confusion will be lessened once
    // we move to a dependency graph that's not the action graph.
    params = new BuildRuleParams(params.getBuildTarget(), paramRules.build(), params.getVisibilityPatterns(),
            params.getProjectFilesystem(), params.getRuleKeyBuilderFactory());
    return new DescribedRule(description.getBuildRuleType(), buildable, params);
}

From source file:com.facebook.buck.thrift.ThriftCxxEnhancer.java

@VisibleForTesting
protected ImmutableSortedSet<String> getGeneratedSources(String thriftName, ImmutableList<String> services,
        ImmutableSet<String> options) {

    final String base = Files.getNameWithoutExtension(thriftName);
    final boolean bootstrap = options.contains("bootstrap");
    final boolean layouts = options.contains("frozen2");
    final boolean templates = cpp2 || options.contains("templates");
    final boolean perfhash = !cpp2 && options.contains("perfhash");
    final boolean separateProcessmap = cpp2 && options.contains("separate_processmap");
    final boolean fatal = cpp2 && options.contains("fatal");

    ImmutableSortedSet.Builder<String> sources = ImmutableSortedSet.naturalOrder();

    sources.add(base + "_constants.h");
    sources.add(base + "_constants.cpp");

    sources.add(base + "_types.h");
    sources.add(base + "_types.cpp");

    if (templates) {
        sources.add(base + "_types.tcc");
    }/*w ww .j a  v a 2 s.  co m*/

    if (layouts) {
        sources.add(base + "_layouts.h");
        sources.add(base + "_layouts.cpp");
    }

    if (!bootstrap && !cpp2) {
        sources.add(base + "_reflection.h");
        sources.add(base + "_reflection.cpp");
    }

    if (fatal) {
        final String[] suffixes = new String[] { "", "_enum", "_union", "_struct", "_constant", "_service",
                "_types", "_all" };

        for (String suffix : suffixes) {
            sources.add(base + "_fatal" + suffix + ".h");
            sources.add(base + "_fatal" + suffix + ".cpp");
        }
    }

    if (cpp2) {
        sources.add(base + "_types_custom_protocol.h");
    }

    for (String service : services) {

        sources.add(service + ".h");
        sources.add(service + ".cpp");

        if (cpp2) {
            sources.add(service + "_client.cpp");
            sources.add(service + "_custom_protocol.h");
        }

        if (separateProcessmap) {
            sources.add(service + "_processmap_binary.cpp");
            sources.add(service + "_processmap_compact.cpp");
        }

        if (templates) {
            sources.add(service + ".tcc");
        }

        if (perfhash) {
            sources.add(service + "_gperf.tcc");
        }

    }

    return sources.build();
}

From source file:org.voltcore.utils.COWNavigableSet.java

@Override
public boolean removeAll(Collection<?> c) {
    while (true) {
        ImmutableSortedSet<E> snapshot = m_set.get();
        ImmutableSortedSet.Builder<E> builder = ImmutableSortedSet.naturalOrder();

        boolean hadValues = false;
        for (E e : snapshot) {
            if (c.contains(e)) {
                hadValues = true;//w ww. j a  va  2  s. co  m
                continue;
            }
            builder.add(e);
        }

        if (hadValues) {
            if (m_set.compareAndSet(snapshot, builder.build())) {
                return true;
            }
        } else {
            return false;
        }
    }
}

From source file:com.google.jimfs.JimfsFileSystem.java

@SuppressWarnings("unchecked") // safe cast of immutable set
@Override//from   w  ww . j  a v  a 2 s  .c o m
public ImmutableSortedSet<Path> getRootDirectories() {
    ImmutableSortedSet.Builder<JimfsPath> builder = ImmutableSortedSet.orderedBy(pathService);
    for (Name name : fileStore.getRootDirectoryNames()) {
        builder.add(pathService.createRoot(name));
    }
    return (ImmutableSortedSet<Path>) (ImmutableSortedSet<?>) builder.build();
}

From source file:com.facebook.buck.hashing.FilePathHashLoader.java

@Override
public HashCode get(Path root) throws IOException {
    // In case the root path is a directory, collect all files contained in it and sort them before
    // hashing to avoid non-deterministic directory traversal order from influencing the hash.
    final ImmutableSortedSet.Builder<Path> files = ImmutableSortedSet.naturalOrder();
    Files.walkFileTree(defaultCellRoot.resolve(root), ImmutableSet.of(FileVisitOption.FOLLOW_LINKS),
            Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
                @Override/*from w  w  w . j  a  va  2  s .  c  om*/
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    files.add(file);
                    return FileVisitResult.CONTINUE;
                }
            });
    Hasher hasher = Hashing.sha1().newHasher();
    for (Path file : files.build()) {
        file = defaultCellRoot.resolve(file).toRealPath();
        boolean assumeModified = assumeModifiedFiles.contains(file);
        Path relativePath = MorePaths.relativize(defaultCellRoot, file);

        // For each file add its path to the hasher suffixed by whether we assume the file to be
        // modified or not. This way files with different paths always result in different hashes and
        // files that are assumed to be modified get different hashes than all unmodified files.
        StringHashing.hashStringAndLength(hasher, relativePath.toString());
        hasher.putBoolean(assumeModified);
    }
    return hasher.hash();
}

From source file:com.facebook.buck.cli.BuckQueryEnvironment.java

public ImmutableSet<QueryTarget> getTargetsFromBuildTargetsContainer(
        Iterable<? extends HasBuildTarget> buildTargetsContainer) {
    ImmutableSortedSet.Builder<QueryTarget> builder = ImmutableSortedSet.naturalOrder();
    for (HasBuildTarget hasBuildTarget : buildTargetsContainer) {
        builder.add(getOrCreateQueryBuildTarget(hasBuildTarget.getBuildTarget()));
    }//from   w w  w.  j a  v  a2 s .c om
    return builder.build();
}

From source file:com.facebook.buck.jvm.java.JavaLibraryDescription.java

@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph, BuildRuleParams params,
        BuildRuleResolver resolver, A args) throws NoSuchBuildTargetException {
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    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();

    if (flavors.contains(Javadoc.DOC_JAR)) {
        BuildTarget unflavored = BuildTarget.of(target.getUnflavoredBuildTarget());
        BuildRule baseLibrary = resolver.requireRule(unflavored);

        JarShape shape = params.getBuildTarget().getFlavors().contains(JavaLibrary.MAVEN_JAR) ? JarShape.MAVEN
                : JarShape.SINGLE;//from   w ww .j  a  v  a2  s  .c o m

        JarShape.Summary summary = shape.gatherDeps(baseLibrary);
        ImmutableSet<SourcePath> sources = summary.getPackagedRules().stream()
                .filter(HasSources.class::isInstance).map(rule -> ((HasSources) rule).getSources())
                .flatMap(Collection::stream).collect(MoreCollectors.toImmutableSet());

        // In theory, the only deps we need are the ones that contribute to the sourcepaths. However,
        // javadoc wants to have classes being documented have all their deps be available somewhere.
        // Ideally, we'd not build everything, but then we're not able to document any classes that
        // rely on auto-generated classes, such as those created by the Immutables library. Oh well.
        // Might as well add them as deps. *sigh*
        ImmutableSortedSet.Builder<BuildRule> deps = ImmutableSortedSet.naturalOrder();
        // Sourcepath deps
        deps.addAll(ruleFinder.filterBuildRuleInputs(sources));
        // Classpath deps
        deps.add(baseLibrary);
        deps.addAll(summary.getClasspath().stream()
                .filter(rule -> HasClasspathEntries.class.isAssignableFrom(rule.getClass()))
                .flatMap(rule -> rule.getTransitiveClasspathDeps().stream()).iterator());
        BuildRuleParams emptyParams = params.copyWithDeps(Suppliers.ofInstance(deps.build()),
                Suppliers.ofInstance(ImmutableSortedSet.of()));

        return new Javadoc(emptyParams, pathResolver, args.mavenCoords, args.mavenPomTemplate,
                summary.getMavenDeps(), sources);
    }

    if (flavors.contains(CalculateAbi.FLAVOR)) {
        BuildTarget libraryTarget = target.withoutFlavors(CalculateAbi.FLAVOR);
        resolver.requireRule(libraryTarget);
        return CalculateAbi.of(params.getBuildTarget(), ruleFinder, params,
                new BuildTargetSourcePath(libraryTarget));
    }

    BuildRuleParams paramsWithMavenFlavor = null;
    if (flavors.contains(JavaLibrary.MAVEN_JAR)) {
        paramsWithMavenFlavor = params;

        // 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
                .map(input -> AetherUtil.addClassifier(input, AetherUtil.CLASSIFIER_SOURCES));

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

    JavacOptions javacOptions = JavacOptionsFactory.create(defaultOptions, params, resolver, ruleFinder, args);

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

    ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps);
    BuildRuleParams javaLibraryParams = params.appendExtraDeps(Iterables.concat(
            BuildRules.getExportedRules(Iterables.concat(params.getDeclaredDeps().get(), exportedDeps,
                    resolver.getAllRules(args.providedDeps))),
            ruleFinder.filterBuildRuleInputs(javacOptions.getInputs(ruleFinder))));
    DefaultJavaLibrary defaultJavaLibrary = new DefaultJavaLibrary(javaLibraryParams, pathResolver, ruleFinder,
            args.srcs, validateResources(pathResolver, params.getProjectFilesystem(), args.resources),
            javacOptions.getGeneratedSourceFolderName(),
            args.proguardConfig.map(SourcePaths.toSourcePath(params.getProjectFilesystem())::apply),
            args.postprocessClassesCommands, exportedDeps, resolver.getAllRules(args.providedDeps),
            abiJarTarget, JavaLibraryRules.getAbiInputs(resolver, javaLibraryParams.getDeps()),
            javacOptions.trackClassUsage(), /* additionalClasspathEntries */ ImmutableSet.of(),
            new JavacToJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY), args.resourcesRoot,
            args.manifestFile, args.mavenCoords, args.tests, javacOptions.getClassesToRemoveFromJar());

    if (!flavors.contains(JavaLibrary.MAVEN_JAR)) {
        return defaultJavaLibrary;
    } else {
        return MavenUberJar.create(defaultJavaLibrary, Preconditions.checkNotNull(paramsWithMavenFlavor),
                pathResolver, args.mavenCoords, args.mavenPomTemplate);
    }
}

From source file:com.facebook.buck.util.hashing.FilePathHashLoader.java

@Override
public HashCode get(Path root) throws IOException {
    // In case the root path is a directory, collect all files contained in it and sort them before
    // hashing to avoid non-deterministic directory traversal order from influencing the hash.
    ImmutableSortedSet.Builder<Path> files = ImmutableSortedSet.naturalOrder();
    Files.walkFileTree(defaultCellRoot.resolve(root), ImmutableSet.of(FileVisitOption.FOLLOW_LINKS),
            Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
                @Override/*from w  w  w .  j  a v a 2s.c om*/
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                    files.add(file);
                    return FileVisitResult.CONTINUE;
                }
            });
    Hasher hasher = Hashing.sha1().newHasher();
    for (Path file : files.build()) {
        file = resolvePath(file);
        boolean assumeModified = assumeModifiedFiles.contains(file);
        Path relativePath = MorePaths.relativize(defaultCellRoot, file);

        // For each file add its path to the hasher suffixed by whether we assume the file to be
        // modified or not. This way files with different paths always result in different hashes and
        // files that are assumed to be modified get different hashes than all unmodified files.
        StringHashing.hashStringAndLength(hasher, relativePath.toString());
        hasher.putBoolean(assumeModified);
    }
    return hasher.hash();
}

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

private ImmutableSortedSet<HasMavenCoordinates> computeMavenDeps() {
    ImmutableSortedSet.Builder<HasMavenCoordinates> mavenDeps = ImmutableSortedSet.naturalOrder();
    for (JavaLibrary javaLibrary : getTransitiveClasspathDeps()) {
        if (this.equals(javaLibrary)) {
            // no need to include ourself
            continue;
        } else if (HasMavenCoordinates.MAVEN_COORDS_PRESENT_PREDICATE.apply(javaLibrary)) {
            mavenDeps.add(javaLibrary);
            //FIXME BOC do we always want to exclude all of a maven jar's dependencies? probably.
            mavenDeps.addAll(javaLibrary.getTransitiveClasspathDeps());
        }/*from ww  w . ja  v a2s . c o  m*/
    }
    return mavenDeps.build();
}