Example usage for java.lang Double doubleValue

List of usage examples for java.lang Double doubleValue

Introduction

In this page you can find the example usage for java.lang Double doubleValue.

Prototype

@HotSpotIntrinsicCandidate
public double doubleValue() 

Source Link

Document

Returns the double value of this Double object.

Usage

From source file:org.apache.solr.client.solrj.io.stream.StreamExpressionTest.java

@Test
public void testDaemonStream() throws Exception {

    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1")
            .add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2")
            .add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3")
            .add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4")
            .add(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5")
            .add(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6")
            .add(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7")
            .add(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8")
            .add(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9")
            .add(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10")
            .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

    StreamFactory factory = new StreamFactory()
            .withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress())
            .withFunctionName("search", CloudSolrStream.class).withFunctionName("rollup", RollupStream.class)
            .withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class)
            .withFunctionName("max", MaxMetric.class).withFunctionName("avg", MeanMetric.class)
            .withFunctionName("count", CountMetric.class).withFunctionName("daemon", DaemonStream.class);

    StreamExpression expression;//  w w  w . j a va2 s  .c o m
    DaemonStream daemonStream;

    expression = StreamExpressionParser.parse(
            "daemon(rollup(" + "search(" + COLLECTIONORALIAS + ", q=\"*:*\", fl=\"a_i,a_s\", sort=\"a_s asc\"),"
                    + "over=\"a_s\"," + "sum(a_i)" + "), id=\"test\", runInterval=\"1000\", queueSize=\"9\")");
    daemonStream = (DaemonStream) factory.constructStream(expression);
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    daemonStream.setStreamContext(streamContext);
    try {
        //Test Long and Double Sums

        daemonStream.open(); // This will start the daemon thread

        for (int i = 0; i < 4; i++) {
            Tuple tuple = daemonStream.read(); // Reads from the queue
            String bucket = tuple.getString("a_s");
            Double sumi = tuple.getDouble("sum(a_i)");

            //System.out.println("#################################### Bucket 1:"+bucket);
            assertTrue(bucket.equals("hello0"));
            assertTrue(sumi.doubleValue() == 17.0D);

            tuple = daemonStream.read();
            bucket = tuple.getString("a_s");
            sumi = tuple.getDouble("sum(a_i)");

            //System.out.println("#################################### Bucket 2:"+bucket);
            assertTrue(bucket.equals("hello3"));
            assertTrue(sumi.doubleValue() == 38.0D);

            tuple = daemonStream.read();
            bucket = tuple.getString("a_s");
            sumi = tuple.getDouble("sum(a_i)");
            //System.out.println("#################################### Bucket 3:"+bucket);
            assertTrue(bucket.equals("hello4"));
            assertTrue(sumi.longValue() == 15);
        }

        //Now lets wait until the internal queue fills up

        while (daemonStream.remainingCapacity() > 0) {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {

            }
        }

        //OK capacity is full, let's index a new doc

        new UpdateRequest().add(id, "10", "a_s", "hello0", "a_i", "1", "a_f", "10")
                .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

        //Now lets clear the existing docs in the queue 9, plus 3 more to get passed the run that was blocked. The next run should
        //have the tuples with the updated count.
        for (int i = 0; i < 12; i++) {
            daemonStream.read();
        }

        //And rerun the loop. It should have a new count for hello0
        for (int i = 0; i < 4; i++) {
            Tuple tuple = daemonStream.read(); // Reads from the queue
            String bucket = tuple.getString("a_s");
            Double sumi = tuple.getDouble("sum(a_i)");

            //System.out.println("#################################### Bucket 1:"+bucket);
            assertTrue(bucket.equals("hello0"));
            assertTrue(sumi.doubleValue() == 18.0D);

            tuple = daemonStream.read();
            bucket = tuple.getString("a_s");
            sumi = tuple.getDouble("sum(a_i)");

            //System.out.println("#################################### Bucket 2:"+bucket);
            assertTrue(bucket.equals("hello3"));
            assertTrue(sumi.doubleValue() == 38.0D);

            tuple = daemonStream.read();
            bucket = tuple.getString("a_s");
            sumi = tuple.getDouble("sum(a_i)");
            //System.out.println("#################################### Bucket 3:"+bucket);
            assertTrue(bucket.equals("hello4"));
            assertTrue(sumi.longValue() == 15);
        }
    } finally {
        daemonStream.close(); //This should stop the daemon thread
        solrClientCache.close();
    }
}

From source file:org.apache.solr.client.solrj.io.stream.StreamExpressionTest.java

@Test
public void testSubFacetStream() throws Exception {

    new UpdateRequest().add(id, "0", "level1_s", "hello0", "level2_s", "a", "a_i", "0", "a_f", "1")
            .add(id, "2", "level1_s", "hello0", "level2_s", "a", "a_i", "2", "a_f", "2")
            .add(id, "3", "level1_s", "hello3", "level2_s", "a", "a_i", "3", "a_f", "3")
            .add(id, "4", "level1_s", "hello4", "level2_s", "a", "a_i", "4", "a_f", "4")
            .add(id, "1", "level1_s", "hello0", "level2_s", "b", "a_i", "1", "a_f", "5")
            .add(id, "5", "level1_s", "hello3", "level2_s", "b", "a_i", "10", "a_f", "6")
            .add(id, "6", "level1_s", "hello4", "level2_s", "b", "a_i", "11", "a_f", "7")
            .add(id, "7", "level1_s", "hello3", "level2_s", "b", "a_i", "12", "a_f", "8")
            .add(id, "8", "level1_s", "hello3", "level2_s", "b", "a_i", "13", "a_f", "9")
            .add(id, "9", "level1_s", "hello0", "level2_s", "b", "a_i", "14", "a_f", "10")
            .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

    String clause;//ww w  . j  a  v a2s  .c o  m
    TupleStream stream;
    List<Tuple> tuples;

    StreamFactory factory = new StreamFactory()
            .withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress())
            .withFunctionName("facet", FacetStream.class).withFunctionName("sum", SumMetric.class)
            .withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class)
            .withFunctionName("avg", MeanMetric.class).withFunctionName("count", CountMetric.class);

    // Basic test
    clause = "facet(" + "collection1, " + "q=\"*:*\", " + "buckets=\"level1_s, level2_s\", "
            + "bucketSorts=\"sum(a_i) desc, sum(a_i) desc)\", " + "bucketSizeLimit=100, " + "sum(a_i), count(*)"
            + ")";

    stream = factory.constructStream(clause);
    tuples = getTuples(stream);

    assert (tuples.size() == 6);

    Tuple tuple = tuples.get(0);
    String bucket1 = tuple.getString("level1_s");
    String bucket2 = tuple.getString("level2_s");
    Double sumi = tuple.getDouble("sum(a_i)");
    Double count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello3"));
    assertTrue(bucket2.equals("b"));
    assertTrue(sumi.longValue() == 35);
    assertTrue(count.doubleValue() == 3);

    tuple = tuples.get(1);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello0"));
    assertTrue(bucket2.equals("b"));
    assertTrue(sumi.longValue() == 15);
    assertTrue(count.doubleValue() == 2);

    tuple = tuples.get(2);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello4"));
    assertTrue(bucket2.equals("b"));
    assertTrue(sumi.longValue() == 11);
    assertTrue(count.doubleValue() == 1);

    tuple = tuples.get(3);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello4"));
    assertTrue(bucket2.equals("a"));
    assertTrue(sumi.longValue() == 4);
    assertTrue(count.doubleValue() == 1);

    tuple = tuples.get(4);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello3"));
    assertTrue(bucket2.equals("a"));
    assertTrue(sumi.longValue() == 3);
    assertTrue(count.doubleValue() == 1);

    tuple = tuples.get(5);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello0"));
    assertTrue(bucket2.equals("a"));
    assertTrue(sumi.longValue() == 2);
    assertTrue(count.doubleValue() == 2);

    clause = "facet(" + "collection1, " + "q=\"*:*\", " + "buckets=\"level1_s, level2_s\", "
            + "bucketSorts=\"level1_s desc, level2_s desc)\", " + "bucketSizeLimit=100, " + "sum(a_i), count(*)"
            + ")";

    stream = factory.constructStream(clause);
    tuples = getTuples(stream);

    assert (tuples.size() == 6);

    tuple = tuples.get(0);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello4"));
    assertTrue(bucket2.equals("b"));
    assertTrue(sumi.longValue() == 11);
    assertTrue(count.doubleValue() == 1);

    tuple = tuples.get(1);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello4"));
    assertTrue(bucket2.equals("a"));
    assertTrue(sumi.longValue() == 4);
    assertTrue(count.doubleValue() == 1);

    tuple = tuples.get(2);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello3"));
    assertTrue(bucket2.equals("b"));
    assertTrue(sumi.longValue() == 35);
    assertTrue(count.doubleValue() == 3);

    tuple = tuples.get(3);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello3"));
    assertTrue(bucket2.equals("a"));
    assertTrue(sumi.longValue() == 3);
    assertTrue(count.doubleValue() == 1);

    tuple = tuples.get(4);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello0"));
    assertTrue(bucket2.equals("b"));
    assertTrue(sumi.longValue() == 15);
    assertTrue(count.doubleValue() == 2);

    tuple = tuples.get(5);
    bucket1 = tuple.getString("level1_s");
    bucket2 = tuple.getString("level2_s");
    sumi = tuple.getDouble("sum(a_i)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket1.equals("hello0"));
    assertTrue(bucket2.equals("a"));
    assertTrue(sumi.longValue() == 2);
    assertTrue(count.doubleValue() == 2);
}

From source file:org.apache.solr.client.solrj.io.stream.StreamExpressionTest.java

@Test
public void testStatsStream() throws Exception {

    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1")
            .add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2")
            .add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3")
            .add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4")
            .add(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5")
            .add(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6")
            .add(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7")
            .add(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8")
            .add(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9")
            .add(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10")
            .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

    StreamFactory factory = new StreamFactory()
            .withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress())
            .withFunctionName("stats", StatsStream.class).withFunctionName("sum", SumMetric.class)
            .withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class)
            .withFunctionName("avg", MeanMetric.class).withFunctionName("count", CountMetric.class);

    StreamExpression expression;//  ww  w .  j a  va2  s  .c o m
    TupleStream stream;
    List<Tuple> tuples;
    StreamContext streamContext = new StreamContext();
    SolrClientCache cache = new SolrClientCache();
    try {
        streamContext.setSolrClientCache(cache);
        String expr = "stats(" + COLLECTIONORALIAS
                + ", q=*:*, sum(a_i), sum(a_f), min(a_i), min(a_f), max(a_i), max(a_f), avg(a_i), avg(a_f), count(*))";
        expression = StreamExpressionParser.parse(expr);
        stream = factory.constructStream(expression);
        stream.setStreamContext(streamContext);

        tuples = getTuples(stream);

        assert (tuples.size() == 1);

        //Test Long and Double Sums

        Tuple tuple = tuples.get(0);

        Double sumi = tuple.getDouble("sum(a_i)");
        Double sumf = tuple.getDouble("sum(a_f)");
        Double mini = tuple.getDouble("min(a_i)");
        Double minf = tuple.getDouble("min(a_f)");
        Double maxi = tuple.getDouble("max(a_i)");
        Double maxf = tuple.getDouble("max(a_f)");
        Double avgi = tuple.getDouble("avg(a_i)");
        Double avgf = tuple.getDouble("avg(a_f)");
        Double count = tuple.getDouble("count(*)");

        assertTrue(sumi.longValue() == 70);
        assertTrue(sumf.doubleValue() == 55.0D);
        assertTrue(mini.doubleValue() == 0.0D);
        assertTrue(minf.doubleValue() == 1.0D);
        assertTrue(maxi.doubleValue() == 14.0D);
        assertTrue(maxf.doubleValue() == 10.0D);
        assertTrue(avgi.doubleValue() == 7.0D);
        assertTrue(avgf.doubleValue() == 5.5D);
        assertTrue(count.doubleValue() == 10);

        //Test with shards parameter
        List<String> shardUrls = TupleStream.getShards(cluster.getZkServer().getZkAddress(), COLLECTIONORALIAS,
                streamContext);
        expr = "stats(myCollection, q=*:*, sum(a_i), sum(a_f), min(a_i), min(a_f), max(a_i), max(a_f), avg(a_i), avg(a_f), count(*))";
        Map<String, List<String>> shardsMap = new HashMap();
        shardsMap.put("myCollection", shardUrls);
        StreamContext context = new StreamContext();
        context.put("shards", shardsMap);
        context.setSolrClientCache(cache);
        stream = factory.constructStream(expr);
        stream.setStreamContext(context);

        tuples = getTuples(stream);

        assert (tuples.size() == 1);

        //Test Long and Double Sums

        tuple = tuples.get(0);

        sumi = tuple.getDouble("sum(a_i)");
        sumf = tuple.getDouble("sum(a_f)");
        mini = tuple.getDouble("min(a_i)");
        minf = tuple.getDouble("min(a_f)");
        maxi = tuple.getDouble("max(a_i)");
        maxf = tuple.getDouble("max(a_f)");
        avgi = tuple.getDouble("avg(a_i)");
        avgf = tuple.getDouble("avg(a_f)");
        count = tuple.getDouble("count(*)");

        assertTrue(sumi.longValue() == 70);
        assertTrue(sumf.doubleValue() == 55.0D);
        assertTrue(mini.doubleValue() == 0.0D);
        assertTrue(minf.doubleValue() == 1.0D);
        assertTrue(maxi.doubleValue() == 14.0D);
        assertTrue(maxf.doubleValue() == 10.0D);
        assertTrue(avgi.doubleValue() == 7.0D);
        assertTrue(avgf.doubleValue() == 5.5D);
        assertTrue(count.doubleValue() == 10);

        //Execersise the /stream hander

        //Add the shards http parameter for the myCollection
        StringBuilder buf = new StringBuilder();
        for (String shardUrl : shardUrls) {
            if (buf.length() > 0) {
                buf.append(",");
            }
            buf.append(shardUrl);
        }

        ModifiableSolrParams solrParams = new ModifiableSolrParams();
        solrParams.add("qt", "/stream");
        solrParams.add("expr", expr);
        solrParams.add("myCollection.shards", buf.toString());
        SolrStream solrStream = new SolrStream(shardUrls.get(0), solrParams);
        tuples = getTuples(solrStream);
        assert (tuples.size() == 1);

        tuple = tuples.get(0);

        sumi = tuple.getDouble("sum(a_i)");
        sumf = tuple.getDouble("sum(a_f)");
        mini = tuple.getDouble("min(a_i)");
        minf = tuple.getDouble("min(a_f)");
        maxi = tuple.getDouble("max(a_i)");
        maxf = tuple.getDouble("max(a_f)");
        avgi = tuple.getDouble("avg(a_i)");
        avgf = tuple.getDouble("avg(a_f)");
        count = tuple.getDouble("count(*)");

        assertTrue(sumi.longValue() == 70);
        assertTrue(sumf.doubleValue() == 55.0D);
        assertTrue(mini.doubleValue() == 0.0D);
        assertTrue(minf.doubleValue() == 1.0D);
        assertTrue(maxi.doubleValue() == 14.0D);
        assertTrue(maxf.doubleValue() == 10.0D);
        assertTrue(avgi.doubleValue() == 7.0D);
        assertTrue(avgf.doubleValue() == 5.5D);
        assertTrue(count.doubleValue() == 10);
        //Add a negative test to prove that it cannot find slices if shards parameter is removed

        try {
            ModifiableSolrParams solrParamsBad = new ModifiableSolrParams();
            solrParamsBad.add("qt", "/stream");
            solrParamsBad.add("expr", expr);
            solrStream = new SolrStream(shardUrls.get(0), solrParamsBad);
            tuples = getTuples(solrStream);
            throw new Exception("Exception should have been thrown above");
        } catch (IOException e) {
            assertTrue(e.getMessage().contains("Collection not found: myCollection"));
        }
    } finally {
        cache.close();
    }
}

From source file:org.apache.solr.client.solrj.io.stream.StreamExpressionTest.java

@Test
public void testRollupStream() throws Exception {

    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1")
            .add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2")
            .add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3")
            .add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4")
            .add(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5")
            .add(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6")
            .add(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7")
            .add(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8")
            .add(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9")
            .add(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10")
            .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

    StreamFactory factory = new StreamFactory()
            .withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress())
            .withFunctionName("search", CloudSolrStream.class).withFunctionName("rollup", RollupStream.class)
            .withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class)
            .withFunctionName("max", MaxMetric.class).withFunctionName("avg", MeanMetric.class)
            .withFunctionName("count", CountMetric.class);

    StreamExpression expression;/* w w w .  ja v  a  2s  .co  m*/
    TupleStream stream;
    List<Tuple> tuples;
    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);
    try {
        expression = StreamExpressionParser.parse(
                "rollup(" + "search(" + COLLECTIONORALIAS + ", q=*:*, fl=\"a_s,a_i,a_f\", sort=\"a_s asc\"),"
                        + "over=\"a_s\"," + "sum(a_i)," + "sum(a_f)," + "min(a_i)," + "min(a_f)," + "max(a_i),"
                        + "max(a_f)," + "avg(a_i)," + "avg(a_f)," + "count(*)," + ")");
        stream = factory.constructStream(expression);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);

        assert (tuples.size() == 3);

        //Test Long and Double Sums

        Tuple tuple = tuples.get(0);
        String bucket = tuple.getString("a_s");
        Double sumi = tuple.getDouble("sum(a_i)");
        Double sumf = tuple.getDouble("sum(a_f)");
        Double mini = tuple.getDouble("min(a_i)");
        Double minf = tuple.getDouble("min(a_f)");
        Double maxi = tuple.getDouble("max(a_i)");
        Double maxf = tuple.getDouble("max(a_f)");
        Double avgi = tuple.getDouble("avg(a_i)");
        Double avgf = tuple.getDouble("avg(a_f)");
        Double count = tuple.getDouble("count(*)");

        assertTrue(bucket.equals("hello0"));
        assertTrue(sumi.doubleValue() == 17.0D);
        assertTrue(sumf.doubleValue() == 18.0D);
        assertTrue(mini.doubleValue() == 0.0D);
        assertTrue(minf.doubleValue() == 1.0D);
        assertTrue(maxi.doubleValue() == 14.0D);
        assertTrue(maxf.doubleValue() == 10.0D);
        assertTrue(avgi.doubleValue() == 4.25D);
        assertTrue(avgf.doubleValue() == 4.5D);
        assertTrue(count.doubleValue() == 4);

        tuple = tuples.get(1);
        bucket = tuple.getString("a_s");
        sumi = tuple.getDouble("sum(a_i)");
        sumf = tuple.getDouble("sum(a_f)");
        mini = tuple.getDouble("min(a_i)");
        minf = tuple.getDouble("min(a_f)");
        maxi = tuple.getDouble("max(a_i)");
        maxf = tuple.getDouble("max(a_f)");
        avgi = tuple.getDouble("avg(a_i)");
        avgf = tuple.getDouble("avg(a_f)");
        count = tuple.getDouble("count(*)");

        assertTrue(bucket.equals("hello3"));
        assertTrue(sumi.doubleValue() == 38.0D);
        assertTrue(sumf.doubleValue() == 26.0D);
        assertTrue(mini.doubleValue() == 3.0D);
        assertTrue(minf.doubleValue() == 3.0D);
        assertTrue(maxi.doubleValue() == 13.0D);
        assertTrue(maxf.doubleValue() == 9.0D);
        assertTrue(avgi.doubleValue() == 9.5D);
        assertTrue(avgf.doubleValue() == 6.5D);
        assertTrue(count.doubleValue() == 4);

        tuple = tuples.get(2);
        bucket = tuple.getString("a_s");
        sumi = tuple.getDouble("sum(a_i)");
        sumf = tuple.getDouble("sum(a_f)");
        mini = tuple.getDouble("min(a_i)");
        minf = tuple.getDouble("min(a_f)");
        maxi = tuple.getDouble("max(a_i)");
        maxf = tuple.getDouble("max(a_f)");
        avgi = tuple.getDouble("avg(a_i)");
        avgf = tuple.getDouble("avg(a_f)");
        count = tuple.getDouble("count(*)");

        assertTrue(bucket.equals("hello4"));
        assertTrue(sumi.longValue() == 15);
        assertTrue(sumf.doubleValue() == 11.0D);
        assertTrue(mini.doubleValue() == 4.0D);
        assertTrue(minf.doubleValue() == 4.0D);
        assertTrue(maxi.doubleValue() == 11.0D);
        assertTrue(maxf.doubleValue() == 7.0D);
        assertTrue(avgi.doubleValue() == 7.5D);
        assertTrue(avgf.doubleValue() == 5.5D);
        assertTrue(count.doubleValue() == 2);

    } finally {
        solrClientCache.close();
    }
}

From source file:org.apache.solr.client.solrj.io.stream.StreamExpressionTest.java

@Test
public void testParallelRollupStream() throws Exception {

    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1")
            .add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2")
            .add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3")
            .add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4")
            .add(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5")
            .add(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6")
            .add(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7")
            .add(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8")
            .add(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9")
            .add(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10")
            .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

    StreamFactory factory = new StreamFactory()
            .withCollectionZkHost(COLLECTIONORALIAS, cluster.getZkServer().getZkAddress())
            .withFunctionName("search", CloudSolrStream.class)
            .withFunctionName("parallel", ParallelStream.class).withFunctionName("rollup", RollupStream.class)
            .withFunctionName("sum", SumMetric.class).withFunctionName("min", MinMetric.class)
            .withFunctionName("max", MaxMetric.class).withFunctionName("avg", MeanMetric.class)
            .withFunctionName("count", CountMetric.class);

    StreamContext streamContext = new StreamContext();
    SolrClientCache solrClientCache = new SolrClientCache();
    streamContext.setSolrClientCache(solrClientCache);

    StreamExpression expression;/*from  w  w w.ja  v  a 2  s  .co  m*/
    TupleStream stream;
    List<Tuple> tuples;

    try {
        expression = StreamExpressionParser.parse("parallel(" + COLLECTIONORALIAS + "," + "rollup(" + "search("
                + COLLECTIONORALIAS + ", q=*:*, fl=\"a_s,a_i,a_f\", sort=\"a_s asc\", partitionKeys=\"a_s\"),"
                + "over=\"a_s\"," + "sum(a_i)," + "sum(a_f)," + "min(a_i)," + "min(a_f)," + "max(a_i),"
                + "max(a_f)," + "avg(a_i)," + "avg(a_f)," + "count(*)" + ")," + "workers=\"2\", zkHost=\""
                + cluster.getZkServer().getZkAddress() + "\", sort=\"a_s asc\")");

        stream = factory.constructStream(expression);
        stream.setStreamContext(streamContext);
        tuples = getTuples(stream);

        assert (tuples.size() == 3);

        //Test Long and Double Sums

        Tuple tuple = tuples.get(0);
        String bucket = tuple.getString("a_s");
        Double sumi = tuple.getDouble("sum(a_i)");
        Double sumf = tuple.getDouble("sum(a_f)");
        Double mini = tuple.getDouble("min(a_i)");
        Double minf = tuple.getDouble("min(a_f)");
        Double maxi = tuple.getDouble("max(a_i)");
        Double maxf = tuple.getDouble("max(a_f)");
        Double avgi = tuple.getDouble("avg(a_i)");
        Double avgf = tuple.getDouble("avg(a_f)");
        Double count = tuple.getDouble("count(*)");

        assertTrue(bucket.equals("hello0"));
        assertTrue(sumi.doubleValue() == 17.0D);
        assertTrue(sumf.doubleValue() == 18.0D);
        assertTrue(mini.doubleValue() == 0.0D);
        assertTrue(minf.doubleValue() == 1.0D);
        assertTrue(maxi.doubleValue() == 14.0D);
        assertTrue(maxf.doubleValue() == 10.0D);
        assertTrue(avgi.doubleValue() == 4.25D);
        assertTrue(avgf.doubleValue() == 4.5D);
        assertTrue(count.doubleValue() == 4);

        tuple = tuples.get(1);
        bucket = tuple.getString("a_s");
        sumi = tuple.getDouble("sum(a_i)");
        sumf = tuple.getDouble("sum(a_f)");
        mini = tuple.getDouble("min(a_i)");
        minf = tuple.getDouble("min(a_f)");
        maxi = tuple.getDouble("max(a_i)");
        maxf = tuple.getDouble("max(a_f)");
        avgi = tuple.getDouble("avg(a_i)");
        avgf = tuple.getDouble("avg(a_f)");
        count = tuple.getDouble("count(*)");

        assertTrue(bucket.equals("hello3"));
        assertTrue(sumi.doubleValue() == 38.0D);
        assertTrue(sumf.doubleValue() == 26.0D);
        assertTrue(mini.doubleValue() == 3.0D);
        assertTrue(minf.doubleValue() == 3.0D);
        assertTrue(maxi.doubleValue() == 13.0D);
        assertTrue(maxf.doubleValue() == 9.0D);
        assertTrue(avgi.doubleValue() == 9.5D);
        assertTrue(avgf.doubleValue() == 6.5D);
        assertTrue(count.doubleValue() == 4);

        tuple = tuples.get(2);
        bucket = tuple.getString("a_s");
        sumi = tuple.getDouble("sum(a_i)");
        sumf = tuple.getDouble("sum(a_f)");
        mini = tuple.getDouble("min(a_i)");
        minf = tuple.getDouble("min(a_f)");
        maxi = tuple.getDouble("max(a_i)");
        maxf = tuple.getDouble("max(a_f)");
        avgi = tuple.getDouble("avg(a_i)");
        avgf = tuple.getDouble("avg(a_f)");
        count = tuple.getDouble("count(*)");

        assertTrue(bucket.equals("hello4"));
        assertTrue(sumi.longValue() == 15);
        assertTrue(sumf.doubleValue() == 11.0D);
        assertTrue(mini.doubleValue() == 4.0D);
        assertTrue(minf.doubleValue() == 4.0D);
        assertTrue(maxi.doubleValue() == 11.0D);
        assertTrue(maxf.doubleValue() == 7.0D);
        assertTrue(avgi.doubleValue() == 7.5D);
        assertTrue(avgf.doubleValue() == 5.5D);
        assertTrue(count.doubleValue() == 2);
    } finally {
        solrClientCache.close();
    }
}

From source file:org.apache.solr.client.solrj.io.stream.StreamExpressionTest.java

@Test
public void testFacetStream() throws Exception {

    new UpdateRequest().add(id, "0", "a_s", "hello0", "a_i", "0", "a_f", "1")
            .add(id, "2", "a_s", "hello0", "a_i", "2", "a_f", "2")
            .add(id, "3", "a_s", "hello3", "a_i", "3", "a_f", "3")
            .add(id, "4", "a_s", "hello4", "a_i", "4", "a_f", "4")
            .add(id, "1", "a_s", "hello0", "a_i", "1", "a_f", "5")
            .add(id, "5", "a_s", "hello3", "a_i", "10", "a_f", "6")
            .add(id, "6", "a_s", "hello4", "a_i", "11", "a_f", "7")
            .add(id, "7", "a_s", "hello3", "a_i", "12", "a_f", "8")
            .add(id, "8", "a_s", "hello3", "a_i", "13", "a_f", "9")
            .add(id, "9", "a_s", "hello0", "a_i", "14", "a_f", "10")
            .commit(cluster.getSolrClient(), COLLECTIONORALIAS);

    String clause;/*from  w ww . j ava  2s. c o m*/
    TupleStream stream;
    List<Tuple> tuples;

    StreamFactory factory = new StreamFactory()
            .withCollectionZkHost("collection1", cluster.getZkServer().getZkAddress())
            .withFunctionName("facet", FacetStream.class).withFunctionName("sum", SumMetric.class)
            .withFunctionName("min", MinMetric.class).withFunctionName("max", MaxMetric.class)
            .withFunctionName("avg", MeanMetric.class).withFunctionName("count", CountMetric.class);

    // Basic test
    clause = "facet(" + "collection1, " + "q=\"*:*\", " + "fl=\"a_s,a_i,a_f\", " + "sort=\"a_s asc\", "
            + "buckets=\"a_s\", " + "bucketSorts=\"sum(a_i) asc\", " + "bucketSizeLimit=100, "
            + "sum(a_i), sum(a_f), " + "min(a_i), min(a_f), " + "max(a_i), max(a_f), " + "avg(a_i), avg(a_f), "
            + "count(*)" + ")";

    stream = factory.constructStream(clause);
    tuples = getTuples(stream);

    assert (tuples.size() == 3);

    //Test Long and Double Sums

    Tuple tuple = tuples.get(0);
    String bucket = tuple.getString("a_s");
    Double sumi = tuple.getDouble("sum(a_i)");
    Double sumf = tuple.getDouble("sum(a_f)");
    Double mini = tuple.getDouble("min(a_i)");
    Double minf = tuple.getDouble("min(a_f)");
    Double maxi = tuple.getDouble("max(a_i)");
    Double maxf = tuple.getDouble("max(a_f)");
    Double avgi = tuple.getDouble("avg(a_i)");
    Double avgf = tuple.getDouble("avg(a_f)");
    Double count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello4"));
    assertTrue(sumi.longValue() == 15);
    assertTrue(sumf.doubleValue() == 11.0D);
    assertTrue(mini.doubleValue() == 4.0D);
    assertTrue(minf.doubleValue() == 4.0D);
    assertTrue(maxi.doubleValue() == 11.0D);
    assertTrue(maxf.doubleValue() == 7.0D);
    assertTrue(avgi.doubleValue() == 7.5D);
    assertTrue(avgf.doubleValue() == 5.5D);
    assertTrue(count.doubleValue() == 2);

    tuple = tuples.get(1);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello0"));
    assertTrue(sumi.doubleValue() == 17.0D);
    assertTrue(sumf.doubleValue() == 18.0D);
    assertTrue(mini.doubleValue() == 0.0D);
    assertTrue(minf.doubleValue() == 1.0D);
    assertTrue(maxi.doubleValue() == 14.0D);
    assertTrue(maxf.doubleValue() == 10.0D);
    assertTrue(avgi.doubleValue() == 4.25D);
    assertTrue(avgf.doubleValue() == 4.5D);
    assertTrue(count.doubleValue() == 4);

    tuple = tuples.get(2);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello3"));
    assertTrue(sumi.doubleValue() == 38.0D);
    assertTrue(sumf.doubleValue() == 26.0D);
    assertTrue(mini.doubleValue() == 3.0D);
    assertTrue(minf.doubleValue() == 3.0D);
    assertTrue(maxi.doubleValue() == 13.0D);
    assertTrue(maxf.doubleValue() == 9.0D);
    assertTrue(avgi.doubleValue() == 9.5D);
    assertTrue(avgf.doubleValue() == 6.5D);
    assertTrue(count.doubleValue() == 4);

    //Reverse the Sort.

    clause = "facet(" + "collection1, " + "q=\"*:*\", " + "fl=\"a_s,a_i,a_f\", " + "sort=\"a_s asc\", "
            + "buckets=\"a_s\", " + "bucketSorts=\"sum(a_i) desc\", " + "bucketSizeLimit=100, "
            + "sum(a_i), sum(a_f), " + "min(a_i), min(a_f), " + "max(a_i), max(a_f), " + "avg(a_i), avg(a_f), "
            + "count(*)" + ")";

    stream = factory.constructStream(clause);
    tuples = getTuples(stream);

    //Test Long and Double Sums

    tuple = tuples.get(0);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello3"));
    assertTrue(sumi.doubleValue() == 38.0D);
    assertTrue(sumf.doubleValue() == 26.0D);
    assertTrue(mini.doubleValue() == 3.0D);
    assertTrue(minf.doubleValue() == 3.0D);
    assertTrue(maxi.doubleValue() == 13.0D);
    assertTrue(maxf.doubleValue() == 9.0D);
    assertTrue(avgi.doubleValue() == 9.5D);
    assertTrue(avgf.doubleValue() == 6.5D);
    assertTrue(count.doubleValue() == 4);

    tuple = tuples.get(1);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello0"));
    assertTrue(sumi.doubleValue() == 17.0D);
    assertTrue(sumf.doubleValue() == 18.0D);
    assertTrue(mini.doubleValue() == 0.0D);
    assertTrue(minf.doubleValue() == 1.0D);
    assertTrue(maxi.doubleValue() == 14.0D);
    assertTrue(maxf.doubleValue() == 10.0D);
    assertTrue(avgi.doubleValue() == 4.25D);
    assertTrue(avgf.doubleValue() == 4.5D);
    assertTrue(count.doubleValue() == 4);

    tuple = tuples.get(2);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello4"));
    assertTrue(sumi.longValue() == 15);
    assertTrue(sumf.doubleValue() == 11.0D);
    assertTrue(mini.doubleValue() == 4.0D);
    assertTrue(minf.doubleValue() == 4.0D);
    assertTrue(maxi.doubleValue() == 11.0D);
    assertTrue(maxf.doubleValue() == 7.0D);
    assertTrue(avgi.doubleValue() == 7.5D);
    assertTrue(avgf.doubleValue() == 5.5D);
    assertTrue(count.doubleValue() == 2);

    //Test index sort
    clause = "facet(" + "collection1, " + "q=\"*:*\", " + "fl=\"a_s,a_i,a_f\", " + "sort=\"a_s asc\", "
            + "buckets=\"a_s\", " + "bucketSorts=\"a_s desc\", " + "bucketSizeLimit=100, "
            + "sum(a_i), sum(a_f), " + "min(a_i), min(a_f), " + "max(a_i), max(a_f), " + "avg(a_i), avg(a_f), "
            + "count(*)" + ")";

    stream = factory.constructStream(clause);
    tuples = getTuples(stream);

    assert (tuples.size() == 3);

    tuple = tuples.get(0);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello4"));
    assertTrue(sumi.longValue() == 15);
    assertTrue(sumf.doubleValue() == 11.0D);
    assertTrue(mini.doubleValue() == 4.0D);
    assertTrue(minf.doubleValue() == 4.0D);
    assertTrue(maxi.doubleValue() == 11.0D);
    assertTrue(maxf.doubleValue() == 7.0D);
    assertTrue(avgi.doubleValue() == 7.5D);
    assertTrue(avgf.doubleValue() == 5.5D);
    assertTrue(count.doubleValue() == 2);

    tuple = tuples.get(1);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello3"));
    assertTrue(sumi.doubleValue() == 38.0D);
    assertTrue(sumf.doubleValue() == 26.0D);
    assertTrue(mini.doubleValue() == 3.0D);
    assertTrue(minf.doubleValue() == 3.0D);
    assertTrue(maxi.doubleValue() == 13.0D);
    assertTrue(maxf.doubleValue() == 9.0D);
    assertTrue(avgi.doubleValue() == 9.5D);
    assertTrue(avgf.doubleValue() == 6.5D);
    assertTrue(count.doubleValue() == 4);

    tuple = tuples.get(2);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello0"));
    assertTrue(sumi.doubleValue() == 17.0D);
    assertTrue(sumf.doubleValue() == 18.0D);
    assertTrue(mini.doubleValue() == 0.0D);
    assertTrue(minf.doubleValue() == 1.0D);
    assertTrue(maxi.doubleValue() == 14.0D);
    assertTrue(maxf.doubleValue() == 10.0D);
    assertTrue(avgi.doubleValue() == 4.25D);
    assertTrue(avgf.doubleValue() == 4.5D);
    assertTrue(count.doubleValue() == 4);

    //Test index sort

    clause = "facet(" + "collection1, " + "q=\"*:*\", " + "fl=\"a_s,a_i,a_f\", " + "sort=\"a_s asc\", "
            + "buckets=\"a_s\", " + "bucketSorts=\"a_s asc\", " + "bucketSizeLimit=100, "
            + "sum(a_i), sum(a_f), " + "min(a_i), min(a_f), " + "max(a_i), max(a_f), " + "avg(a_i), avg(a_f), "
            + "count(*)" + ")";

    stream = factory.constructStream(clause);
    tuples = getTuples(stream);

    assert (tuples.size() == 3);

    tuple = tuples.get(0);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello0"));
    assertTrue(sumi.doubleValue() == 17.0D);
    assertTrue(sumf.doubleValue() == 18.0D);
    assertTrue(mini.doubleValue() == 0.0D);
    assertTrue(minf.doubleValue() == 1.0D);
    assertTrue(maxi.doubleValue() == 14.0D);
    assertTrue(maxf.doubleValue() == 10.0D);
    assertTrue(avgi.doubleValue() == 4.25D);
    assertTrue(avgf.doubleValue() == 4.5D);
    assertTrue(count.doubleValue() == 4);

    tuple = tuples.get(1);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello3"));
    assertTrue(sumi.doubleValue() == 38.0D);
    assertTrue(sumf.doubleValue() == 26.0D);
    assertTrue(mini.doubleValue() == 3.0D);
    assertTrue(minf.doubleValue() == 3.0D);
    assertTrue(maxi.doubleValue() == 13.0D);
    assertTrue(maxf.doubleValue() == 9.0D);
    assertTrue(avgi.doubleValue() == 9.5D);
    assertTrue(avgf.doubleValue() == 6.5D);
    assertTrue(count.doubleValue() == 4);

    tuple = tuples.get(2);
    bucket = tuple.getString("a_s");
    sumi = tuple.getDouble("sum(a_i)");
    sumf = tuple.getDouble("sum(a_f)");
    mini = tuple.getDouble("min(a_i)");
    minf = tuple.getDouble("min(a_f)");
    maxi = tuple.getDouble("max(a_i)");
    maxf = tuple.getDouble("max(a_f)");
    avgi = tuple.getDouble("avg(a_i)");
    avgf = tuple.getDouble("avg(a_f)");
    count = tuple.getDouble("count(*)");

    assertTrue(bucket.equals("hello4"));
    assertTrue(sumi.longValue() == 15);
    assertTrue(sumf.doubleValue() == 11.0D);
    assertTrue(mini.doubleValue() == 4.0D);
    assertTrue(minf.doubleValue() == 4.0D);
    assertTrue(maxi.doubleValue() == 11.0D);
    assertTrue(maxf.doubleValue() == 7.0D);
    assertTrue(avgi.doubleValue() == 7.5D);
    assertTrue(avgf.doubleValue() == 5.5D);
    assertTrue(count.doubleValue() == 2);

    //Test zero result facets
    clause = "facet(" + "collection1, " + "q=\"blahhh\", " + "fl=\"a_s,a_i,a_f\", " + "sort=\"a_s asc\", "
            + "buckets=\"a_s\", " + "bucketSorts=\"a_s asc\", " + "bucketSizeLimit=100, "
            + "sum(a_i), sum(a_f), " + "min(a_i), min(a_f), " + "max(a_i), max(a_f), " + "avg(a_i), avg(a_f), "
            + "count(*)" + ")";

    stream = factory.constructStream(clause);
    tuples = getTuples(stream);

    assert (tuples.size() == 0);

}

From source file:org.sakaiproject.tool.messageforums.DiscussionForumTool.java

public String processDfGradeSubmit() {
    GradebookService gradebookService = getGradebookService();
    if (gradebookService == null) {
        //      Maybe print an error message if it's possible to get into this state
        //      setErrorMessage(getResourceBundleString(STATE_INCONSISTENT));
        return null;
    }/*  w  ww .  j a v  a2 s .c o  m*/
    if (selectedMessageCount != 0) {
        setErrorMessage(getResourceBundleString(STATE_INCONSISTENT));
        return null;
    }
    selectedMessageCount = 0;

    gbItemScore = gradePoint;
    gbItemComment = gradeComment;
    if (selectedAssign == null || selectedAssign.trim().length() == 0
            || DEFAULT_GB_ITEM.equalsIgnoreCase(selectedAssign)) {
        setErrorMessage(getResourceBundleString(NO_ASSGN));
        return null;
    }

    if (gradePoint == null || gradePoint.trim().length() == 0) {
        setErrorMessage(getResourceBundleString(NO_GRADE_PTS));
        return null;
    }

    if (!validateGradeInput())
        return null;

    NumberFormat nf = DecimalFormat.getInstance(new ResourceLoader().getLocale());
    Double gradeAsDouble = null;
    double pointsPossibleAsDouble = 0.0;
    try {
        gradeAsDouble = new Double(nf.parse(gradePoint).doubleValue());
    } catch (ParseException pe) {
        // we shouldn't get here if the validation above is working properly
        LOG.warn("Error converting grade " + gradePoint + " to Double");
        return null;
    }

    if (gradeByPoints) {
        try {
            pointsPossibleAsDouble = nf.parse(gbItemPointsPossible).doubleValue();
            if ((gradeAsDouble.doubleValue() > pointsPossibleAsDouble) && !grade_too_large_make_sure) {
                setErrorMessage(getResourceBundleString(TOO_LARGE_GRADE));
                grade_too_large_make_sure = true;
                return null;
            } else {
                LOG.info("the user confirms he wants to give student higher grade");
            }
        } catch (ParseException e) {
            LOG.warn("Unable to parse points possible " + gbItemPointsPossible
                    + " to determine if entered grade is greater than points possible");
        }
    }
    String studentUid = null;
    try {
        String selectedAssignName = ((SelectItem) assignments.get((Integer.valueOf(selectedAssign)).intValue()))
                .getLabel();
        String gradebookUuid = ToolManager.getCurrentPlacement().getContext();
        if (selectedMessage == null && selectedGradedUserId != null && !"".equals(selectedGradedUserId)) {
            studentUid = selectedGradedUserId;
        } else {
            studentUid = UserDirectoryService.getUser(selectedMessage.getMessage().getCreatedBy()).getId();
        }

        Long gbItemId = gradebookService.getAssignment(gradebookUuid, selectedAssignName).getId();
        gradebookService.saveGradeAndCommentForStudent(gradebookUuid, gbItemId, studentUid, gradePoint,
                gradeComment);

        if (selectedMessage != null) {
            Message msg = selectedMessage.getMessage();
            msg.setGradeAssignmentName(selectedAssignName);
            msg.setTopic(
                    (DiscussionTopic) forumManager.getTopicByIdWithMessages(selectedTopic.getTopic().getId()));
            forumManager.saveMessage(msg, false);
        }

        setSuccessMessage(getResourceBundleString(GRADE_SUCCESSFUL));
    } catch (SecurityException se) {
        LOG.error("Security Exception - processDfGradeSubmit:" + se);
        setErrorMessage(getResourceBundleString("cdfm_no_gb_perm"));
    } catch (Exception e) {
        LOG.error("DiscussionForumTool - processDfGradeSubmit:" + e);
        e.printStackTrace();
    }

    String eventRef = "";
    if (selectedMessage != null) {
        eventRef = getEventReference(selectedMessage.getMessage());
    } else if (selectedTopic != null) {
        eventRef = getEventReference(selectedTopic.getTopic());
    } else if (selectedForum != null) {
        eventRef = getEventReference(selectedForum.getForum());
    }
    LearningResourceStoreService lrss = (LearningResourceStoreService) ComponentManager
            .get("org.sakaiproject.event.api.LearningResourceStoreService");
    Event event = EventTrackingService.newEvent(DiscussionForumService.EVENT_FORUMS_GRADE, eventRef, true);
    EventTrackingService.post(event);
    if (null != lrss) {
        try {
            lrss.registerStatement(getStatementForGrade(studentUid, lrss.getEventActor(event),
                    selectedTopic.getTopic().getTitle(), gradeAsDouble), "msgcntr");
        } catch (Exception e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(e);
            }
        }
    }

    gradeNotify = false;
    selectedAssign = DEFAULT_GB_ITEM;
    resetGradeInfo();
    getThreadFromMessage();
    return MESSAGE_VIEW;
}

From source file:com.clark.func.Functions.java

/**
 * <p>/*from ww  w  . ja  v  a  2s .co  m*/
 * Turns a string value into a java.lang.Number.
 * </p>
 * 
 * <p>
 * First, the value is examined for a type qualifier on the end (
 * <code>'f','F','d','D','l','L'</code>). If it is found, it starts trying
 * to create successively larger types from the type specified until one is
 * found that can represent the value.
 * </p>
 * 
 * <p>
 * If a type specifier is not found, it will check for a decimal point and
 * then try successively larger types from <code>Integer</code> to
 * <code>BigInteger</code> and from <code>Float</code> to
 * <code>BigDecimal</code>.
 * </p>
 * 
 * <p>
 * If the string starts with <code>0x</code> or <code>-0x</code>, it will be
 * interpreted as a hexadecimal integer. Values with leading <code>0</code>
 * 's will not be interpreted as octal.
 * </p>
 * 
 * <p>
 * Returns <code>null</code> if the string is <code>null</code>.
 * </p>
 * 
 * <p>
 * This method does not trim the input string, i.e., strings with leading or
 * trailing spaces will generate NumberFormatExceptions.
 * </p>
 * 
 * @param str
 *            String containing a number, may be null
 * @return Number created from the string
 * @throws NumberFormatException
 *             if the value cannot be converted
 */
public static Number createNumber(String str) throws NumberFormatException {
    if (str == null) {
        return null;
    }
    if (isBlank(str)) {
        throw new NumberFormatException("A blank string is not a valid number");
    }
    if (str.startsWith("--")) {
        // this is protection for poorness in java.lang.BigDecimal.
        // it accepts this as a legal value, but it does not appear
        // to be in specification of class. OS X Java parses it to
        // a wrong value.
        return null;
    }
    if (str.startsWith("0x") || str.startsWith("-0x")) {
        return createInteger(str);
    }
    char lastChar = str.charAt(str.length() - 1);
    String mant;
    String dec;
    String exp;
    int decPos = str.indexOf('.');
    int expPos = str.indexOf('e') + str.indexOf('E') + 1;

    if (decPos > -1) {

        if (expPos > -1) {
            if (expPos < decPos) {
                throw new NumberFormatException(str + " is not a valid number.");
            }
            dec = str.substring(decPos + 1, expPos);
        } else {
            dec = str.substring(decPos + 1);
        }
        mant = str.substring(0, decPos);
    } else {
        if (expPos > -1) {
            mant = str.substring(0, expPos);
        } else {
            mant = str;
        }
        dec = null;
    }
    if (!Character.isDigit(lastChar) && lastChar != '.') {
        if (expPos > -1 && expPos < str.length() - 1) {
            exp = str.substring(expPos + 1, str.length() - 1);
        } else {
            exp = null;
        }
        // Requesting a specific type..
        String numeric = str.substring(0, str.length() - 1);
        boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
        switch (lastChar) {
        case 'l':
        case 'L':
            if (dec == null && exp == null
                    && (numeric.charAt(0) == '-' && isDigits(numeric.substring(1)) || isDigits(numeric))) {
                try {
                    return createLong(numeric);
                } catch (NumberFormatException nfe) {
                    // Too big for a long
                }
                return createBigInteger(numeric);

            }
            throw new NumberFormatException(str + " is not a valid number.");
        case 'f':
        case 'F':
            try {
                Float f = createFloat(numeric);
                if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                    // If it's too big for a float or the float value =
                    // 0
                    // and the string
                    // has non-zeros in it, then float does not have the
                    // precision we want
                    return f;
                }

            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            //$FALL-THROUGH$
        case 'd':
        case 'D':
            try {
                Double d = createDouble(numeric);
                if (!(d.isInfinite() || (d.floatValue() == 0.0D && !allZeros))) {
                    return d;
                }
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            try {
                return createBigDecimal(numeric);
            } catch (NumberFormatException e) {
                // ignore the bad number
            }
            //$FALL-THROUGH$
        default:
            throw new NumberFormatException(str + " is not a valid number.");

        }
    } else {
        // User doesn't have a preference on the return type, so let's start
        // small and go from there...
        if (expPos > -1 && expPos < str.length() - 1) {
            exp = str.substring(expPos + 1, str.length());
        } else {
            exp = null;
        }
        if (dec == null && exp == null) {
            // Must be an int,long,bigint
            try {
                return createInteger(str);
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            try {
                return createLong(str);
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            return createBigInteger(str);

        } else {
            // Must be a float,double,BigDec
            boolean allZeros = isAllZeros(mant) && isAllZeros(exp);
            try {
                Float f = createFloat(str);
                if (!(f.isInfinite() || (f.floatValue() == 0.0F && !allZeros))) {
                    return f;
                }
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }
            try {
                Double d = createDouble(str);
                if (!(d.isInfinite() || (d.doubleValue() == 0.0D && !allZeros))) {
                    return d;
                }
            } catch (NumberFormatException nfe) {
                // ignore the bad number
            }

            return createBigDecimal(str);

        }
    }
}