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:org.apache.cassandra.service.RowRepairResolver.java

static ColumnFamily resolveSuperset(Iterable<ColumnFamily> versions) {
    assert Iterables.size(versions) > 0;

    ColumnFamily resolved = null;/*from w ww . ja va  2 s.c o  m*/
    for (ColumnFamily cf : versions) {
        if (cf == null)
            continue;

        if (resolved == null)
            resolved = cf.cloneMeShallow();
        else
            resolved.delete(cf);
    }
    if (resolved == null)
        return null;

    // mimic the collectCollatedColumn + removeDeleted path that getColumnFamily takes.
    // this will handle removing columns and subcolumns that are supressed by a row or
    // supercolumn tombstone.
    QueryFilter filter = new QueryFilter(null, new QueryPath(resolved.metadata().cfName),
            new IdentityQueryFilter());
    List<CloseableIterator<IColumn>> iters = new ArrayList<CloseableIterator<IColumn>>();
    for (ColumnFamily version : versions) {
        if (version == null)
            continue;
        iters.add(FBUtilities.closeableIterator(version.iterator()));
    }
    filter.collateColumns(resolved, iters, Integer.MIN_VALUE);
    return ColumnFamilyStore.removeDeleted(resolved, Integer.MIN_VALUE);
}

From source file:fr.rjoakim.android.jonetouch.async.CommandExecutor.java

private String formatDisplayMessage(String message) {
    Iterable<String> multi = Splitter.on("\n").split(message);
    if (Iterables.size(multi) > 1) {
        return server.getAuthentication().getLogin() + "@" + server.getHost() + ":~$ " + "./script.sh";
    } else {// w w w .j  a v a2  s  . c o  m
        return server.getAuthentication().getLogin() + "@" + server.getHost() + ":~$ " + message;
    }
}

From source file:org.gradle.api.internal.impldeps.GradleImplDepsRelocatedJarCreator.java

private void processFiles(ZipOutputStream outputStream, Iterable<? extends File> files, byte[] buffer,
        HashSet<String> seenPaths, Map<String, List<String>> services, ProgressLogger progressLogger)
        throws Exception {
    PercentageProgressFormatter progressFormatter = new PercentageProgressFormatter("Generating",
            Iterables.size(files) + ADDITIONAL_PROGRESS_STEPS);

    for (File file : files) {
        progressLogger.progress(progressFormatter.getProgress());

        if (file.getName().endsWith(".jar")) {
            processJarFile(outputStream, file, buffer, seenPaths, services);
        } else {/*from w  w  w .j a  v a 2 s. c o m*/
            processDirectory(outputStream, file, buffer, seenPaths, services);
        }

        progressFormatter.incrementAndGetProgress();
    }

    writeServiceFiles(outputStream, services);
    progressFormatter.incrementAndGetProgress();

    writeMarkerFile(outputStream);
    progressFormatter.incrementAndGetProgress();
}

From source file:com.projectnine.SparkPageRankProgram.java

