1 9 package javax.measure.converters; 10 11 import java.io.Serializable ; 12 13 27 public abstract class UnitConverter implements Serializable { 28 29 33 public static final UnitConverter IDENTITY = new Identity(); 34 35 38 protected UnitConverter() { 39 } 40 41 48 public abstract UnitConverter inverse(); 49 50 57 public abstract double convert(double x) throws ConversionException; 58 59 71 public abstract boolean isLinear(); 72 73 81 public abstract boolean equals(Object cvtr); 82 83 90 public abstract int hashCode(); 91 92 104 public UnitConverter concatenate(UnitConverter converter) { 105 return (converter == IDENTITY) ? this : new Compound(converter, this); 106 } 107 108 111 private static final class Identity extends UnitConverter { 112 113 @Override 114 public UnitConverter inverse() { 115 return this; 116 } 117 118 @Override 119 public double convert(double x) { 120 return x; 121 } 122 123 @Override 124 public boolean isLinear() { 125 return true; 126 } 127 128 @Override 129 public UnitConverter concatenate(UnitConverter converter) { 130 return converter; 131 } 132 133 @Override 134 public boolean equals(Object obj) { 135 return this == obj; 136 } 137 138 @Override 139 public int hashCode() { 140 return 0; 141 } 142 143 private static final long serialVersionUID = 1L; 144 145 } 146 147 150 private static final class Compound extends UnitConverter { 151 152 155 private final UnitConverter _first; 156 157 160 private final UnitConverter _second; 161 162 169 private Compound(UnitConverter first, UnitConverter second) { 170 _first = first; 171 _second = second; 172 } 173 174 @Override 175 public UnitConverter inverse() { 176 return new Compound(_second.inverse(), _first.inverse()); 177 } 178 179 @Override 180 public double convert(double x) { 181 return _second.convert(_first.convert(x)); 182 } 183 184 @Override 185 public boolean isLinear() { 186 return _first.isLinear() && _second.isLinear(); 187 } 188 189 @Override 190 public boolean equals(Object cvtr) { 191 return (cvtr instanceof Compound) 192 && _first.equals(((Compound) cvtr)._first) 193 && _second.equals(((Compound) cvtr)._second); 194 } 195 196 @Override 197 public int hashCode() { 198 return _first.hashCode() + _second.hashCode(); 199 } 200 201 private static final long serialVersionUID = 1L; 202 203 } 204 } | Popular Tags |