KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > metrics > MetricsComparator


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.metrics;
34
35 import java.util.*;
36
37 public class MetricsComparator implements Comparator {
38     public static final int DESCENDING = -1;
39     public static final int ASCENDING = 1;
40     
41     private String JavaDoc name;
42     private int direction;
43     private int dispose;
44
45     public MetricsComparator(String JavaDoc name) {
46         this(name, StatisticalMeasurement.DISPOSE_IGNORE);
47     }
48         
49     public MetricsComparator(String JavaDoc name, int dispose) {
50         setName(name);
51         setDispose(dispose);
52         setDirection(ASCENDING);
53     }
54
55     public String JavaDoc getName() {
56         return name;
57     }
58
59     public void setName(String JavaDoc name) {
60         this.name = name;
61     }
62
63     public int getDirection() {
64         return direction;
65     }
66
67     public void setDirection(int direction) {
68         this.direction = direction;
69     }
70
71     public int getDispose() {
72         return dispose;
73     }
74
75     public void setDispose(int dispose) {
76         this.dispose = dispose;
77     }
78
79     public void sortOn(String JavaDoc name, int dispose) {
80         if (name.equals(this.name) && dispose == this.dispose) {
81             reverse();
82         } else {
83             setName(name);
84             setDirection(ASCENDING);
85             setDispose(dispose);
86         }
87     }
88     
89     public void reverse() {
90         direction *= -1;
91     }
92     
93     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
94         int result;
95
96         Metrics metrics1 = (Metrics) o1;
97         Metrics metrics2 = (Metrics) o2;
98
99         if ("name".equals(name)) {
100             result = metrics1.getName().compareTo(metrics2.getName());
101         } else {
102             Measurement m1 = metrics1.getMeasurement(name);
103             Measurement m2 = metrics2.getMeasurement(name);
104             
105             if (m1 == null && m2 != null) {
106                 result = -1;
107             } else if (m1 != null && m2 == null) {
108                 result = 1;
109             } else if (m1 == m2) {
110                 result = 0;
111             } else {
112                 double v1 = extractValue(m1);
113                 double v2 = extractValue(m2);
114                 
115                 if (Double.isNaN(v1) && !Double.isNaN(v2)) {
116                     result = 1 * getDirection();
117                 } else if (!Double.isNaN(v1) && Double.isNaN(v2)) {
118                     result = -1 * getDirection();
119                 } else if (Double.isNaN(v1) && Double.isNaN(v2)) {
120                     result = 0;
121                 } else if (v1 < v2) {
122                     result = -1;
123                 } else if (v1 > v2) {
124                     result = 1;
125                 } else {
126                     result = 0;
127                 }
128             }
129         }
130         
131         result *= getDirection();
132
133         return result;
134     }
135
136     private double extractValue(Measurement m) {
137         double result = Double.NaN;
138
139         if (m instanceof StatisticalMeasurement) {
140             StatisticalMeasurement sm = (StatisticalMeasurement) m;
141             switch (getDispose()) {
142                 case StatisticalMeasurement.DISPOSE_MINIMUM:
143                     result = sm.getMinimum();
144                     break;
145                     
146                 case StatisticalMeasurement.DISPOSE_MEDIAN:
147                     result = sm.getMedian();
148                     break;
149                     
150                 case StatisticalMeasurement.DISPOSE_AVERAGE:
151                     result = sm.getAverage();
152                     break;
153                     
154                 case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION:
155                     result = sm.getStandardDeviation();
156                     break;
157                     
158                 case StatisticalMeasurement.DISPOSE_MAXIMUM:
159                     result = sm.getMaximum();
160                     break;
161                     
162                 case StatisticalMeasurement.DISPOSE_SUM:
163                     result = sm.getSum();
164                     break;
165                     
166                 case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS:
167                     result = sm.getNbDataPoints();
168                     break;
169
170                 default:
171                 case StatisticalMeasurement.DISPOSE_IGNORE:
172                     break;
173             }
174         } else {
175             result = m.doubleValue();
176         }
177         
178         return result;
179     }
180 }
181
Popular Tags