@Override
public void run(JavaSparkExecutionContext sec) throws Exception {
    JavaSparkContext jsc = new JavaSparkContext();

    LOG.info("Processing backlinkURLs data");
    JavaPairRDD<Long, String> backlinkURLs = sec.fromStream("backlinkURLStream", String.class);
    int iterationCount = getIterationCount(sec);

    LOG.info("Grouping data by key");
    // Grouping backlinks by unique URL in key
    JavaPairRDD<String, Iterable<String>> links = backlinkURLs.values()
            .mapToPair(new PairFunction<String, String, String>() {
                @Override//from   w  w w  . j  a  v  a2  s.c  o  m
                public Tuple2<String, String> call(String s) {
                    String[] parts = SPACES.split(s);
                    return new Tuple2<>(parts[0], parts[1]);
                }
            }).distinct().groupByKey().cache();

    // Initialize default rank for each key URL
    JavaPairRDD<String, Double> ranks = links.mapValues(new Function<Iterable<String>, Double>() {
        @Override
        public Double call(Iterable<String> rs) {
            return 1.0;
        }
    });
    // Calculates and updates URL ranks continuously using PageRank algorithm.
    for (int current = 0; current < iterationCount; current++) {
        LOG.debug("Processing data with PageRank algorithm. Iteration {}/{}", current + 1, (iterationCount));
        // Calculates URL contributions to the rank of other URLs.
        JavaPairRDD<String, Double> contribs = links.join(ranks).values()
                .flatMapToPair(new PairFlatMapFunction<Tuple2<Iterable<String>, Double>, String, Double>() {
                    @Override
                    public Iterable<Tuple2<String, Double>> call(Tuple2<Iterable<String>, Double> s) {
                        LOG.debug("Processing {} with rank {}", s._1(), s._2());
                        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 backlink contributions.
        ranks = contribs.reduceByKey(new Sum()).mapValues(new Function<Double, Double>() {
            @Override
            public Double call(Double sum) {
                return 0.15 + sum * 0.85;
            }
        });
    }

    LOG.info("Writing ranks data");

    final ServiceDiscoverer discoveryServiceContext = sec.getServiceDiscoverer();
    final Metrics sparkMetrics = sec.getMetrics();
    JavaPairRDD<byte[], Integer> ranksRaw = ranks
            .mapToPair(new PairFunction<Tuple2<String, Double>, byte[], Integer>() {
                @Override
                public Tuple2<byte[], Integer> call(Tuple2<String, Double> tuple) throws Exception {
                    LOG.debug("URL {} has rank {}", Arrays.toString(tuple._1().getBytes(Charsets.UTF_8)),
                            tuple._2());
                    URL serviceURL = discoveryServiceContext.getServiceURL(SparkPageRankApp.SERVICE_HANDLERS);
                    if (serviceURL == null) {
                        throw new RuntimeException(
                                "Failed to discover service: " + SparkPageRankApp.SERVICE_HANDLERS);
                    }
                    try {
                        URLConnection connection = new URL(serviceURL,
                                String.format("%s/%s",
                                        SparkPageRankApp.SparkPageRankServiceHandler.TRANSFORM_PATH,
                                        tuple._2().toString())).openConnection();
                        try (BufferedReader reader = new BufferedReader(
                                new InputStreamReader(connection.getInputStream(), Charsets.UTF_8))) {
                            String pr = reader.readLine();
                            if ((Integer.parseInt(pr)) == POPULAR_PAGE_THRESHOLD) {
                                sparkMetrics.count(POPULAR_PAGES, 1);
                            } else if (Integer.parseInt(pr) <= UNPOPULAR_PAGE_THRESHOLD) {
                                sparkMetrics.count(UNPOPULAR_PAGES, 1);
                            } else {
                                sparkMetrics.count(REGULAR_PAGES, 1);
                            }
                            return new Tuple2<>(tuple._1().getBytes(Charsets.UTF_8), Integer.parseInt(pr));
                        }
                    } catch (Exception e) {
                        LOG.warn("Failed to read the Stream for service {}", SparkPageRankApp.SERVICE_HANDLERS,
                                e);
                        throw Throwables.propagate(e);
                    }
                }
            });

    // Store calculated results in output Dataset.
    // All calculated results are stored in one row.
    // Each result, the calculated URL rank based on backlink contributions, is an entry of the row.
    // The value of the entry is the URL rank.
    sec.saveAsDataset(ranksRaw, "ranks");

    LOG.info("PageRanks successfuly computed and written to \"ranks\" dataset");
}

From source file:com.google.template.soy.jbcsrc.Expression.java

/**
 * Checks that the given expressions are compatible with the given types.
 *///from w  ww.j av  a  2s  .  c o m
static void checkTypes(ImmutableList<Type> types, Iterable<? extends Expression> exprs) {
    int size = Iterables.size(exprs);
    checkArgument(size == types.size(), "Supplied the wrong number of parameters. Expected %s, got %s",
            types.size(), size);
    int i = 0;
    for (Expression expr : exprs) {
        expr.checkAssignableTo(types.get(i), "Parameter %s", i);
        i++;
    }
}

From source file:org.roda.core.common.iterables.CloseableIterables.java

public static <T> int size(CloseableIterable<T> iterable) {
    int size = Iterables.size(iterable);
    IOUtils.closeQuietly(iterable);//from   w  ww. ja v a 2s.c o m
    return size;
}

From source file:org.eclipse.osee.orcs.core.internal.search.CallableQueryFactory.java

public CancellableCallable<ResultSet<ArtifactReadable>> createSearch(OrcsSession session, QueryData queryData) {
    return new AbstractSearchCallable<ResultSet<ArtifactReadable>>(session, queryData) {

        @Override// w  w  w .  j av a 2s  .c o m
        protected ResultSet<ArtifactReadable> innerCall() throws Exception {
            GraphBuilder handler = builderFactory.createGraphBuilder(provider);
            OptionsUtil.setLoadLevel(getQueryData().getOptions(), LoadLevel.ALL);
            queryEngine.createArtifactQuery(getSession(), getQueryData(), handler).call();
            Iterable<Artifact> results = handler.getArtifacts();
            setItemsFound(Iterables.size(results));
            return proxyManager.asExternalArtifacts(getSession(), results);
        }
    };
}

From source file:org.apache.beam.sdk.testutils.metrics.MetricsReader.java

private <T> void checkIfMetricResultIsUnique(String name, Iterable<MetricResult<T>> metricResult)
        throws IllegalStateException {

    int resultCount = Iterables.size(metricResult);
    Preconditions.checkState(resultCount <= 1,
            "More than one metric result matches name: %s in namespace %s. Metric results count: %s", name,
            namespace, resultCount);/*w  w  w.ja v  a  2 s .co  m*/
}

From source file:com.groupon.jenkins.buildsetup.GithubReposController.java

public int getSelectedOrgIndex() throws IOException {
    Iterable<String> orgs = getOrgs();
    for (int i = 0; i < Iterables.size(orgs); i++) {
        if (Iterables.get(orgs, i).equals(getCurrentOrg()))
            return i;
    }//from ww w . j  a va  2 s  . c o m
    return 0;
}

From source file:org.eclipse.sirius.ui.tools.internal.actions.export.AbstractExportRepresentationsAction.java

/**
 * Display the export path and file format dialog and then export the
 * representations./* www  .  jav a 2 s. c o m*/
 * 
 * @param exportPath
 *            the default export path.
 * @param dRepresentationToExport
 *            the representation to export.
 * @param session
 *            the corresponding session.
 */
protected void exportRepresentation(IPath exportPath, Iterable<DRepresentation> dRepresentationToExport,
        Session session) {
    final Shell shell = Display.getCurrent().getActiveShell();

    final AbstractExportRepresentationsAsImagesDialog dialog;
    if (Iterables.size(dRepresentationToExport) > 1) {
        dialog = new ExportSeveralRepresentationsAsImagesDialog(shell, exportPath);
    } else {
        dialog = new ExportOneRepresentationAsImageDialog(shell, exportPath,
                dRepresentationToExport.iterator().next().getName());
    }

    if (dialog.open() == Window.OK) {
        List<DRepresentation> toExport = Lists.<DRepresentation>newArrayList(dRepresentationToExport);
        final ExportAction exportAction = new ExportAction(session, toExport, dialog.getOutputPath(),
                dialog.getImageFormat(), dialog.isExportToHtml());
        final ProgressMonitorDialog pmd = new ProgressMonitorDialog(shell);
        try {
            pmd.run(false, false, exportAction);
        } catch (final InvocationTargetException e) {
            MessageDialog.openError(shell, Messages.AbstractExportRepresentationsAction_error,
                    e.getTargetException().getMessage());
        } catch (final InterruptedException e) {
            MessageDialog.openInformation(shell, Messages.AbstractExportRepresentationsAction_error,
                    e.getMessage());
        } finally {
            pmd.close();
        }
    } else {
        dialog.close();
    }
}