Support for Sum function for grouping collection #244

Closed
valadan opened this Issue Oct 5, 2012 · 5 comments

Comments

Projects
None yet
2 participants
@valadan

valadan commented Oct 5, 2012

Something along the lines of the following would be great:

import java.math.BigDecimal;
import java.math.BigInteger;

import com.mysema.query.group.AbstractGroupExpression;
import com.mysema.query.group.GroupCollector;
import com.mysema.query.types.Expression;
import com.mysema.util.MathUtils;

public class GSum<T extends Number & Comparable<?>> extends AbstractGroupExpression<T, T> {

private static final long serialVersionUID = 3518868612387641383L;

@SuppressWarnings("unchecked")
public GSum(Expression<T> expr) {
    super((Class)expr.getType(), expr);
}

@Override
public GroupCollector<T, T> createGroupCollector() {
    final Class<?> clazz=this.getType();
    return new GroupCollector<T,T>(){
        BigDecimal sum=BigDecimal.ZERO;

        @Override
        public void add(T t) {
            sum=MathUtils.sum(sum,t);
        }

        @Override
        public T get() {
            return (T) cast(sum);
        }

        @SuppressWarnings("unchecked")
        private T cast(BigDecimal number){
            if(clazz.equals(Double.class)){
                return (T) (Double)number.doubleValue();
            } else if(clazz.equals(Float.class)){
                return (T) (Float)number.floatValue();
            } else if(clazz.equals(Integer.class)){
                return (T) (Integer)number.intValue();
            } else if(clazz.equals(Long.class)){
                return (T) (Long)number.longValue();
            } else if(clazz.equals(Short.class)){
                return (T) (Short)number.shortValue();
            } else if(clazz.equals(Byte.class)){
                return (T) (Byte)number.byteValue();
            } else if(clazz.equals(BigInteger.class)){
                return (T) (BigInteger)number.toBigInteger();
            } else if(clazz.equals(BigDecimal.class)){
                return (T) number;
            }
            else {
                throw new UnsupportedOperationException();
            }
        }
    };
}

}

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 6, 2012

Member

I added now both sum and avg.

Member

timowest commented Oct 6, 2012

I added now both sum and avg.

@valadan

This comment has been minimized.

Show comment
Hide comment

valadan commented Oct 7, 2012

Thanks

@valadan valadan closed this Oct 7, 2012

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 8, 2012

Member

I'll close it when it's released

Member

timowest commented Oct 8, 2012

I'll close it when it's released

@timowest timowest reopened this Oct 8, 2012

@valadan

This comment has been minimized.

Show comment
Hide comment
@valadan

valadan Oct 12, 2012

Is the release date known? Thanks again for the quick turnaround!

valadan commented Oct 12, 2012

Is the release date known? Thanks again for the quick turnaround!

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 19, 2012

Member

Released in 2.8.1

Member

timowest commented Oct 19, 2012

Released in 2.8.1

@timowest timowest closed this Oct 19, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment