JavaMoney / jsr354-api

JSR 354 - Money and Currency API
http://javamoney.org
Apache License 2.0
357 stars 79 forks source link

Make Money::with less restritive to support interop with others implementations #113

Open DiegoCoronel opened 5 years ago

DiegoCoronel commented 5 years ago

I had a situation where I needed to implement my own MonetaryAmount (decorated Money) and now Im not able to integrate my code smooth with Money::with because theres a forced cast to Money. Is it possible to relax this and return a MonetaryAmount ?

 @Override
    public Money with(MonetaryOperator operator) { // <-- Can you return MonetaryAmount ?
        Objects.requireNonNull(operator);
        try {
            return Money.class.cast(operator.apply(this)); // <-- Can you relax this code ?
        } catch (MonetaryException e) {
            throw e;
        } catch (Exception e) {
            throw new MonetaryException("Operator failed: " + operator, e);
        }
    }

Initially I had open this issue in https://github.com/JavaMoney/jsr354-ri/issues/190 and @marschall suggested to discuss it here.