KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > lib > stats > Stats


1 /*
2   Copyright (C) 2002 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.lib.stats;
19
20 import java.util.Collection JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import org.apache.log4j.Logger;
23 import org.objectweb.jac.core.rtti.FieldItem;
24
25 /**
26  * This class contains static methods to do statistical computations
27  * on collections.
28  */

29 public class Stats {
30     static Logger logger = Logger.getLogger("stats");
31
32     /**
33      * Compute average, min and max of a the field of collection's items
34      *
35      * @param stats store result in this structure. Values are not reset to zero.
36      * @param items the items to compute the stats on
37      * @param field the numerical field (float, double or int) to compute the stats of
38      * @return an object containing the average, max and min of field
39      * in items. If there's no item in the collection all values are 0.0
40      */

41     public static void computeStats(Stat stats, Collection JavaDoc items, FieldItem field) {
42         double average = 0;
43         double sum = 0;
44         double max = 0;
45         double min = 0;
46         long count = 0;
47         long pos = 0;
48         Iterator JavaDoc it = items.iterator();
49         while (it.hasNext()) {
50             Object JavaDoc item = it.next();
51             if (item!=null) {
52                 double value = ((Number JavaDoc)field.getThroughAccessor(item)).doubleValue();
53                 stats.sum += value;
54                 if (count==0 || value<stats.min)
55                     stats.min = value;
56                 if (count==0 || value>stats.max)
57                     stats.max = value;
58                 stats.count++;
59             } else {
60                 logger.error("computeStats "+field+": null element in collection at position "+pos);
61             }
62             pos++;
63         }
64     }
65
66     public static Stat computeStats(Collection JavaDoc items, FieldItem field) {
67         Stat stat = new Stat();
68         computeStats(stat,items,field);
69         return stat;
70     }
71 }
72
Popular Tags