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

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

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSet.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:io.prestosql.plugin.cassandra.CassandraPartitionManager.java

private static List<Set<Object>> getPartitionKeysList(CassandraTable table,
        TupleDomain<ColumnHandle> tupleDomain) {
    ImmutableList.Builder<Set<Object>> partitionColumnValues = ImmutableList.builder();
    for (CassandraColumnHandle columnHandle : table.getPartitionKeyColumns()) {
        Domain domain = tupleDomain.getDomains().get().get(columnHandle);

        // if there is no constraint on a partition key, return an empty set
        if (domain == null) {
            return ImmutableList.of();
        }/* ww  w . j  av  a2s  .com*/

        // todo does cassandra allow null partition keys?
        if (domain.isNullAllowed()) {
            return ImmutableList.of();
        }

        Set<Object> values = domain.getValues().getValuesProcessor().transform(ranges -> {
            ImmutableSet.Builder<Object> columnValues = ImmutableSet.builder();
            for (Range range : ranges.getOrderedRanges()) {
                // if the range is not a single value, we can not perform partition pruning
                if (!range.isSingleValue()) {
                    return ImmutableSet.of();
                }
                Object value = range.getSingleValue();

                CassandraType valueType = columnHandle.getCassandraType();
                columnValues.add(valueType.validatePartitionKey(value));
            }
            return columnValues.build();
        }, discreteValues -> {
            if (discreteValues.isWhiteList()) {
                return ImmutableSet.copyOf(discreteValues.getValues());
            }
            return ImmutableSet.of();
        }, allOrNone -> ImmutableSet.of());
        partitionColumnValues.add(values);
    }
    return partitionColumnValues.build();
}

From source file:com.facebook.buck.cxx.NativeLinkables.java

/**
 * Extract from the dependency graph all the libraries which must be considered for linking.
 *
 * Traversal proceeds depending on whether each dependency is to be statically or dynamically
 * linked.//from  www .  j  a v  a 2  s  .  c  o  m
 *
 * @param linkStyle how dependencies should be linked, if their preferred_linkage is
 *                  {@code NativeLinkable.Linkage.ANY}.
 */
public static ImmutableMap<BuildTarget, NativeLinkable> getNativeLinkables(final CxxPlatform cxxPlatform,
        Iterable<? extends NativeLinkable> inputs, final Linker.LinkableDepType linkStyle,
        final Predicate<? super NativeLinkable> traverse) {

    final Map<BuildTarget, NativeLinkable> nativeLinkables = Maps.newHashMap();
    for (NativeLinkable nativeLinkable : inputs) {
        nativeLinkables.put(nativeLinkable.getBuildTarget(), nativeLinkable);
    }

    final MutableDirectedGraph<BuildTarget> graph = new MutableDirectedGraph<>();
    AbstractBreadthFirstTraversal<BuildTarget> visitor = new AbstractBreadthFirstTraversal<BuildTarget>(
            nativeLinkables.keySet()) {
        @Override
        public ImmutableSet<BuildTarget> visit(BuildTarget target) {
            NativeLinkable nativeLinkable = Preconditions.checkNotNull(nativeLinkables.get(target));
            graph.addNode(target);

            // We always traverse a rule's exported native linkables.
            Iterable<? extends NativeLinkable> nativeLinkableDeps = nativeLinkable
                    .getNativeLinkableExportedDepsForPlatform(cxxPlatform);

            boolean shouldTraverse = true;
            switch (nativeLinkable.getPreferredLinkage(cxxPlatform)) {
            case ANY:
                shouldTraverse = linkStyle != Linker.LinkableDepType.SHARED;
                break;
            case SHARED:
                shouldTraverse = false;
                break;
            case STATIC:
                shouldTraverse = true;
                break;
            }

            // If we're linking this dependency statically, we also need to traverse its deps.
            if (shouldTraverse) {
                nativeLinkableDeps = Iterables.concat(nativeLinkableDeps,
                        nativeLinkable.getNativeLinkableDepsForPlatform(cxxPlatform));
            }

            // Process all the traversable deps.
            ImmutableSet.Builder<BuildTarget> deps = ImmutableSet.builder();
            for (NativeLinkable dep : nativeLinkableDeps) {
                if (traverse.apply(dep)) {
                    BuildTarget depTarget = dep.getBuildTarget();
                    graph.addEdge(target, depTarget);
                    deps.add(depTarget);
                    nativeLinkables.put(depTarget, dep);
                }
            }
            return deps.build();
        }
    };
    visitor.start();

    // Topologically sort the rules.
    Iterable<BuildTarget> ordered = TopologicalSort.sort(graph).reverse();

    // Return a map of of the results.
    ImmutableMap.Builder<BuildTarget, NativeLinkable> result = ImmutableMap.builder();
    for (BuildTarget target : ordered) {
        result.put(target, nativeLinkables.get(target));
    }
    return result.build();
}

