1 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 name; 42 private int direction; 43 private int dispose; 44 45 public MetricsComparator(String name) { 46 this(name, StatisticalMeasurement.DISPOSE_IGNORE); 47 } 48 49 public MetricsComparator(String name, int dispose) { 50 setName(name); 51 setDispose(dispose); 52 setDirection(ASCENDING); 53 } 54 55 public String getName() { 56 return name; 57 } 58 59 public void setName(String 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 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 o1, Object 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 |