Example usage for com.google.common.collect Iterables size

List of usage examples for com.google.common.collect Iterables size

Introduction

In this page you can find the example usage for com.google.common.collect Iterables size.

Prototype

public static int size(Iterable<?> iterable) 

Source Link

Document

Returns the number of elements in iterable .

Usage

From source file:com.facebook.buck.simulate.BuildSimulator.java

public SimulateReport simulateBuild(long currentTimeMillis, ImmutableList<BuildTarget> buildTargets) {
    Preconditions.checkArgument(buildTargets.size() > 0, "No targets provided for the simulation.");
    SimulateReport.Builder simulateReport = SimulateReport.builder();

    for (String timeAggregate : times.getTimeAggregates()) {
        // Setup the build order.
        Map<BuildTarget, NodeState> reverseDependencies = Maps.newHashMap();
        Queue<BuildTarget> leafNodes = Queues.newArrayDeque();
        int totalDagEdges = 0;
        for (BuildTarget target : buildTargets) {
            BuildRule rule;/*from   www .  j  av a 2s.  c  om*/
            try {
                rule = resolver.requireRule(target);
            } catch (NoSuchBuildTargetException e) {
                throw new HumanReadableException(e.getHumanReadableErrorMessage());
            }

            totalDagEdges += recursiveTraversal(rule, reverseDependencies, leafNodes);
        }

        SingleRunReport.Builder report = SingleRunReport.builder().setTimestampMillis(currentTimeMillis)
                .setBuildTargets(FluentIterable.from(buildTargets).transform(Functions.toStringFunction()))
                .setSimulateTimesFile(times.getFile())
                .setRuleFallbackTimeMillis(times.getRuleFallbackTimeMillis())
                .setTotalActionGraphNodes(Iterables.size(actionGraph.getNodes()))
                .setTimeAggregate(timeAggregate).setNumberOfThreads(numberOfThreads);

        report.setTotalDependencyDagEdges(totalDagEdges);

        // Run the simulation.
        simulateReport.addRunReports(runSimulation(report, reverseDependencies, leafNodes, timeAggregate));
    }

    return simulateReport.build();
}

From source file:org.janusgraph.core.JanusGraphVertexQuery.java

/**
 * Returns the number of relations that match this query
 *
 * @return Number of relations that match this query
 *//* w  ww .  ja  va2  s  . c  om*/
public default long count() {
    return Iterables.size(relations());
}

From source file:com.davidbracewell.math.CompactCounter.java

/**
 * Instantiates a new Compact counter.//from  w  w w . j  a v a2 s  .  c  o  m
 *
 * @param iterable the iterable
 */
public CompactCounter(Iterable<? extends V> iterable) {
    this(Iterables.size(Preconditions.checkNotNull(iterable)));
    incrementAll(Preconditions.checkNotNull(iterable));
}

From source file:org.apache.giraph.graph.EdgeListVertex.java

@Override
public void initialize(I vertexId, V vertexValue, Map<I, E> edges, Iterable<M> messages) {
    if (vertexId != null) {
        setVertexId(vertexId);//from   w  w w.java 2s . com
    }
    if (vertexValue != null) {
        setVertexValue(vertexValue);
    }
    if (edges != null && !edges.isEmpty()) {
        destEdgeIndexList = Lists.newArrayListWithCapacity(edges.size());
        destEdgeValueList = Lists.newArrayListWithCapacity(edges.size());
        List<I> sortedIndexList = new ArrayList<I>(edges.keySet());
        Collections.sort(sortedIndexList, new VertexIdComparator());
        for (I index : sortedIndexList) {
            destEdgeIndexList.add(index);
            destEdgeValueList.add(edges.get(index));
        }
        sortedIndexList.clear();
    } else {
        destEdgeIndexList = Lists.newArrayListWithCapacity(0);
        destEdgeValueList = Lists.newArrayListWithCapacity(0);
    }
    if (messages != null) {
        msgList = Lists.newArrayListWithCapacity(Iterables.size(messages));
        Iterables.<M>addAll(msgList, messages);
    } else {
        msgList = Lists.newArrayListWithCapacity(0);
    }
}

