|  | 
| 17 | 17 |  */ | 
| 18 | 18 | package org.javamoney.calc; | 
| 19 | 19 | 
 | 
| 20 |  | -import javax.money.CurrencyUnit; | 
| 21 |  | -import javax.money.MonetaryAmount; | 
| 22 |  | -import javax.money.MonetaryAmountFactory; | 
| 23 |  | -import javax.money.MonetaryContext; | 
| 24 |  | -import javax.money.MonetaryOperator; | 
| 25 |  | -import javax.money.MonetaryQuery; | 
| 26 |  | -import javax.money.NumberValue; | 
| 27 |  | - | 
| 28 |  | -import org.javamoney.moneta.Money; | 
|  | 20 | +import javax.money.*; | 
| 29 | 21 | 
 | 
| 30 | 22 | import java.util.function.Predicate; | 
| 31 | 23 | 
 | 
| @@ -177,24 +169,6 @@ public ValidatedMoney plus() { | 
| 177 | 169 |         return of(this.amount.plus(), predicate); | 
| 178 | 170 |     } | 
| 179 | 171 | 
 | 
| 180 |  | -    /* | 
| 181 |  | -     * (non-Javadoc) | 
| 182 |  | -     * | 
| 183 |  | -     * @see javax.money.MonetaryAmount#subtract(javax.money.MonetaryAmount) | 
| 184 |  | -     */ | 
| 185 |  | -    public ValidatedMoney subtract(Money subtrahend) { | 
| 186 |  | -        return of(this.amount.subtract(subtrahend), predicate); | 
| 187 |  | -    } | 
| 188 |  | - | 
| 189 |  | -    // /* | 
| 190 |  | -    // * (non-Javadoc) | 
| 191 |  | -    // * | 
| 192 |  | -    // * @see javax.money.MonetaryAmount#ulp() | 
| 193 |  | -    // */ | 
| 194 |  | -    // public ConstraintMoney ulp() { | 
| 195 |  | -    // return of(this.amount.ulp(), predicate); | 
| 196 |  | -    // } | 
| 197 |  | - | 
| 198 | 172 |     /* | 
| 199 | 173 |      * (non-Javadoc) | 
| 200 | 174 |      * | 
| @@ -417,13 +391,79 @@ public ValidatedMoney stripTrailingZeros() { | 
| 417 | 391 | 
 | 
| 418 | 392 |     @Override | 
| 419 | 393 |     public MonetaryAmountFactory<ValidatedMoney> getFactory() { | 
| 420 |  | -        return null; | 
| 421 |  | -        // return new ConstraintMoneyFactory(this); | 
|  | 394 | +        return new ValidatedAmountFactory(this); | 
| 422 | 395 |     } | 
| 423 | 396 | 
 | 
| 424 | 397 |     @Override | 
| 425 | 398 |     public int compareTo(MonetaryAmount o) { | 
| 426 | 399 |         return this.amount.compareTo(o); | 
| 427 | 400 |     } | 
| 428 | 401 | 
 | 
|  | 402 | +    /** | 
|  | 403 | +     * Amount factory, which validates the amount created, based on the default amount factory. | 
|  | 404 | +     */ | 
|  | 405 | +    private final class ValidatedAmountFactory implements MonetaryAmountFactory<ValidatedMoney>{ | 
|  | 406 | +        private Predicate<MonetaryAmount> predicate; | 
|  | 407 | +        private MonetaryAmountFactory<?> factory = Monetary.getDefaultAmountFactory(); | 
|  | 408 | + | 
|  | 409 | +        public ValidatedAmountFactory(ValidatedMoney amount){ | 
|  | 410 | +            this.predicate = amount.predicate; | 
|  | 411 | +        } | 
|  | 412 | + | 
|  | 413 | +        @Override | 
|  | 414 | +        public Class<? extends MonetaryAmount> getAmountType() { | 
|  | 415 | +            return ValidatedMoney.class; | 
|  | 416 | +        } | 
|  | 417 | + | 
|  | 418 | +        @Override | 
|  | 419 | +        public MonetaryAmountFactory setCurrency(CurrencyUnit currency) { | 
|  | 420 | +            factory.setCurrency(currency); | 
|  | 421 | +            return this; | 
|  | 422 | +        } | 
|  | 423 | + | 
|  | 424 | +        @Override | 
|  | 425 | +        public MonetaryAmountFactory setNumber(double number) { | 
|  | 426 | +            factory.setNumber(number); | 
|  | 427 | +            return this; | 
|  | 428 | +        } | 
|  | 429 | + | 
|  | 430 | +        @Override | 
|  | 431 | +        public MonetaryAmountFactory setNumber(long number) { | 
|  | 432 | +            factory.setNumber(number); | 
|  | 433 | +            return this; | 
|  | 434 | +        } | 
|  | 435 | + | 
|  | 436 | +        @Override | 
|  | 437 | +        public MonetaryAmountFactory setNumber(Number number) { | 
|  | 438 | +            factory.setNumber(number); | 
|  | 439 | +            return this; | 
|  | 440 | +        } | 
|  | 441 | + | 
|  | 442 | +        @Override | 
|  | 443 | +        public NumberValue getMaxNumber() { | 
|  | 444 | +            return factory.getMaxNumber(); | 
|  | 445 | +        } | 
|  | 446 | + | 
|  | 447 | +        @Override | 
|  | 448 | +        public NumberValue getMinNumber() { | 
|  | 449 | +            return factory.getMinNumber(); | 
|  | 450 | +        } | 
|  | 451 | + | 
|  | 452 | +        @Override | 
|  | 453 | +        public MonetaryAmountFactory setContext(MonetaryContext monetaryContext) { | 
|  | 454 | +            factory.setContext(monetaryContext); | 
|  | 455 | +            return this; | 
|  | 456 | +        } | 
|  | 457 | + | 
|  | 458 | +        @Override | 
|  | 459 | +        public ValidatedMoney create() { | 
|  | 460 | +            return new ValidatedMoney(factory.create(), predicate); | 
|  | 461 | +        } | 
|  | 462 | + | 
|  | 463 | +        @Override | 
|  | 464 | +        public MonetaryContext getDefaultMonetaryContext() { | 
|  | 465 | +            return factory.getDefaultMonetaryContext(); | 
|  | 466 | +        } | 
|  | 467 | +    } | 
|  | 468 | + | 
| 429 | 469 | } | 
0 commit comments