1 11 package org.eclipse.test.internal.performance.data; 12 13 import java.text.NumberFormat ; 14 15 16 19 public class Unit { 20 21 public static final Unit SECOND= new Unit("s", "second", false); public static final Unit BYTE= new Unit("byte", "byte", true); public static final Unit CARDINAL= new Unit("", "", false); public static final Unit INVOCATION= new Unit("invoc.", "invocation", false); 26 private static final int T_DECIMAL= 1000; 27 private static final int T_BINARY= 1024; 28 29 34 private final String fShortName; 35 private final String fFullName; 36 private final boolean fIsBinary; 37 39 public Unit(String shortName, String fullName, boolean binary) { 40 fShortName= shortName; 41 fFullName= fullName; 42 fIsBinary= binary; 43 } 44 45 public String getShortName() { 46 return fShortName; 47 } 48 49 public String getFullName() { 50 return fFullName; 51 } 52 53 public String getDisplayValue1(long magnitudel, int multiplier) { 54 55 return getDisplayValue1(magnitudel / multiplier); 58 } 59 60 public String getDisplayValue1(double magnitude) { 61 62 if (this == SECOND) 63 return formatedTime((long) (magnitude*1000.0)); 64 return formatEng((long) (magnitude)); 65 66 94 } 95 96 public String toString() { 97 return "Unit [" + getShortName() + "]"; } 99 100 109 public static String formatedTime(long diff) { 110 if (diff < 0) 111 diff *= -1; 112 if (diff < 1000) 113 return String.valueOf(diff) + " ms"; 115 NumberFormat nf= NumberFormat.getInstance(); 116 nf.setMaximumFractionDigits(2); 117 double d = diff / 1000.0; 118 if (d < 60) 119 return nf.format(d) + " s"; 121 d = d / 60.0; 122 if (d < 60.0) 123 return nf.format(d) + " m"; 125 d = d / 60.0; 126 if (d < 24.0) 127 return nf.format(d) + " h"; 129 d = d / 24.0; 130 return nf.format(d) + " d"; } 132 133 141 public String formatEng(long n) { 142 int TSD= fIsBinary ? T_BINARY : T_DECIMAL; 143 if (n < TSD) 144 return String.valueOf(n); 145 double d = ((double)n) / TSD; 146 NumberFormat nf = NumberFormat.getInstance(); 147 nf.setMaximumFractionDigits(2); 148 if (d < TSD) 149 return nf.format(d) + "K"; 151 d = d / TSD; 152 if ( d < TSD) 153 return nf.format(d) + "M"; 155 d = d / TSD; 156 if ( d < TSD) 157 return nf.format(d) + "G"; 159 d = d / TSD; 160 return nf.format(d) + "T"; } 162 163 } 164 | Popular Tags |