001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.metrics;
034:
035: import java.util.*;
036:
037: public class MetricsComparator implements Comparator<Metrics> {
038: public static final int DESCENDING = -1;
039: public static final int ASCENDING = 1;
040:
041: private String name;
042: private int direction;
043: private int dispose;
044:
045: public MetricsComparator(String name) {
046: this (name, StatisticalMeasurement.DISPOSE_IGNORE);
047: }
048:
049: public MetricsComparator(String name, int dispose) {
050: setName(name);
051: setDispose(dispose);
052: setDirection(ASCENDING);
053: }
054:
055: public String getName() {
056: return name;
057: }
058:
059: public void setName(String name) {
060: this .name = name;
061: }
062:
063: public int getDirection() {
064: return direction;
065: }
066:
067: public void setDirection(int direction) {
068: this .direction = direction;
069: }
070:
071: public int getDispose() {
072: return dispose;
073: }
074:
075: public void setDispose(int dispose) {
076: this .dispose = dispose;
077: }
078:
079: public void sortOn(String name, int dispose) {
080: if (name.equals(this .name) && dispose == this .dispose) {
081: reverse();
082: } else {
083: setName(name);
084: setDirection(ASCENDING);
085: setDispose(dispose);
086: }
087: }
088:
089: public void reverse() {
090: direction *= -1;
091: }
092:
093: public int compare(Metrics metrics1, Metrics metrics2) {
094: int result;
095:
096: if ("name".equals(name)) {
097: result = metrics1.getName().compareTo(metrics2.getName());
098: } else {
099: Measurement m1 = metrics1.getMeasurement(name);
100: Measurement m2 = metrics2.getMeasurement(name);
101:
102: if (m1 == null && m2 != null) {
103: result = -1;
104: } else if (m1 != null && m2 == null) {
105: result = 1;
106: } else if (m1 == m2) {
107: result = 0;
108: } else {
109: double v1 = extractValue(m1);
110: double v2 = extractValue(m2);
111:
112: if (Double.isNaN(v1) && !Double.isNaN(v2)) {
113: result = 1 * getDirection();
114: } else if (!Double.isNaN(v1) && Double.isNaN(v2)) {
115: result = -1 * getDirection();
116: } else if (Double.isNaN(v1) && Double.isNaN(v2)) {
117: result = 0;
118: } else if (v1 < v2) {
119: result = -1;
120: } else if (v1 > v2) {
121: result = 1;
122: } else {
123: result = 0;
124: }
125: }
126: }
127:
128: result *= getDirection();
129:
130: return result;
131: }
132:
133: private double extractValue(Measurement m) {
134: double result = Double.NaN;
135:
136: if (m instanceof StatisticalMeasurement) {
137: StatisticalMeasurement sm = (StatisticalMeasurement) m;
138: switch (getDispose()) {
139: case StatisticalMeasurement.DISPOSE_MINIMUM:
140: result = sm.getMinimum();
141: break;
142:
143: case StatisticalMeasurement.DISPOSE_MEDIAN:
144: result = sm.getMedian();
145: break;
146:
147: case StatisticalMeasurement.DISPOSE_AVERAGE:
148: result = sm.getAverage();
149: break;
150:
151: case StatisticalMeasurement.DISPOSE_STANDARD_DEVIATION:
152: result = sm.getStandardDeviation();
153: break;
154:
155: case StatisticalMeasurement.DISPOSE_MAXIMUM:
156: result = sm.getMaximum();
157: break;
158:
159: case StatisticalMeasurement.DISPOSE_SUM:
160: result = sm.getSum();
161: break;
162:
163: case StatisticalMeasurement.DISPOSE_NB_DATA_POINTS:
164: result = sm.getNbDataPoints();
165: break;
166:
167: default:
168: case StatisticalMeasurement.DISPOSE_IGNORE:
169: break;
170: }
171: } else {
172: result = m.doubleValue();
173: }
174:
175: return result;
176: }
177: }
|