1 9 package javax.measure.converters; 10 11 22 public final class LogConverter extends UnitConverter { 23 24 27 private final double _base; 28 29 32 private final double _logBase; 33 34 37 private final double _invLogBase; 38 39 42 private final Inverse _inverse = new Inverse(); 43 44 50 public LogConverter(double base) { 51 _base = base; 52 _logBase = Math.log(base); 53 _invLogBase = 1.0 / _logBase; 54 } 55 56 62 public double getBase() { 63 return _base; 64 } 65 66 @Override 67 public UnitConverter inverse() { 68 return _inverse; 69 } 70 71 @Override 72 public double convert(double amount) { 73 return _invLogBase * Math.log(amount); 74 } 75 76 @Override 77 public boolean isLinear() { 78 return false; 79 } 80 81 @Override 82 public boolean equals(Object obj) { 83 if (!(obj instanceof LogConverter)) 84 return false; 85 LogConverter lc = (LogConverter) obj; 86 return (float) _base == (float) lc._base; 87 } 88 89 @Override 90 public int hashCode() { 91 int h = Float.floatToIntBits((float) _base); 92 h += ~(h << 9); 93 h ^= (h >>> 14); 94 h += (h << 4); 95 return h ^ (h >>> 10); 96 } 97 98 102 private class Inverse extends UnitConverter { 103 104 105 @Override 106 public UnitConverter inverse() { 107 return LogConverter.this; 108 } 109 110 @Override 111 public double convert(double amount) { 112 return Math.exp(_logBase * amount); 113 } 114 115 @Override 116 public boolean isLinear() { 117 return false; 118 } 119 120 @Override 121 public boolean equals(Object cvtr) { 122 return (cvtr instanceof Inverse) 123 && LogConverter.this.equals(((Inverse) cvtr).inverse()); 124 } 125 126 @Override 127 public int hashCode() { 128 return - LogConverter.this.hashCode(); 129 } 130 131 private static final long serialVersionUID = 1L; 132 } 133 134 private static final long serialVersionUID = 1L; 135 } | Popular Tags |