Statistics
| Revision:

root / org.gvsig.legend.aggregate / trunk / org.gvsig.legend.aggregate / org.gvsig.legend.aggregate.lib / org.gvsig.legend.aggregate.lib.impl / src / main / java / org / gvsig / legend / aggregate / lib / impl / operation / SumOperation.java @ 1846

History | View | Annotate | Download (1.31 KB)

1
package org.gvsig.legend.aggregate.lib.impl.operation;
2

    
3
import java.text.MessageFormat;
4
import org.apache.commons.lang3.StringUtils;
5
import org.gvsig.fmap.dal.feature.Feature;
6
import org.gvsig.legend.aggregate.lib.api.AbstractOperation;
7

    
8

    
9
public class SumOperation extends AbstractOperation {
10
    int sum = 0;
11
    
12
    public SumOperation() {
13
        super("_Sum", "_Calculate_the_sum_of_the_selected_attribute_for_the_grouped_features");
14

    
15
    }
16

    
17
    @Override
18
    public boolean isAttributeRequiered() {
19
        return true;
20
    }
21

    
22
    @Override
23
    public boolean isAditionalValueRequiered() {
24
        return true;
25
    }
26
    
27
    @Override
28
    public void reset() {
29
        sum = 0;
30
    }
31
    
32
    @Override
33
    public void perform(Feature feature) {
34
        try {
35
            double x = feature.getDouble(this.getAttributeName());
36
            sum += x;
37
        } catch (Exception ex) {
38
        }
39
    }
40

    
41
    @Override
42
    public Object getValue() {
43
        return sum;
44
    }
45

    
46
    @Override
47
    public String format() {
48
        if( StringUtils.isEmpty(this.getAditionalValue()) ) {
49
            return super.format(); 
50
        }
51
        try {
52
            return MessageFormat.format("{0,number,"+this.getAditionalValue().trim()+"}", this.getValue());
53
        } catch(Exception ex) {
54
            return super.format(); 
55
        }
56
    }    
57
}