From source file:org.estatio.services.scheduler.work.ScheduledWork.java

private static int numItemsInState(Iterable<ScheduledWorkItem> workItems, ItemState state) {
    return Iterables.size(Iterables.filter(workItems, ScheduledWorkItem.Predicates.inState(state)));
}

From source file:com.google.devtools.build.lib.bazel.rules.genrule.GenRule.java

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException, InterruptedException {
    final List<Artifact> resolvedSrcs = Lists.newArrayList();

    final NestedSet<Artifact> filesToBuild = NestedSetBuilder.wrap(Order.STABLE_ORDER,
            ruleContext.getOutputArtifacts());
    if (filesToBuild.isEmpty()) {
        ruleContext.attributeError("outs", "Genrules without outputs don't make sense");
    }//from ww w  . j a v  a  2  s.  co  m
    if (ruleContext.attributes().get("executable", Type.BOOLEAN) && Iterables.size(filesToBuild) > 1) {
        ruleContext.attributeError("executable",
                "if genrules produce executables, they are allowed only one output. "
                        + "If you need the executable=1 argument, then you should split this genrule into "
                        + "genrules producing single outputs");
    }

    ImmutableMap.Builder<Label, Iterable<Artifact>> labelMap = ImmutableMap.builder();
    for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("srcs", Mode.TARGET)) {
        Iterable<Artifact> files = dep.getProvider(FileProvider.class).getFilesToBuild();
        Iterables.addAll(resolvedSrcs, files);
        labelMap.put(dep.getLabel(), files);
    }

    CommandHelper commandHelper = new CommandHelper(ruleContext,
            ruleContext.getPrerequisites("tools", Mode.HOST), labelMap.build());

    if (ruleContext.hasErrors()) {
        return null;
    }

    String baseCommand = commandHelper.resolveCommandAndExpandLabels(
            ruleContext.attributes().get("heuristic_label_expansion", Type.BOOLEAN), false);

    // Adds the genrule environment setup script before the actual shell command
    String command = String.format("source %s; %s",
            ruleContext.getPrerequisiteArtifact("$genrule_setup", Mode.HOST).getExecPath(), baseCommand);

    command = resolveCommand(ruleContext, command, resolvedSrcs, filesToBuild);

    String message = ruleContext.attributes().get("message", Type.STRING);
    if (message.isEmpty()) {
        message = "Executing genrule";
    }

    ImmutableMap<String, String> env = ruleContext.getConfiguration().getLocalShellEnvironment();
    ImmutableSet<String> clientEnvVars = ruleContext.getConfiguration().getVariableShellEnvironment();

    Map<String, String> executionInfo = Maps.newLinkedHashMap();
    executionInfo.putAll(TargetUtils.getExecutionInfo(ruleContext.getRule()));

    if (ruleContext.attributes().get("local", Type.BOOLEAN)) {
        executionInfo.put("local", "");
    }

    NestedSetBuilder<Artifact> inputs = NestedSetBuilder.stableOrder();
    inputs.addAll(resolvedSrcs);
    inputs.addAll(commandHelper.getResolvedTools());
    FilesToRunProvider genruleSetup = ruleContext.getPrerequisite("$genrule_setup", Mode.HOST,
            FilesToRunProvider.class);
    inputs.addAll(genruleSetup.getFilesToRun());
    List<String> argv = commandHelper.buildCommandLine(command, inputs, ".genrule_script.sh");

    if (ruleContext.attributes().get("stamp", Type.BOOLEAN)) {
        inputs.add(ruleContext.getAnalysisEnvironment().getStableWorkspaceStatusArtifact());
        inputs.add(ruleContext.getAnalysisEnvironment().getVolatileWorkspaceStatusArtifact());
    }

    ruleContext.registerAction(new GenRuleAction(ruleContext.getActionOwner(),
            ImmutableList.copyOf(commandHelper.getResolvedTools()), inputs.build(), filesToBuild, argv, env,
            clientEnvVars, ImmutableMap.copyOf(executionInfo),
            ImmutableMap.copyOf(commandHelper.getRemoteRunfileManifestMap()),
            message + ' ' + ruleContext.getLabel()));

    RunfilesProvider runfilesProvider = withData(
            // No runfiles provided if not a data dependency.
            Runfiles.EMPTY,
            // We only need to consider the outputs of a genrule
            // No need to visit the dependencies of a genrule. They cross from the target into the host
            // configuration, because the dependencies of a genrule are always built for the host
            // configuration.
            new Runfiles.Builder(ruleContext.getWorkspaceName(),
                    ruleContext.getConfiguration().legacyExternalRunfiles())
                            .addTransitiveArtifacts(filesToBuild).build());

    return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild)
            .setRunfilesSupport(null, getExecutable(ruleContext, filesToBuild))
            .addProvider(RunfilesProvider.class, runfilesProvider).build();
}