From source file:edu.oregonstate.eecs.mcplan.abstraction.WekaUtil.java

public static Instances powerSet(final Instances D, final int n) {
    final Attribute class_attr = D.classAttribute();

    final ImmutableSet.Builder<Integer> b = new ImmutableSet.Builder<Integer>();
    final int Nattr = class_attr != null ? D.numAttributes() - 1 : D.numAttributes();
    for (final int i : Fn.range(1, Nattr)) {
        b.add(i);
    }// www  .  j av  a 2 s .c  o m
    final Set<Set<Integer>> index = Sets.powerSet(b.build());

    final ArrayList<Attribute> attributes = new ArrayList<Attribute>();
    for (final Set<Integer> subset : index) {
        if (subset.isEmpty() || subset.size() > n) {
            continue;
        }

        final StringBuilder attr_name = new StringBuilder();
        int count = 0;
        for (final Integer i : subset) {
            if (count++ > 0) {
                attr_name.append("_x_");
            }
            attr_name.append(D.attribute(i).name());
        }

        attributes.add(new Attribute(attr_name.toString()));
    }
    if (class_attr != null) {
        assert (class_attr.isNominal());
        attributes.add(WekaUtil.createNominalAttribute(class_attr.name(), class_attr.numValues()));
    }

    final String Pname = "P" + n + "_" + D.relationName();
    final Instances P = new Instances(Pname, attributes, 0);
    if (class_attr != null) {
        P.setClassIndex(attributes.size() - 1);
    }

    for (final Instance inst : D) {
        final double[] xp = new double[attributes.size()];
        int idx = 0;
        for (final Set<Integer> subset : index) {
            if (subset.isEmpty() || subset.size() > n) {
                continue;
            }

            double p = 1.0;
            for (final Integer i : subset) {
                p *= inst.value(i);
            }
            xp[idx++] = p;
        }
        if (class_attr != null) {
            xp[idx++] = inst.classValue();
        }

        WekaUtil.addInstance(P, new DenseInstance(inst.weight(), xp));
    }

    return P;
}

From source file:com.android.build.gradle.internal.pipeline.IntermediateFolderUtils.java

@Nullable
private static Set<ContentType> stringToTypes(String folderName) {
    int value;//from w w w . j av a  2 s  .  co m
    try {
        value = Integer.parseInt(folderName, 16);
    } catch (NumberFormatException e) {
        return null;
    }

    ImmutableSet.Builder<ContentType> typesBuilder = ImmutableSet.builder();

    for (ContentType type : ExtendedContentType.getAllContentTypes()) {
        if ((type.getValue() & value) != 0) {
            typesBuilder.add(type);
        }
    }

    Set<ContentType> types = typesBuilder.build();
    if (types.isEmpty()) {
        return null;
    }

    return types;
}

From source file:com.google.caliper.runner.instrument.InstrumentModule.java

@Provides
static ImmutableSet<InstrumentedMethod> provideInstrumentedMethods(CaliperOptions options,
        BenchmarkClassModel benchmarkClass, ImmutableSet<Instrument> instruments)
        throws InvalidBenchmarkException {
    ImmutableSet.Builder<InstrumentedMethod> builder = ImmutableSet.builder();
    ImmutableSet<String> benchmarkMethodNames = options.benchmarkMethodNames();
    Set<String> unusedBenchmarkNames = new HashSet<String>(benchmarkMethodNames);
    for (Instrument instrument : instruments) {
        for (MethodModel method : findAllBenchmarkMethods(benchmarkClass, instrument)) {
            if (benchmarkMethodNames.isEmpty() || benchmarkMethodNames.contains(method.name())) {
                builder.add(instrument.createInstrumentedMethod(method));
                unusedBenchmarkNames.remove(method.name());
            }// w w w  .j  ava 2 s.  co  m
        }
    }
    if (!unusedBenchmarkNames.isEmpty()) {
        throw new InvalidBenchmarkException("Invalid benchmark method(s) specified in options: %s",
                unusedBenchmarkNames);
    }
    return builder.build();
}

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

