Example usage for java.util Collection size

List of usage examples for java.util Collection size

Introduction

In this page you can find the example usage for java.util Collection size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this collection.

Usage

From source file:de.unileipzig.ub.scroller.Main.java

public static void main(String[] args) throws IOException {

    Options options = new Options();
    // add t option
    options.addOption("h", "help", false, "display this help");

    // elasticsearch options
    options.addOption("t", "host", true, "elasticsearch hostname (default: 0.0.0.0)");
    options.addOption("p", "port", true, "transport port (that's NOT the http port, default: 9300)");
    options.addOption("c", "cluster", true, "cluster name (default: elasticsearch_mdma)");

    options.addOption("i", "index", true, "index to use");

    options.addOption("f", "filter", true, "filter(s) - e.g. meta.kind=title");
    options.addOption("j", "junctor", true, "values: and, or (default: and)");
    options.addOption("n", "notice-every", true, "show speed after every N items");

    options.addOption("v", "verbose", false, "be verbose");
    // options.addOption("z", "end-of-message", true, "sentinel to print to stdout, once the regular input finished (default: EOM)");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;//from   w  w w  .  j a v a 2  s  .  com

    try {
        cmd = parser.parse(options, args);
    } catch (ParseException ex) {
        logger.error(ex);
        System.exit(1);
    }

    // process options
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("scroller", options, true);
        System.exit(0);
    }

    String endOfMessage = "EOM";

    boolean verbose = false;
    if (cmd.hasOption("verbose")) {
        verbose = true;
    }

    if (!cmd.hasOption("i")) {
        System.err.println("error: no index specified");
        System.exit(1);
    }

    long noticeEvery = 10000;
    if (cmd.hasOption("n")) {
        noticeEvery = Long.parseLong(cmd.getOptionValue("n"));
    }

    // ES options
    String[] hosts = new String[] { "0.0.0.0" };
    int port = 9300;
    String clusterName = "elasticsearch_mdma";
    int bulkSize = 3000;

    if (cmd.hasOption("host")) {
        hosts = cmd.getOptionValues("host");
    }
    if (cmd.hasOption("port")) {
        port = Integer.parseInt(cmd.getOptionValue("port"));
    }
    if (cmd.hasOption("cluster")) {
        clusterName = cmd.getOptionValue("cluster");
    }

    // Index
    String indexName = cmd.getOptionValue("index");

    Map<String, String> filterMap = new HashMap<String, String>();
    if (cmd.hasOption("filter")) {
        try {
            filterMap = getMapForKeys(cmd.getOptionValues("filter"));
        } catch (ParseException pe) {
            System.err.println(pe);
            System.exit(1);
        }
    }

    Collection<HashMap> filterList = new ArrayList<HashMap>();
    if (cmd.hasOption("filter")) {
        try {
            filterList = getFilterList(cmd.getOptionValues("filter"));
        } catch (ParseException pe) {
            System.err.println(pe);
            System.exit(1);
        }
    }

    // ES Client
    final Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch_mdma")
            .put("client.transport.ping_timeout", "60s").build();
    final TransportClient client = new TransportClient(settings);
    for (String host : hosts) {
        client.addTransportAddress(new InetSocketTransportAddress(host, port));
    }

    // build the query
    String junctor = "and";
    if (cmd.hasOption("j")) {
        junctor = cmd.getOptionValue("j");
    }

    //        ArrayList<TermFilterBuilder> filters = new ArrayList<TermFilterBuilder>();
    //        if (filterMap.size() > 0) {
    //            for (Map.Entry<String, String> entry : filterMap.entrySet()) {
    //                filters.add(new TermFilterBuilder(entry.getKey(), entry.getValue()));
    //            }
    //        }

    ArrayList<TermFilterBuilder> filters = new ArrayList<TermFilterBuilder>();
    if (filterList.size() > 0) {
        for (HashMap map : filterList) {
            for (Object obj : map.entrySet()) {
                Map.Entry entry = (Map.Entry) obj;
                filters.add(new TermFilterBuilder(entry.getKey().toString(), entry.getValue().toString()));
            }
        }
    }

    FilterBuilder fb = null;
    if (junctor.equals("and")) {
        AndFilterBuilder afb = new AndFilterBuilder();
        for (TermFilterBuilder tfb : filters) {
            afb.add(tfb);
        }
        fb = afb;
    }

    if (junctor.equals("or")) {
        OrFilterBuilder ofb = new OrFilterBuilder();
        for (TermFilterBuilder tfb : filters) {
            ofb.add(tfb);
        }
        fb = ofb;
    }

    //        TermFilterBuilder tfb0 = new TermFilterBuilder("meta.kind", "title");
    //        TermFilterBuilder tfb1 = new TermFilterBuilder("meta.timestamp", "201112081240");
    //
    //        AndFilterBuilder afb0 = new AndFilterBuilder(tfb0, tfb1);

    QueryBuilder qb0 = null;
    if (filterMap.isEmpty()) {
        qb0 = matchAllQuery();
    } else {
        qb0 = filteredQuery(matchAllQuery(), fb);
    }

    // sorting
    // FieldSortBuilder sortBuilder = new FieldSortBuilder("meta.timestamp");
    // sortBuilder.order(SortOrder.DESC);

    // FilteredQueryBuilder fqb0 = filteredQuery(matchAllQuery(), tfb0);

    final CountResponse countResponse = client.prepareCount(indexName).setQuery(qb0).execute().actionGet();
    final long total = countResponse.getCount();

    SearchResponse scrollResp = client.prepareSearch(indexName).setSearchType(SearchType.SCAN)
            .setScroll(new TimeValue(60000)).setQuery(qb0)
            // .addSort(sortBuilder) // sort has no effect on scroll type (see: https://github.com/CPAN-API/cpan-api/issues/172)
            .setSize(1000) //1000 hits per shard will be returned for each scroll
            .execute().actionGet();

    //Scroll until no hits are returned

    System.err.println("[Scroller] query: " + qb0.toString());
    System.err.println("[Scroller] took: " + scrollResp.getTookInMillis() + "ms");
    System.err.println("[Scroller] docs found: " + total);

    long counter = 0;
    long start = System.currentTimeMillis();

    while (true) {
        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000))
                .execute().actionGet();

        if (scrollResp.getHits().hits().length == 0) {
            break;
        }

        for (SearchHit hit : scrollResp.getHits()) {

            System.out.println(hit.sourceAsString());
            counter += 1;
            if (counter % noticeEvery == 0) {
                final double elapsed = (System.currentTimeMillis() - start) / 1000;
                final double speed = counter / elapsed;
                final long eta = (long) ((elapsed / counter) * (total - counter) * 1000);
                System.err.println(
                        counter + "/" + total + " records recvd @ speed " + String.format("%1$,.1f", speed)
                                + " r/s eta: " + DurationFormatUtils.formatDurationWords(eta, false, false));
            }
        }
    }
    System.out.close();
    // System.out.println(endOfMessage);
}

