bobobear / lambdaj

Automatically exported from code.google.com/p/lambdaj
Apache License 2.0
0 stars 0 forks source link

sum throws ClassCastException when list is empty #54

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Just run the following snippet:

public class App
{
    public static void main(String[] args) {
        Long sum = sum(new ArrayList<TestObj>(), on(TestObj.class).getValue());
        System.out.println(sum);
    }

    public static class TestObj {
        final long value;
        public TestObj(long value) { this.value = value; }
        public long getValue() { return value; }
    }
}

What is the expected output? What do you see instead?
Expected: 
0

Actual:
Exception in thread "main" java.lang.ClassCastException: java.lang.Double 
cannot be cast to java.lang.Long

What version of the product are you using? On what operating system?
version 2.3.1

Please provide any additional information below.

When iterator is empty, default value Double(0.0) is returned. 

See LambdaJ.java (lines 541-545)
public static Number sum(Object iterable) {
    if (iterable instanceof Number) return (Number)iterable;
    Iterator<?> iterator = asIterator(iterable);
    return iterator.hasNext() ? aggregate(iterator, getSumAggregator(iterator.next())) : 0.0;
}

Also on Lambdaj.java (lines 556-548)
public static <T> T sum(Object iterable, T argument) {
    return (T)sum(convertIterator(iterable, new ArgumentConverter<Object, T>(argument)));
}
Here we are casting to T (Long) but return value will be Double(0.0)

Original issue reported on code.google.com by renato.c...@gmail.com on 15 Dec 2010 at 1:26

GoogleCodeExporter commented 9 years ago
Just saw that this bug seems to be fixed in the trunk. 

Please, disregard the issue.

Original comment by renato.c...@gmail.com on 15 Dec 2010 at 1:55

GoogleCodeExporter commented 9 years ago

Original comment by mario.fu...@gmail.com on 16 Dec 2010 at 5:16