Skip to content

Commit

Permalink
Add Joda-Convert
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed Feb 16, 2011
1 parent 4e7f2c1 commit 747d82f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 1 deletion.
1 change: 1 addition & 0 deletions .classpath
Expand Up @@ -4,5 +4,6 @@
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="var" path="M2_REPO/org/testng/testng/5.8/testng-5.8-jdk15.jar"/>
<classpathentry kind="var" path="M2_REPO/org/joda/joda-convert/1.1/joda-convert-1.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Expand Up @@ -7,6 +7,7 @@ by providing a Money class and associated formatting.
This is release 0.6 of Joda-Money (Alpha).

The release depends on Java 5.
Joda-Money has a compile-time dependency on Joda-Convert, but this is not required at runtime.


Feedback
Expand Down
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -177,6 +177,13 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.joda</groupId>
<artifactId>joda-convert</artifactId>
<version>1.1</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/joda/money/BigMoney.java
Expand Up @@ -23,6 +23,9 @@
import java.util.Iterator;
import java.util.regex.Pattern;

import org.joda.convert.FromString;
import org.joda.convert.ToString;

/**
* An amount of money with unrestricted decimal place precision.
* <p>
Expand Down Expand Up @@ -354,6 +357,7 @@ public static BigMoney total(CurrencyUnit currency, Iterable<? extends BigMoneyP
* @throws IllegalArgumentException if the string is malformed
* @throws ArithmeticException if the amount is too large
*/
@FromString
public static BigMoney parse(String moneyStr) {
MoneyUtils.checkNotNull(moneyStr, "Money must not be null");
if (moneyStr.length() < 5 || moneyStr.charAt(3) != ' ') {
Expand Down Expand Up @@ -1679,6 +1683,7 @@ public int hashCode() {
* @return the string representation of this monetary value, never null
*/
@Override
@ToString
public String toString() {
return new StringBuilder()
.append(iCurrency.getCode())
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/joda/money/CurrencyUnit.java
Expand Up @@ -24,6 +24,9 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.joda.convert.FromString;
import org.joda.convert.ToString;

/**
* A unit of currency.
* <p>
Expand Down Expand Up @@ -190,6 +193,7 @@ public static CurrencyUnit of(Currency currency) {
* @return the singleton instance, never null
* @throws IllegalCurrencyException if the currency is unknown
*/
@FromString
public static CurrencyUnit of(String currencyCode) {
MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
CurrencyUnit currency = cCurrenciesByCode.get(currencyCode);
Expand Down Expand Up @@ -526,6 +530,8 @@ public int hashCode() {
*
* @return the currency code, never null
*/
@Override
@ToString
public String toString() {
return iCode;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/joda/money/Money.java
Expand Up @@ -21,6 +21,9 @@
import java.util.Arrays;
import java.util.Iterator;

import org.joda.convert.FromString;
import org.joda.convert.ToString;

/**
* An amount of money with the standard decimal places defined by the currency.
* <p>
Expand Down Expand Up @@ -327,6 +330,7 @@ public static Money total(CurrencyUnit currency, Iterable<Money> monies) {
* @throws IllegalArgumentException if the string is malformed
* @throws ArithmeticException if the amount is too large
*/
@FromString
public static Money parse(String moneyStr) {
return Money.of(BigMoney.parse(moneyStr));
}
Expand Down Expand Up @@ -1303,6 +1307,7 @@ public int hashCode() {
* @return the string representation of this monetary value, never null
*/
@Override
@ToString
public String toString() {
return iMoney.toString();
}
Expand Down
3 changes: 2 additions & 1 deletion src/site/xdoc/index.xml
Expand Up @@ -78,7 +78,8 @@ is the current development release intended for feedback rather than production
(The code is fully tested, but there may yet be bugs and the API may yet change).
</p>
<p>
There are no dependencies beyond JDK 1.5.
There are no runtime dependencies beyond JDK 1.5.<br />
(Joda-Money has a compile-time dependency on Joda-Convert, but this is not required at runtime)
</p>
</section>

Expand Down
50 changes: 50 additions & 0 deletions src/test/java/org/joda/money/TestStringConvert.java
@@ -0,0 +1,50 @@
/*
* Copyright 2009-2011 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.money;

import static org.testng.Assert.assertEquals;

import org.joda.convert.StringConvert;
import org.testng.annotations.Test;

/**
* Test string conversion.
*/
@Test
public class TestStringConvert {

public void test_BigMoney() {
BigMoney test = BigMoney.of(CurrencyUnit.CHF, 1234.5678d);
String str = StringConvert.INSTANCE.convertToString(test);
assertEquals("CHF 1234.5678", str);
assertEquals(test, StringConvert.INSTANCE.convertFromString(BigMoney.class, str));
}

public void test_Money() {
Money test = Money.of(CurrencyUnit.CHF, 1234.56d);
String str = StringConvert.INSTANCE.convertToString(test);
assertEquals("CHF 1234.56", str);
assertEquals(test, StringConvert.INSTANCE.convertFromString(Money.class, str));
}

public void test_CurrencyUnit() {
CurrencyUnit test = CurrencyUnit.CHF;
String str = StringConvert.INSTANCE.convertToString(test);
assertEquals("CHF", str);
assertEquals(test, StringConvert.INSTANCE.convertFromString(CurrencyUnit.class, str));
}

}

0 comments on commit 747d82f

Please sign in to comment.