01: package com.technoetic.xplanner.util;
02:
03: import java.util.Collection;
04: import java.util.Iterator;
05:
06: import org.apache.commons.beanutils.PropertyUtils;
07:
08: public class CollectionUtils {
09: public static double sum(Collection collection, DoubleFilter filter) {
10: if (collection == null || collection.isEmpty())
11: return 0.0;
12: double value = 0.0;
13: Iterator it = collection.iterator();
14: while (it.hasNext()) {
15: value += filter.filter(it.next());
16: }
17: return value;
18: }
19:
20: public interface DoubleFilter {
21: double filter(Object o);
22: }
23:
24: public static class DoublePropertyFilter implements DoubleFilter {
25: private String name;
26:
27: public DoublePropertyFilter(String name) {
28: this .name = name;
29: }
30:
31: public double filter(Object o) {
32: try {
33: return ((Double) PropertyUtils.getProperty(o, name))
34: .doubleValue();
35: } catch (Exception e) {
36: throw new RuntimeException(e);
37: }
38: }
39: }
40: }
|