operator % method

ValidatedQuantity operator %(
  1. Object other
)

Implementation

ValidatedQuantity operator %(Object other) {
  if (other is UcumDecimal) {
    return copyWith(value: value.modulo(other));
  } else if (other is ValidatedQuantity) {
    if (UcumService().isComparable(unit, other.unit)) {
      final UcumDecimal convertedValue =
          UcumService().convert(other.value, other.unit, unit);
      return copyWith(value: value.modulo(convertedValue));
    } else {
      throw UcumException('$this could not be moduloed with $other '
          '(reason: units are not comparable)');
    }
  } else if (other is num || other is BigInt) {
    return copyWith(
        value: value.modulo(UcumDecimal.fromString(other.toString())));
  } else if (other is String) {
    final ValidatedQuantity newQuantity = ValidatedQuantity.fromString(other);
    if (newQuantity.isValid()) {
      if (UcumService().isComparable(unit, newQuantity.unit)) {
        final UcumDecimal convertedValue =
            UcumService().convert(newQuantity.value, newQuantity.unit, unit);
        return copyWith(value: value.modulo(convertedValue));
      } else {
        throw UcumException('$this could not be moduloed with $other '
            '(reason: units are not comparable)');
      }
    } else {
      throw UcumException('$this could not be moduloed with $other '
          '(reason: it is not an accepted type)');
    }
  } else {
    throw UcumException('$this could not be moduloed with $other '
        '(reason: it is not an accepted type)');
  }
}