| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.loggers; |
| 27 |
|
| 28 |
import java.text.DecimalFormat; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public abstract class NumberColumn extends LogColumn.Abstract { |
| 40 |
|
| 41 |
private int sf = -1; |
| 42 |
private int dp = -1; |
| 43 |
|
| 44 |
private double upperCutoff; |
| 45 |
private double[] cutoffTable; |
| 46 |
private final DecimalFormat decimalFormat = new DecimalFormat(); |
| 47 |
private DecimalFormat scientificFormat = null; |
| 48 |
|
| 49 |
|
| 50 |
public NumberColumn(String label) { |
| 51 |
super(label); |
| 52 |
decimalFormat.setGroupingUsed(false); |
| 53 |
} |
| 54 |
|
| 55 |
public NumberColumn(String label, int sf) { |
| 56 |
this(label); |
| 57 |
setSignificantFigures(sf); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public void setSignificantFigures(int sf) { |
| 65 |
this.sf = sf; |
| 66 |
this.dp = -1; |
| 67 |
|
| 68 |
upperCutoff = Math.pow(10,sf-1); |
| 69 |
cutoffTable = new double[sf]; |
| 70 |
long num = 10; |
| 71 |
for (int i =0; i < cutoffTable.length; i++) { |
| 72 |
cutoffTable[i] = (double)num; |
| 73 |
num *= 10; |
| 74 |
} |
| 75 |
decimalFormat.setGroupingUsed(false); |
| 76 |
decimalFormat.setMinimumIntegerDigits(1); |
| 77 |
decimalFormat.setMaximumFractionDigits(sf-1); |
| 78 |
decimalFormat.setMinimumFractionDigits(sf-1); |
| 79 |
scientificFormat = new DecimalFormat(getPattern(sf)); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public int getSignificantFigures() { return sf; } |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public void setDecimalPlaces(int dp) { |
| 93 |
this.dp = dp; |
| 94 |
this.sf = -1; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public int getDecimalPlaces() { return dp; } |
| 102 |
|
| 103 |
|
| 104 |
public String formatValue(double value) { |
| 105 |
if (dp < 0 && sf < 0) { |
| 106 |
|
| 107 |
return Double.toString(value); |
| 108 |
} |
| 109 |
|
| 110 |
int numFractionDigits = 0; |
| 111 |
|
| 112 |
if (dp < 0) { |
| 113 |
|
| 114 |
double absValue = Math.abs(value); |
| 115 |
|
| 116 |
if ((absValue > upperCutoff) || (absValue < 0.1)) { |
| 117 |
|
| 118 |
return scientificFormat.format(value); |
| 119 |
|
| 120 |
} else { |
| 121 |
|
| 122 |
numFractionDigits = getNumFractionDigits(value); |
| 123 |
} |
| 124 |
|
| 125 |
} else { |
| 126 |
|
| 127 |
numFractionDigits = dp; |
| 128 |
} |
| 129 |
|
| 130 |
decimalFormat.setMaximumFractionDigits(numFractionDigits); |
| 131 |
decimalFormat.setMinimumFractionDigits(numFractionDigits); |
| 132 |
return decimalFormat.format(value); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
protected String getFormattedValue() { |
| 142 |
return formatValue(getDoubleValue()); |
| 143 |
} |
| 144 |
|
| 145 |
private int getNumFractionDigits(double value) { |
| 146 |
value = Math.abs(value); |
| 147 |
for (int i = 0; i < cutoffTable.length; i++) { |
| 148 |
if (value < cutoffTable[i]) return sf-i-1; |
| 149 |
} |
| 150 |
return sf - 1; |
| 151 |
} |
| 152 |
|
| 153 |
private String getPattern(int sf) { |
| 154 |
String pattern = "0."; |
| 155 |
for (int i =0; i < sf-1; i++) { |
| 156 |
pattern += "#"; |
| 157 |
} |
| 158 |
pattern += "E0"; |
| 159 |
return pattern; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public abstract double getDoubleValue(); |
| 166 |
|
| 167 |
} |