Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Sum function for grouping collection #244

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

Support for Sum function for grouping collection #244

valadan opened this issue Oct 5, 2012 · 5 comments

Comments

@valadan
Copy link

@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
Copy link
Member

@timowest timowest commented Oct 6, 2012

I added now both sum and avg.

@valadan
Copy link
Author

@valadan valadan commented Oct 7, 2012

Thanks

@valadan valadan closed this Oct 7, 2012
@timowest
Copy link
Member

@timowest timowest commented Oct 8, 2012

I'll close it when it's released

@timowest timowest reopened this Oct 8, 2012
@valadan
Copy link
Author

@valadan valadan commented Oct 12, 2012

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

@timowest
Copy link
Member

@timowest 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
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants