Skip to content

Commit ab2307b

Browse files
committed
Added BasePoint.
1 parent 420e365 commit ab2307b

File tree

4 files changed

+267
-25
lines changed

4 files changed

+267
-25
lines changed

.classpath

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" output="target/classes" path="src/main/java">
4-
<attributes>
5-
<attribute name="optional" value="true"/>
6-
<attribute name="maven.pomderived" value="true"/>
7-
</attributes>
8-
</classpathentry>
9-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
10-
<attributes>
11-
<attribute name="maven.pomderived" value="true"/>
12-
</attributes>
13-
</classpathentry>
14-
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
15-
<attributes>
16-
<attribute name="maven.pomderived" value="true"/>
17-
</attributes>
18-
</classpathentry>
19-
<classpathentry kind="output" path="target/classes"/>
20-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="src" path="/money-ri-platform"/>
21+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
22+
<attributes>
23+
<attribute name="maven.pomderived" value="true"/>
24+
</attributes>
25+
</classpathentry>
26+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
27+
<attributes>
28+
<attribute name="maven.pomderived" value="true"/>
29+
</attributes>
30+
</classpathentry>
31+
<classpathentry kind="output" path="target/classes"/>
32+
</classpath>

pom.xml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
<name>JMoney and Currency - Extras not part of JSR 354</name>
99
<packaging>jar</packaging>
1010