From source file:org.gitools.analysis.groupcomparison.GroupComparisonFunction.java

@Override
public Map<String, GroupComparisonResult> apply(String identifier, IMatrixPosition position) {

    // Filter according to Predicate (groupFilter), transform according to nullConversion,
    // and finally remove nulls.

    Iterator<int[]> combIterator = CombinatoricsUtils.combinationsIterator(groups.length, 2);
    // LinkedHashMap to guarantee order in result heatmap
    Map<String, GroupComparisonResult> resultHashMap = new LinkedHashMap<>();

    while (combIterator.hasNext()) {
        int[] groupIndices = combIterator.next();

        DimensionGroup dimensionGroup1 = groups[groupIndices[0]];
        DimensionGroup dimensionGroup2 = groups[groupIndices[1]];

        Iterable<Double> group1 = filter(
                transform(position.iterate(valueLayer, sourceDimension).filter(dimensionGroup1.getPredicate()),
                        nullConversion),
                notNull());//from www  .j  a v a  2 s. c  o m
        Iterable<Double> group2 = filter(
                transform(position.iterate(valueLayer, sourceDimension).filter(dimensionGroup2.getPredicate()),
                        nullConversion),
                notNull());

        GroupComparisonResult result = null;
        if (Iterables.size(group1) < 3 || Iterables.size(group2) < 3) {
            result = test.getNullResult(Iterables.size(group1) + Iterables.size(group2), Iterables.size(group1),
                    Iterables.size(group2));
        } else {
            result = test.processTest(group1, group2);
        }

        resultHashMap.put(dimensionGroup1.getName() + " VS " + dimensionGroup2.getName(), result);

    }

    return resultHashMap;
}