public static ImmutableSet<Path> getOutputClasspathJars(JavaLibrary javaLibraryRule,
        SourcePathResolver resolver, Optional<SourcePath> outputJar) {
    ImmutableSet.Builder<Path> outputClasspathBuilder = ImmutableSet.builder();
    Iterable<JavaLibrary> javaExportedLibraryDeps;
    if (javaLibraryRule instanceof ExportDependencies) {
        javaExportedLibraryDeps = getJavaLibraryDeps(((ExportDependencies) javaLibraryRule).getExportedDeps());
    } else {/*w  w  w.  j  a va 2  s .c  o m*/
        javaExportedLibraryDeps = Sets.newHashSet();
    }

    for (JavaLibrary rule : javaExportedLibraryDeps) {
        outputClasspathBuilder.addAll(rule.getOutputClasspaths());
    }

    if (outputJar.isPresent()) {
        outputClasspathBuilder.add(resolver.getAbsolutePath(outputJar.get()));
    }

    return outputClasspathBuilder.build();
}

From source file:co.cask.cdap.explore.service.ExploreServiceUtils.java

/**
 * Return the list of absolute paths of the bootstrap classes.
 *//*  w  w  w .j a v  a  2 s . c  om*/
public static Set<String> getBoostrapClasses() {
    ImmutableSet.Builder<String> builder = ImmutableSet.builder();
    for (String classpath : Splitter.on(File.pathSeparatorChar)
            .split(System.getProperty("sun.boot.class.path"))) {
        File file = new File(classpath);
        builder.add(file.getAbsolutePath());
        try {
            builder.add(file.getCanonicalPath());
        } catch (IOException e) {
            LOG.warn("Could not add canonical path to aux class path for file {}", file.toString(), e);
        }
    }
    return builder.build();
}

From source file:com.google.api.codegen.transformer.py.PythonSampleOutputImportTransformer.java

private static void addEnumImports(ImmutableSet.Builder<ImportFileView> imports, MethodContext context,
        OutputView.PrintView view) {//from   w  w w. j a v  a2s  . co m
    boolean addEnumImports = view.args().stream().flatMap(arg -> arg.segments().stream())
            .filter(seg -> seg.kind() == PrintArgView.ArgSegmentView.Kind.VARIABLE)
            .map(seg -> ((PrintArgView.VariableSegmentView) seg).variable().type())
            .anyMatch(type -> type != null && type.isEnum());
    if (addEnumImports) {
        ImportTypeView importTypeView = ImportTypeView.newBuilder().fullName("enums")
                .type(ImportType.SimpleImport).nickname("").build();
        imports.add(ImportFileView.newBuilder().moduleName(context.getNamer().getVersionedDirectoryNamespace())
                .types(Collections.singletonList(importTypeView)).build());
    }
}

From source file:google.registry.batch.VerifyEntityIntegrityAction.java

private static ImmutableSet<Input<? extends Object>> getInputs() {
    ImmutableSet.Builder<Input<? extends Object>> builder = new ImmutableSet.Builder<Input<? extends Object>>()
            .add(EppResourceInputs.createIndexInput());
    for (Class<?> clazz : RESOURCE_CLASSES) {
        builder.add(new DatastoreKeyInput(getKind(clazz), NUM_SHARDS));
    }//w ww.  j  a va 2 s  .c om
    return builder.build();
}

From source file:com.facebook.buck.go.GoDescriptors.java

private static ImmutableSet<GoLinkable> requireGoLinkables(final BuildTarget sourceTarget,
        final BuildRuleResolver resolver, final GoPlatform platform, Iterable<BuildTarget> targets)
        throws NoSuchBuildTargetException {
    final ImmutableSet.Builder<GoLinkable> linkables = ImmutableSet.builder();
    new AbstractBreadthFirstThrowingTraversal<BuildTarget, NoSuchBuildTargetException>(targets) {
        @Override/*from  ww  w  . j  av a 2s.co m*/
        public Iterable<BuildTarget> visit(BuildTarget target) throws NoSuchBuildTargetException {
            GoLinkable linkable = requireGoLinkable(sourceTarget, resolver, platform, target);
            linkables.add(linkable);
            return linkable.getExportedDeps();
        }
    }.start();
    return linkables.build();
}