From source file:edu.stanford.muse.index.Indexer.java

public static void main(String args[])
        throws IOException, ClassNotFoundException, ParseException, GeneralSecurityException {
    //      String content = "A FEDERAL JUDGE DECLINED on Monday to grant to the University of South Florida a declaratory judgment that the university's plan"
    //            + " to fire Sami Al-Arian, a tenured professor with alleged links to terrorism, did not violate his First Amendment right to free speech";
    //      Set<String> pnames = getNamesFromPatterns(content, true);
    //      Set<String> names = getNamesFromPatterns(content, false);
    //      System.err.println("Pnames: " + pnames);
    //      System.err.println("Names: " + names);
    //System.err.println(Indexer.getNamesFromPatterns("There is a story in today's New Zealand Herald about a German and French peace-keeping force that would work as UN Peacekeepers, presumably to protect IRAQ after the US invasion.... Mark", false));
    //      EmailUtils.test("NEA and the Massachusetts Cultural Council",
    //            "Dartmouth College and Harvard Law School",
    //            "Cornell University and Boston College",
    //            "The National Park Service",
    //            "Adams President Natural Resources Defense Council",
    //            "Tougher Disguises Press",
    //            "The Chronicle of Higher Education for Tuesday",
    //            "Electronic Poetry Center",
    //            "CIA",
    //            "CNN",
    //            "FBI",
    //            "Meridian Books",
    //            "Apple",
    //            "House Press",
    //            "Microsoft");
    //      EmailUtils.test("China", "NYState", "Orlando", "Elmwood", "Mykonos", "Raliegh",
    //            "Dresden", "Northampton", "IRAQ", "New Zealand", "English in Dresden");
    //testQueries();
    //      try {
    //         String aFile = System.getProperty("user.home") + File.separator + "epadd-appraisal" + File.separator + "user";
    //         String type = "3classes";
    ///*from  w ww. j  av  a  2  s .  c  o m*/
    //         String BASE_DIR = System.getProperty("user.home") + File.separator + "epadd-appraisal" + File.separator + "user" + File.separator + "models" + File.separator;
    //         String path = BASE_DIR + File.separator + type + "_" + edu.stanford.muse.ModeConfig.SVM_MODEL_FILE;
    //         String wfsPath = BASE_DIR + File.separator + type + "_" + edu.stanford.muse.ModeConfig.WORD_FEATURES;
    //         Archive archive = SimpleSessions.readArchiveIfPresent(aFile);
    //         WordFeatures.trainCustomModel(archive, path, wfsPath);
    //      } catch (Exception e) {
    //         e.printStackTrace();
    //      }
    String userDir = System.getProperty("user.home") + File.separator + "epadd-appraisal" + File.separator
            + "user";
    Archive archive = SimpleSessions.readArchiveIfPresent(userDir);
    String queries[] = new String[] {
            "(teaching|learning|research|university|college|school|education|fellowship|professor|graduate)",
            "(book|chapter|article|draft|submission|review|festschrift|poetry|prose|writing)",
            "(award|prize|medal|fellowship|certificate)",
            "(father|dad|dada|daddy|papa|pappa|pop|\"old man\"|mother|mama|mamma|mom|momma|mommy|mammy|mum|mummy|\"father in law\"|\"mother in law\"|\"brother in law\"|\"sister in law\"|stepfather|stepmother|husband|wife|son|boy|daughter|girl|sister|brother|cousin|grandfather|grandmother|grandson|granddaughter|genealogy|reunion)" };
    for (String q : queries) {
        long st = System.currentTimeMillis();
        Collection<Document> docs = archive.indexer.docsForQuery(q, new QueryOptions());
        long et = System.currentTimeMillis();
        System.err.println("Took: " + (et - st) + "ms to search for: " + q + "\nFound: #" + docs.size());
    }
}