From source file:com.google.devtools.build.lib.bazel.rules.genrule.BazelGenRule.java

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException, InterruptedException {
    final List<Artifact> resolvedSrcs = Lists.newArrayList();

    final NestedSet<Artifact> filesToBuild = NestedSetBuilder.wrap(Order.STABLE_ORDER,
            ruleContext.getOutputArtifacts());
    if (filesToBuild.isEmpty()) {
        ruleContext.attributeError("outs", "Genrules without outputs don't make sense");
    }/*from w ww  . ja  v a2 s  .  co m*/
    if (ruleContext.attributes().get("executable", Type.BOOLEAN) && Iterables.size(filesToBuild) > 1) {
        ruleContext.attributeError("executable",
                "if genrules produce executables, they are allowed only one output. "
                        + "If you need the executable=1 argument, then you should split this genrule into "
                        + "genrules producing single outputs");
    }

    ImmutableMap.Builder<Label, Iterable<Artifact>> labelMap = ImmutableMap.builder();
    for (TransitiveInfoCollection dep : ruleContext.getPrerequisites("srcs", Mode.TARGET)) {
        Iterable<Artifact> files = dep.getProvider(FileProvider.class).getFilesToBuild();
        Iterables.addAll(resolvedSrcs, files);
        labelMap.put(dep.getLabel(), files);
    }

    CommandHelper commandHelper = new CommandHelper(ruleContext,
            ruleContext.getPrerequisites("tools", Mode.HOST), labelMap.build());

    if (ruleContext.hasErrors()) {
        return null;
    }

    String baseCommand = commandHelper.resolveCommandAndExpandLabels(
            ruleContext.attributes().get("heuristic_label_expansion", Type.BOOLEAN), false);

    // Adds the genrule environment setup script before the actual shell command
    String command = String.format("source %s; %s",
            ruleContext.getPrerequisiteArtifact("$genrule_setup", Mode.HOST).getExecPath(), baseCommand);

    command = resolveCommand(ruleContext, command, resolvedSrcs, filesToBuild);

    String message = ruleContext.attributes().get("message", Type.STRING);
    if (message.isEmpty()) {
        message = "Executing genrule";
    }

    ImmutableMap<String, String> env = ruleContext.getConfiguration().getLocalShellEnvironment();
    ImmutableSet<String> clientEnvVars = ruleContext.getConfiguration().getVariableShellEnvironment();

    Map<String, String> executionInfo = Maps.newLinkedHashMap();
    executionInfo.putAll(TargetUtils.getExecutionInfo(ruleContext.getRule()));

    if (ruleContext.attributes().get("local", Type.BOOLEAN)) {
        executionInfo.put("local", "");
    }

    NestedSetBuilder<Artifact> inputs = NestedSetBuilder.stableOrder();
    inputs.addAll(resolvedSrcs);
    inputs.addAll(commandHelper.getResolvedTools());
    FilesToRunProvider genruleSetup = ruleContext.getPrerequisite("$genrule_setup", Mode.HOST,
            FilesToRunProvider.class);
    inputs.addAll(genruleSetup.getFilesToRun());
    List<String> argv = commandHelper.buildCommandLine(command, inputs, ".genrule_script.sh");

    if (ruleContext.attributes().get("stamp", Type.BOOLEAN)) {
        inputs.add(ruleContext.getAnalysisEnvironment().getStableWorkspaceStatusArtifact());
        inputs.add(ruleContext.getAnalysisEnvironment().getVolatileWorkspaceStatusArtifact());
    }

    ruleContext.registerAction(new BazelGenRuleAction(ruleContext.getActionOwner(),
            ImmutableList.copyOf(commandHelper.getResolvedTools()), inputs.build(), filesToBuild, argv, env,
            clientEnvVars, ImmutableMap.copyOf(executionInfo),
            ImmutableMap.copyOf(commandHelper.getRemoteRunfileManifestMap()),
            message + ' ' + ruleContext.getLabel()));

    RunfilesProvider runfilesProvider = withData(
            // No runfiles provided if not a data dependency.
            Runfiles.EMPTY,
            // We only need to consider the outputs of a genrule
            // No need to visit the dependencies of a genrule. They cross from the target into the host
            // configuration, because the dependencies of a genrule are always built for the host
            // configuration.
            new Runfiles.Builder(ruleContext.getWorkspaceName(),
                    ruleContext.getConfiguration().legacyExternalRunfiles())
                            .addTransitiveArtifacts(filesToBuild).build());

    return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild)
            .setRunfilesSupport(null, getExecutable(ruleContext, filesToBuild))
            .addProvider(RunfilesProvider.class, runfilesProvider).build();
}

From source file:univ.bigdata.course.JavaPageRank.java

