1 23 24 package info.monitorenter.util.units; 25 26 40 public abstract class AUnit extends Object { 41 42 43 protected int m_decimals = 2; 44 45 49 protected double m_factor; 50 51 55 protected AUnit m_nexLowerUnit; 56 57 61 protected AUnit m_nextHigherUnit; 62 63 68 protected String m_unitName; 69 70 79 protected AUnit() { 80 } 82 83 91 public int getDecimals() { 92 return this.m_decimals; 93 } 94 95 107 public double getFactor() { 108 return this.m_factor; 109 } 110 111 126 public String getLabel(final double value) { 127 return new StringBuffer ().append(round(value / this.m_factor)).append(" ").append( 128 this.m_unitName).toString(); 129 } 130 131 144 public AUnit getNexLowerUnit() { 145 return this.m_nexLowerUnit; 146 } 147 148 161 public AUnit getNextHigherUnit() { 162 return this.m_nextHigherUnit; 163 } 164 165 176 public String getUnitName() { 177 return this.m_unitName; 178 } 179 180 194 public double getValue(final double value) { 195 return round(value / this.m_factor); 196 } 197 198 206 private final double round(final double value) { 207 double tmp = Math.pow(10, this.m_decimals); 208 return (Math.floor(value * tmp + 0.5d)) / tmp; 209 } 210 211 220 public void setDecimals(final int aftercomma) { 221 if (aftercomma >= 0) { 222 this.m_decimals = aftercomma; 223 } 224 } 225 226 235 public String toString() { 236 return this.getUnitName(); 237 } 238 } 239 | Popular Tags |