From source file:edu.brown.costmodel.SingleSitedCostModel.java

/**
 * MAIN!//from  www  .  j av a 2 s  .c  o  m
 * 
 * @param vargs
 * @throws Exception
 */
public static void main(String[] vargs) throws Exception {
    ArgumentsParser args = ArgumentsParser.load(vargs);
    args.require(ArgumentsParser.PARAM_CATALOG, ArgumentsParser.PARAM_WORKLOAD,
            ArgumentsParser.PARAM_PARTITION_PLAN);
    assert (args.workload.getTransactionCount() > 0) : "No transactions were loaded from " + args.workload_path;

    if (args.hasParam(ArgumentsParser.PARAM_CATALOG_HOSTS)) {
        ClusterConfiguration cc = new ClusterConfiguration(args.getParam(ArgumentsParser.PARAM_CATALOG_HOSTS));
        args.updateCatalog(FixCatalog.addHostInfo(args.catalog, cc), null);
    }

    // Enable compact output
    final boolean table_output = (args.getOptParams().contains("table"));

    // If given a PartitionPlan, then update the catalog
    File pplan_path = new File(args.getParam(ArgumentsParser.PARAM_PARTITION_PLAN));
    PartitionPlan pplan = new PartitionPlan();
    pplan.load(pplan_path.getAbsolutePath(), args.catalog_db);
    if (args.getBooleanParam(ArgumentsParser.PARAM_PARTITION_PLAN_REMOVE_PROCS, false)) {
        for (Procedure catalog_proc : pplan.proc_entries.keySet()) {
            pplan.setNullProcParameter(catalog_proc);
        } // FOR
    }
    if (args.getBooleanParam(ArgumentsParser.PARAM_PARTITION_PLAN_RANDOM_PROCS, false)) {
        for (Procedure catalog_proc : pplan.proc_entries.keySet()) {
            pplan.setRandomProcParameter(catalog_proc);
        } // FOR
    }
    pplan.apply(args.catalog_db);

    System.out.println("Applied PartitionPlan '" + pplan_path + "' to catalog\n" + pplan);
    System.out.print(StringUtil.DOUBLE_LINE);
    // if (!table_output) {
    //
    // }
    // } else if (!table_output) {
    // System.err.println("PartitionPlan file '" + pplan_path +
    // "' does not exist. Ignoring...");
    // }
    if (args.hasParam(ArgumentsParser.PARAM_PARTITION_PLAN_OUTPUT)) {
        String output = args.getParam(ArgumentsParser.PARAM_PARTITION_PLAN_OUTPUT);
        if (output.equals("-"))
            output = pplan_path.getAbsolutePath();
        pplan.save(output);
        System.out.println("Saved PartitionPlan to '" + output + "'");
    }

    System.out.flush();

    // TODO: REMOVE STORED PROCEDURE ROUTING FOR SCHISM

    long singlepartition = 0;
    long multipartition = 0;
    long total = 0;
    SingleSitedCostModel costmodel = new SingleSitedCostModel(args.catalog_db);
    Collection<Integer> all_partitions = CatalogUtil.getAllPartitionIds(args.catalog_db);
    // costmodel.setEntropyWeight(4.0);
    // costmodel.setJavaExecutionWeightEnabled(true);
    // costmodel.setJavaExecutionWeight(100);

    // XXX: 2011-10-28
    costmodel.setCachingEnabled(true);
    Histogram<String> hist = new Histogram<String>();
    for (int i = 0; i < 2; i++) {
        ProfileMeasurement time = new ProfileMeasurement("costmodel").start();
        hist.clear();
        for (AbstractTraceElement<? extends CatalogType> element : args.workload) {
            if (element instanceof TransactionTrace) {
                total++;
                TransactionTrace xact = (TransactionTrace) element;
                boolean is_singlesited = costmodel.processTransaction(args.catalog_db, xact, null).singlesited;
                if (is_singlesited) {
                    singlepartition++;
                    hist.put(xact.getCatalogItemName());
                } else {
                    multipartition++;
                    if (!hist.contains(xact.getCatalogItemName()))
                        hist.put(xact.getCatalogItemName(), 0);
                }
            }
        } // FOR
        System.err.println("ESTIMATE TIME: " + time.stop().getTotalThinkTimeSeconds());
        break; // XXX
    } // FOR
      // long total_partitions_touched_txns =
      // costmodel.getTxnPartitionAccessHistogram().getSampleCount();
      // long total_partitions_touched_queries =
      // costmodel.getQueryPartitionAccessHistogram().getSampleCount();

    Histogram<Integer> h = null;
    if (!table_output) {
        System.out.println("Workload Procedure Histogram:");
        System.out.println(StringUtil.addSpacers(args.workload.getProcedureHistogram().toString()));
        System.out.print(StringUtil.DOUBLE_LINE);

        System.out.println("SinglePartition Procedure Histogram:");
        System.out.println(StringUtil.addSpacers(hist.toString()));
        System.out.print(StringUtil.DOUBLE_LINE);

        System.out.println("Java Execution Histogram:");
        h = costmodel.getJavaExecutionHistogram();
        h.setKeepZeroEntries(true);
        h.putAll(all_partitions, 0);
        System.out.println(StringUtil.addSpacers(h.toString()));
        System.out.print(StringUtil.DOUBLE_LINE);

        System.out.println("Transaction Partition Histogram:");
        h = costmodel.getTxnPartitionAccessHistogram();
        h.setKeepZeroEntries(true);
        h.putAll(all_partitions, 0);
        System.out.println(StringUtil.addSpacers(h.toString()));
        System.out.print(StringUtil.DOUBLE_LINE);

        System.out.println("Query Partition Touch Histogram:");
        h = costmodel.getQueryPartitionAccessHistogram();
        h.setKeepZeroEntries(true);
        h.putAll(all_partitions, 0);
        System.out.println(StringUtil.addSpacers(h.toString()));
        System.out.print(StringUtil.DOUBLE_LINE);
    }

    Map<String, Object> maps[] = new Map[2];
    int idx = 0;
    ListOrderedMap<String, Object> m = null;

    // Execution Cost
    m = new ListOrderedMap<String, Object>();
    m.put("SINGLE-PARTITION", singlepartition);
    m.put("MULTI-PARTITION", multipartition);
    m.put("TOTAL", total + " [" + singlepartition / (double) total + "]");
    m.put("PARTITIONS TOUCHED (TXNS)", costmodel.getTxnPartitionAccessHistogram().getSampleCount());
    m.put("PARTITIONS TOUCHED (QUERIES)", costmodel.getQueryPartitionAccessHistogram().getSampleCount());
    maps[idx++] = m;

    // Utilization
    m = new ListOrderedMap<String, Object>();
    costmodel.getJavaExecutionHistogram().setKeepZeroEntries(false);
    int active_partitions = costmodel.getJavaExecutionHistogram().getValueCount();
    m.put("ACTIVE PARTITIONS", active_partitions);
    m.put("IDLE PARTITIONS", (all_partitions.size() - active_partitions));
    // System.out.println("Partitions Touched By Queries: " +
    // total_partitions_touched_queries);

    Histogram<Integer> entropy_h = costmodel.getJavaExecutionHistogram();
    m.put("JAVA SKEW",
            SkewFactorUtil.calculateSkew(all_partitions.size(), entropy_h.getSampleCount(), entropy_h));

    entropy_h = costmodel.getTxnPartitionAccessHistogram();
    m.put("TRANSACTION SKEW",
            SkewFactorUtil.calculateSkew(all_partitions.size(), entropy_h.getSampleCount(), entropy_h));

    // TimeIntervalCostModel<SingleSitedCostModel> timecostmodel = new
    // TimeIntervalCostModel<SingleSitedCostModel>(args.catalog_db,
    // SingleSitedCostModel.class, 1);
    // timecostmodel.estimateCost(args.catalog_db, args.workload);
    // double entropy = timecostmodel.getLastEntropyCost()
    m.put("UTILIZATION",
            (costmodel.getJavaExecutionHistogram().getValueCount() / (double) all_partitions.size()));
    maps[idx++] = m;

    System.out.println(StringUtil.formatMaps(maps));
}

From source file:Main.java

public static boolean isEmply(Collection<?> c) {
    return c.size() == 0;
}

From source file:Main.java

public static <T> T only(Collection<T> coll) {
    if (coll.size() != 1) {
        throw new NoSuchElementException("Collection has none or more than one elements");
    }//from w ww  . j a v  a 2 s  . c  om
    return coll.iterator().next();
}

From source file:Main.java

public static boolean isEmpty(Collection c) {
    return c == null || c.size() == 0;
}

From source file:Main.java

public static <E> int size(Collection<E> c) {
    return c != null ? c.size() : 0;
}

From source file:Main.java

public static <T> int countNotNull(Collection<T> objects) {
    return objects.size() - countNull(objects);
}

From source file:Main.java

public static boolean isNotEmpty(Collection<?> c) {
    return (null != c && c.size() > 0);
}

From source file:Main.java

public static boolean isEmpty(Collection list) {
    return list == null || list.size() == 0;
}