-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds src/main/java/org/javamoney/moneta/ScaleRoundedOperator.java
- Loading branch information
otaviojava
committed
Apr 22, 2015
1 parent
fc92435
commit 434f06b
Showing
4 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/org/javamoney/moneta/ScaleRoundedOperator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package org.javamoney.moneta; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.MathContext; | ||
import java.math.RoundingMode; | ||
import java.util.Objects; | ||
|
||
import javax.money.MonetaryAmount; | ||
import javax.money.MonetaryOperator; | ||
|
||
/** | ||
* <p>This implementation uses a scale and {@link RoundingMode} to does the rounding operations. The implementation will use the <b>scale</b>, in other words, the number of digits to the right of the decimal point</p> | ||
* <p>The derived class will implements the {@link RoundedMoney} with this rounding monetary operator</p> | ||
* <pre> | ||
* {@code | ||
* | ||
* MonetaryOperator monetaryOperator = ScaleRoundedOperator.of(scale, RoundingMode.HALF_EVEN); | ||
* CurrencyUnit real = Monetary.getCurrency("BRL"); | ||
* MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real); | ||
* MonetaryAmount result = monetaryOperator.apply(money); // BRL 35.3457 | ||
* | ||
* } | ||
* </pre> | ||
* <p>Case the parameter in {@link MonetaryOperator#apply(MonetaryAmount)} be null, the apply will return a {@link NullPointerException}</p> | ||
* @author Otavio Santana | ||
* @see {@link ScaleRoundedOperator#of(MathContext)} | ||
* @see {@link RoundedMoney} | ||
* @see {@link MonetaryOperator} | ||
* @see {@link BigDecimal#scale()} | ||
*/ | ||
public final class ScaleRoundedOperator implements MonetaryOperator { | ||
|
||
private final int scale; | ||
|
||
private final RoundingMode roundingMode; | ||
|
||
private ScaleRoundedOperator(int scale, RoundingMode roundingMode) { | ||
this.scale = scale; | ||
this.roundingMode = roundingMode; | ||
} | ||
|
||
/** | ||
* Creates the rounded Operator from scale and roundingMode | ||
* @param mathContext | ||
* @return the {@link MonetaryOperator} using the scale and {@link roundingMode} used in parameter | ||
* @throws NullPointerException when the {@link MathContext} is null | ||
* @see {@linkplain RoundingMode} | ||
*/ | ||
public static ScaleRoundedOperator of(int scale, RoundingMode roundingMode) { | ||
return new ScaleRoundedOperator(scale, Objects.requireNonNull(roundingMode)); | ||
} | ||
|
||
@Override | ||
public MonetaryAmount apply(MonetaryAmount amount) { | ||
RoundedMoney roundedMoney = RoundedMoney.from(Objects.requireNonNull(amount)); | ||
BigDecimal numberValue = roundedMoney.getNumber().numberValue(BigDecimal.class); | ||
BigDecimal numberRounded = numberValue.setScale(scale, roundingMode); | ||
return RoundedMoney.of(numberRounded, roundedMoney.getCurrency(), this); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(ScaleRoundedOperator.class.getName()).append('{') | ||
.append("scale:").append(Integer.toString(scale)).append(',') | ||
.append("roundingMode:").append(roundingMode).append('}'); | ||
return sb.toString(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/test/java/org/javamoney/moneta/ScaleRoundedOperatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.javamoney.moneta; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
import static org.testng.Assert.fail; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
|
||
import javax.money.CurrencyUnit; | ||
import javax.money.Monetary; | ||
import javax.money.MonetaryAmount; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
public class ScaleRoundedOperatorTest { | ||
|
||
@Test(expectedExceptions = NullPointerException.class) | ||
public void shouldReturnNullPointerExceptionWhenParameterIsNull() { | ||
ScaleRoundedOperator.of(0, null); | ||
fail(); | ||
} | ||
|
||
@Test(expectedExceptions = NullPointerException.class) | ||
public void shouldReturnErrorWhenParameterIsNUll() { | ||
ScaleRoundedOperator monetaryOperator = ScaleRoundedOperator.of(0, RoundingMode.HALF_EVEN); | ||
monetaryOperator.apply(null); | ||
fail(); | ||
} | ||
|
||
@Test | ||
public void shouldRoundedMonetaryOperatorWhenTheImplementationIsMoney() { | ||
int scale = 4; | ||
|
||
CurrencyUnit real = Monetary.getCurrency("BRL"); | ||
MonetaryAmount money = Money.of(BigDecimal.valueOf(35.34567), real); | ||
|
||
ScaleRoundedOperator monetaryOperator = ScaleRoundedOperator.of(scale, RoundingMode.HALF_EVEN); | ||
MonetaryAmount result = monetaryOperator.apply(money); | ||
assertTrue(RoundedMoney.class.isInstance(result)); | ||
assertEquals(result.getCurrency(), real); | ||
assertEquals(result.getNumber().getScale(), scale); | ||
assertEquals(BigDecimal.valueOf(35.3457), result.getNumber().numberValue(BigDecimal.class)); | ||
|
||
|
||
|
||
} | ||
} |