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 / AvgOperation.java @ 1846

History | View | Annotate | Download (1.47 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
public class AvgOperation extends AbstractOperation {
9

    
10
    private double avg = 0;
11
    private int count = 0;
12

    
13
    public AvgOperation() {
14
        super(
15
            "_Average", 
16
            "_Calculate_the_average_of_the_selected_attribute_for_the_grouped_features"
17
        );
18
    }
19

    
20
    @Override
21
    public boolean isAttributeRequiered() {
22
        return true;
23
    }
24

    
25
    @Override
26
    public boolean isAditionalValueRequiered() {
27
        return true;
28
    }
29

    
30
    @Override
31
    public void reset() {
32
        avg = 0;
33
        count = 0;
34
    }
35

    
36
    @Override
37
    public void perform(Feature feature) {
38
            try {
39
                double value = feature.getDouble(this.getAttributeName());
40
                avg = ( avg * count + value ) / (count+1);
41
                count++;
42
            } catch(Exception ex) {
43
            }
44
    }
45

    
46
    @Override
47
    public Object getValue() {
48
        return this.avg;
49
    }
50

    
51
    @Override
52
    public String format() {
53
        if( StringUtils.isEmpty(this.getAditionalValue()) ) {
54
            return super.format(); 
55
        }
56
        try {
57
            return MessageFormat.format("{0,number,"+this.getAditionalValue()+"}", this.getValue());
58
        } catch(Exception ex) {
59
            return super.format(); 
60
        }
61
    }
62
}