@Test(expectedExceptions = IllegalArgumentException.class) public void shouldReturnErrorWhenPrecisionIsZero() { MathContext mathContext = new MathContext(0, RoundingMode.HALF_EVEN); PrecisionContextRoundedOperator.of(mathContext); fail(); }
public static PrecisionContextRoundedOperator of(MathContext mathContext) { Objects.requireNonNull(mathContext); if(RoundingMode.UNNECESSARY.equals(mathContext.getRoundingMode())) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the RoundingMode.UNNECESSARY on MathContext"); } if(mathContext.getPrecision() <= 0) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the zero precision on MathContext"); } return new PrecisionContextRoundedOperator(mathContext); }
PrecisionContextRoundedOperator implements MonetaryOperator { public static PrecisionContextRoundedOperator of(MathContext mathContext) { Objects.requireNonNull(mathContext); if(RoundingMode.UNNECESSARY.equals(mathContext.getRoundingMode())) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the RoundingMode.UNNECESSARY on MathContext"); } if(mathContext.getPrecision() <= 0) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the zero precision on MathContext"); } return new PrecisionContextRoundedOperator(mathContext); } }
PrecisionContextRoundedOperator implements MonetaryOperator { public static PrecisionContextRoundedOperator of(MathContext mathContext) { Objects.requireNonNull(mathContext); if(RoundingMode.UNNECESSARY.equals(mathContext.getRoundingMode())) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the RoundingMode.UNNECESSARY on MathContext"); } if(mathContext.getPrecision() <= 0) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the zero precision on MathContext"); } return new PrecisionContextRoundedOperator(mathContext); } private  PrecisionContextRoundedOperator(MathContext mathContext); }
PrecisionContextRoundedOperator implements MonetaryOperator { public static PrecisionContextRoundedOperator of(MathContext mathContext) { Objects.requireNonNull(mathContext); if(RoundingMode.UNNECESSARY.equals(mathContext.getRoundingMode())) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the RoundingMode.UNNECESSARY on MathContext"); } if(mathContext.getPrecision() <= 0) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the zero precision on MathContext"); } return new PrecisionContextRoundedOperator(mathContext); } private  PrecisionContextRoundedOperator(MathContext mathContext); static PrecisionContextRoundedOperator of(MathContext mathContext); @Override MonetaryAmount apply(MonetaryAmount amount); MathContext getMathContext(); @Override String toString(); }
PrecisionContextRoundedOperator implements MonetaryOperator { public static PrecisionContextRoundedOperator of(MathContext mathContext) { Objects.requireNonNull(mathContext); if(RoundingMode.UNNECESSARY.equals(mathContext.getRoundingMode())) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the RoundingMode.UNNECESSARY on MathContext"); } if(mathContext.getPrecision() <= 0) { throw new IllegalArgumentException("To create the MathContextRoundedOperator you cannot use the zero precision on MathContext"); } return new PrecisionContextRoundedOperator(mathContext); } private  PrecisionContextRoundedOperator(MathContext mathContext); static PrecisionContextRoundedOperator of(MathContext mathContext); @Override MonetaryAmount apply(MonetaryAmount amount); MathContext getMathContext(); @Override String toString();  }