@Nonnull public Unit<String> createUnit(@Nonnull String value) { return UnitImpl.newInstance(value, this); }
@Nonnull public static <V> Unit<V> newInstance(@Nonnull V value, @Nonnull UnitType<V> unitType) { final UnitImpl<V> result = new UnitImpl<V>(); result.value = value; result.unitType = unitType; return result; }
@Nonnull @Override public Unit<String> convert(@Nonnull Unit<?> from, @Nonnull UnitType<String> toType) { if (!isSupported(from.getUnitType(), toType)) { throw new IllegalArgumentException("Types are not supported!"); } final CalculatorNumeralBase fromTypeAndroid = (CalculatorNumeralBase) from.getUnitType(); final NumeralBase fromNumeralBase = fromTypeAndroid.numeralBase; final NumeralBase toNumeralBase = ((CalculatorNumeralBase) toType).numeralBase; final String fromValue = (String) from.getValue(); final BigInteger decBigInteger = fromNumeralBase.toBigInteger(fromValue); return UnitImpl.newInstance(toNumeralBase.toString(decBigInteger), toType); } }
@Nonnull public static String doConversion(@Nonnull UnitConverter<String> converter, @Nullable String from, @Nonnull UnitType<String> fromUnitType, @Nonnull UnitType<String> toUnitType) throws ConversionException { final String result; if (Strings.isEmpty(from)) { result = ""; } else { final String to; try { if (converter.isSupported(fromUnitType, toUnitType)) { to = converter.convert(UnitImpl.newInstance(from, fromUnitType), toUnitType).getValue(); } else { to = ""; } } catch (RuntimeException e) { throw new ConversionException(e); } result = to; } return result; } }