public static List<PageRankResults> Rank(JavaRDD<String> ranksrdd, int numOfIterations) throws Exception {

    //    showWarning();
    ///*from   w  w  w  .j  a  v  a 2 s  .  c o m*/
    //    SparkSession spark = SparkSession
    //      .builder()
    //      .appName("JavaPageRank")
    //      .getOrCreate();

    // Loads in input file. It should be in format of:
    //     URL         neighbor URL
    //     URL         neighbor URL
    //     URL         neighbor URL
    //     ...

    /////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////
    //change main to function that gets JavaRDD and number of iterations. 
    /////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////
    JavaRDD<String> lines = ranksrdd;

    // Loads all URLs from input file and initialize their neighbors.
    JavaPairRDD<String, Iterable<String>> links = lines.mapToPair((PairFunction<String, String, String>) s -> {
        String[] parts = SPACES.split(s);
        return new Tuple2<>(parts[0], parts[1]);
    }).distinct().groupByKey().cache();

    // Loads all URLs with other URL(s) link to from input file and initialize ranks of them to one.
    JavaPairRDD<String, Double> ranks = links.mapValues((Function<Iterable<String>, Double>) rs -> 1.0);

    // Calculates and updates URL ranks continuously using PageRank algorithm.
    for (int current = 0; current < numOfIterations; current++) {
        // Calculates URL contributions to the rank of other URLs.
        JavaPairRDD<String, Double> contribs = links.join(ranks).values()
                .flatMapToPair((PairFlatMapFunction<Tuple2<Iterable<String>, Double>, String, Double>) s -> {
                    int urlCount = Iterables.size(s._1);
                    List<Tuple2<String, Double>> results = new ArrayList<>();
                    for (String n : s._1) {
                        results.add(new Tuple2<>(n, s._2() / urlCount));
                    }
                    return results;
                });

        // Re-calculates URL ranks based on neighbor contributions.
        ranks = contribs.reduceByKey(new Sum()).mapValues((Function<Double, Double>) sum -> 0.15 + sum * 0.85);
        //      List<Tuple2<String, Double>> t2 = contribs.collect();
        //      t2.clear();
    }
    //    List<Tuple2<String, Double>> t = ranks.collect();
    // Collects all URL ranks and dump them to console.
    JavaRDD<PageRankResults> output = ranks.map(o -> new PageRankResults(o._1, o._2));
    //    List<PageRankResults> t = output.collect();
    //    List<PageRankResults> t1 = output.collect();
    return output.collect();//.sort(new PageRankComperator());

    //    spark.stop();
}

From source file:org.eclipse.elk.core.util.RandomLayoutProvider.java

/**
 * Randomize the given graph./*from  w  w  w .j  a va2 s. c o m*/
 * 
 * @param parent the parent node of the graph
 * @param random the random number generator
 * @param aspectRatio desired aspect ratio (must not be 0)
 * @param spacing desired object spacing
 * @param offset offset to the border
 */
private void randomize(final ElkNode parent, final Random random, final double aspectRatio,
        final double spacing, final ElkPadding padding) {

    // determine width and height of the drawing and count the number of edges
    double nodesArea = 0.0f, maxWidth = 0.0f, maxHeight = 0.0f;
    int m = 1;
    for (ElkNode node : parent.getChildren()) {
        m += Iterables.size(ElkGraphUtil.allOutgoingEdges(node));

        double width = node.getWidth();
        maxWidth = Math.max(maxWidth, width);
        double height = node.getHeight();
        maxHeight = Math.max(maxHeight, height);

        nodesArea += width * height;
    }
    int n = parent.getChildren().size();

    // a heuristic formula that determines an area in which nodes are randomly distributed
    double drawArea = nodesArea + 2 * spacing * spacing * m * n;
    double areaSqrt = (double) Math.sqrt(drawArea);
    double drawWidth = Math.max(areaSqrt * aspectRatio, maxWidth);
    double drawHeight = Math.max(areaSqrt / aspectRatio, maxHeight);

    // randomize node positions
    for (ElkNode node : parent.getChildren()) {
        double x = padding.getLeft() + random.nextDouble() * (drawWidth - node.getWidth());
        double y = padding.getLeft() + random.nextDouble() * (drawHeight - node.getHeight());
        node.setLocation(x, y);
    }

    // randomize edge positions
    double totalWidth = drawWidth + padding.getHorizontal();
    double totalHeight = drawHeight + padding.getVertical();
    for (ElkNode source : parent.getChildren()) {
        for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(source)) {
            if (!edge.isHierarchical()) {
                randomize(edge, random, totalWidth, totalHeight);
            }
        }
    }

    totalWidth += padding.getLeft() + padding.getRight();
    totalHeight += padding.getTop() + padding.getBottom();
    ElkUtil.resizeNode(parent, totalWidth, totalHeight, false, true);
}