11+
<parent>
12+
<groupId>javax.money</groupId>
13+
<artifactId>jsr354</artifactId>
14+
<version>0.5-SNAPSHOT</version>
15+
</parent>
16+
1117
<properties>
1218
<javamoney.version>0.5-SNAPSHOT</javamoney.version>
1319
</properties>
@@ -24,11 +30,6 @@
2430
</plugins>
2531
</build>
2632
<dependencies>
27-
<dependency>
28-
<groupId>javax.money</groupId>
29-
<artifactId>money-platform-ri</artifactId>
30-
<version>${javamoney.version}</version>
31-
</dependency>
3233
<dependency>
3334
<groupId>javax.money</groupId>
3435
<artifactId>money-api-convert</artifactId>
@@ -44,5 +45,15 @@
4445
<artifactId>money-api-ext</artifactId>
4546
<version>${javamoney.version}</version>
4647
</dependency>
48+
<dependency>
49+
<groupId>javax.money</groupId>
50+
<artifactId>money-ri-platform</artifactId>
51+
<version>${javamoney.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<scope>test</scope>
57+
</dependency>
4758
</dependencies>
4859
</project>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (c) 2012, 2013, Credit Suisse (Anatole Tresch), Werner Keil.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package net.java.javamoney.extras;
17+
18+
import java.math.BigDecimal;
19+
import java.math.MathContext;
20+
import java.text.NumberFormat;
21+
22+
import javax.money.MonetaryAmount;
23+
import javax.money.MonetaryOperator;
24+
import javax.money.Permil;
25+
26+
/**
27+
* This class allows to extract the permil of a {@link MonetaryAmount} instance.
28+
*
29+
* @version 0.5
30+
* @author Anatole Tresch
31+
* @author Werner Keil
32+
*
33+
* @see <a href="http://en.wikipedia.org/wiki/Per_mil">Wikipedia: Per mil</a>
34+
*/
35+
public final class BasisPoint implements MonetaryOperator {
36+
37+
private static final MathContext DEFAULT_MATH_CONTEXT = initDefaultMathContext();
38+
private static final BigDecimal ONE_TENTHOUSAND = new BigDecimal(10000,
39+
DEFAULT_MATH_CONTEXT);
40+
41+
private final BigDecimal basisPointValue;
42+
43+
/**
44+
* Access the shared instance of {@link Permil} for use.
45+
*
46+
* @return the shared instance, never {@code null}.
47+
*/
48+
private BasisPoint(final BigDecimal decimal) {
49+
basisPointValue = calcBasisPoint(decimal);
50+
}
51+
52+
/**
53+
* Get {@link MathContext} for {@link Permil} instances.
54+
*
55+
* @return the {@link MathContext} to be used, by default
56+
* {@link MathContext#DECIMAL64}.
57+
*/
58+
private static MathContext initDefaultMathContext() {
59+
// TODO Initialize default, e.g. by system properties, or better:
60+
// classpath properties!
61+
return MathContext.DECIMAL64;
62+
}
63+
64+
/**
65+
* Factory method creating a new instance with the given {@code BigDecimal) permil value;
66+
* @param decimal the decimal value of the permil operator being created.
67+
* @return a new {@code Permil} operator
68+
*/
69+
public static BasisPoint of(BigDecimal decimal) {
70+
return new BasisPoint(decimal); // TODO caching, e.g. array for 1-100
71+
// might
72+
// work.
73+
}
74+
75+
/**
76+
* Factory method creating a new instance with the given {@code Number) permil value;
77+
* @param decimal the decimal value of the permil operator being created.
78+
* @return a new {@code Permil} operator
79+
*/
80+
public static BasisPoint of(Number number) {
81+
return of(getBigDecimal(number, DEFAULT_MATH_CONTEXT));
82+
}
83+
84+
/**
85+
* Gets the permil of the amount.
86+
* <p>
87+
* This returns the monetary amount in permil. For example, for 10% 'EUR
88+
* 2.35' will return 0.235.
89+
* <p>
90+
* This is returned as a {@code MonetaryAmount}.
91+
*
92+
* @return the permil result of the amount, never {@code null}
93+
*/
94+
public MonetaryAmount apply(MonetaryAmount amount) {
95+
return amount.multiply(basisPointValue);
96+
}
97+
98+
/*
99+
* (non-Javadoc)
100+
*
101+
* @see java.lang.Object#toString()
102+
*/
103+
@Override
104+
public String toString() {
105+
return NumberFormat.getInstance()
106+
.format(
107+
basisPointValue.multiply(ONE_TENTHOUSAND,
108+
DEFAULT_MATH_CONTEXT))
109+
+
110+
"\u2031";
111+
}
112+
113+
/**
114+
* Converts to {@link BigDecimal}, if necessary, or casts, if possible.
115+
*
116+
* @param number
117+
* The {@link Number}
118+
* @param mathContext
119+
* the {@link MathContext}
120+
* @return the {@code number} as {@link BigDecimal}
121+
*/
122+
private static final BigDecimal getBigDecimal(Number number,
123+
MathContext mathContext) {
124+
if (number instanceof BigDecimal) {
125+
return (BigDecimal) number;
126+
} else {
127+
return new BigDecimal(number.doubleValue(), mathContext);
128+
}
129+
}
130+
131+
/**
132+
* Calculate a BigDecimal value for a Permil e.g. "3" (3 permil) will
133+
* generate .003
134+
*
135+
* @return java.math.BigDecimal
136+
* @param decimal
137+
* java.math.BigDecimal
138+
*/
139+
private static final BigDecimal calcBasisPoint(BigDecimal decimal) {
140+
return decimal.divide(ONE_TENTHOUSAND, DEFAULT_MATH_CONTEXT); // we now
141+
// have
142+
// .003
143+
}
144+
145+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package net.java.javamoney.extras;
2+
/*
3+
* Copyright (c) 2012, 2013, Credit Suisse (Anatole Tresch), Werner Keil.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
6+
* use this file except in compliance with the License. You may obtain a copy of
7+
* the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
* License for the specific language governing permissions and limitations under
15+
* the License.
16+
*/
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.junit.Assert.assertNotNull;
20+
21+
import java.math.BigDecimal;
22+
23+
import javax.money.Money;
24+
25+
import net.java.javamoney.extras.BasisPoint;
26+
27+
import org.junit.Test;
28+
29+
/**
30+
* @author Werner Keil
31+
*
32+
*/
33+
public class BasisPointTest {
34+
35+
@Test
36+
public void testOf() {
37+
BasisPoint perc = BasisPoint.of(BigDecimal.ONE);
38+
assertNotNull(perc);
39+
}
40+
41+
@Test
42+
public void testApply() {
43+
Money m = Money.of("CHF", BigDecimal.valueOf(2.35d));
44+
assertEquals(Money.of("CHF", BigDecimal.valueOf(0.00235d)),
45+
BasisPoint.of(BigDecimal.TEN).apply(m));
46+
}
47+
48+
@Test
49+
public void testApply10() {
50+
Money m = Money.of("CHF", 3);
51+
assertEquals(Money.of("CHF", BigDecimal.valueOf(0.003d)),
52+
BasisPoint.of(BigDecimal.TEN).apply(m));
53+
}
54+
55+
@Test
56+
public void testApply20() {
57+
Money m = Money.of("CHF", 12);
58+
assertEquals(Money.of("CHF", BigDecimal.valueOf(0.024d)),
59+
BasisPoint.of(BigDecimal.valueOf(20)).apply(m));
60+
}
61+
62+
@Test
63+
public void testApply30() {
64+
Money m = Money.of("CHF", 12);
65+
assertEquals(Money.of("CHF", BigDecimal.valueOf(0.036d)),
66+
BasisPoint.of(BigDecimal.valueOf(30)).apply(m));
67+
}
68+
69+
@Test
70+
public void testToString() {
71+
assertEquals("15\u2031", BasisPoint.of(BigDecimal.valueOf(15))
72+
.toString());
73+
}
74+
}

0 commit comments

Comments
 (0)