Skip to content

Commit

Permalink
Use modern Java language features (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed Aug 25, 2024
1 parent f0adbd6 commit ef1d3af
Show file tree
Hide file tree
Showing 35 changed files with 1,609 additions and 1,688 deletions.
251 changes: 125 additions & 126 deletions src/main/java/org/joda/money/BigMoney.java

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/main/java/org/joda/money/BigMoneyProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public interface BigMoneyProvider {
* preference to calling this method directly. It is also recommended that the
* converted {@code BigMoney} is cached in a local variable instead of
* performing the conversion multiple times.
*
*
* @return the converted money instance, never null
* @throws RuntimeException if conversion is not possible
*/
BigMoney toBigMoney();
public abstract BigMoney toBigMoney();

}
6 changes: 3 additions & 3 deletions src/main/java/org/joda/money/CurrencyMismatchException.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CurrencyMismatchException extends IllegalArgumentException {

/**
* Constructor.
*
*
* @param firstCurrency the first currency, may be null
* @param secondCurrency the second currency, not null
*/
Expand All @@ -50,7 +50,7 @@ public CurrencyMismatchException(CurrencyUnit firstCurrency, CurrencyUnit second
//-----------------------------------------------------------------------
/**
* Gets the first currency at fault.
*
*
* @return the currency at fault, may be null
*/
public CurrencyUnit getFirstCurrency() {
Expand All @@ -59,7 +59,7 @@ public CurrencyUnit getFirstCurrency() {

/**
* Gets the second currency at fault.
*
*
* @return the currency at fault, may be null
*/
public CurrencyUnit getSecondCurrency() {
Expand Down
82 changes: 39 additions & 43 deletions src/main/java/org/joda/money/CurrencyUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,20 @@ public final class CurrencyUnit implements Comparable<CurrencyUnit>, Serializabl
/**
* Map of registered currencies by text code.
*/
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCode = new ConcurrentSkipListMap<String, CurrencyUnit>();
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCode = new ConcurrentSkipListMap<>();
/**
* Map of registered currencies by numeric code.
*/
private static final ConcurrentMap<Integer, CurrencyUnit> currenciesByNumericCode = new ConcurrentHashMap<Integer, CurrencyUnit>();
private static final ConcurrentMap<Integer, CurrencyUnit> currenciesByNumericCode = new ConcurrentHashMap<>();
/**
* Map of registered currencies by country.
*/
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCountry = new ConcurrentSkipListMap<String, CurrencyUnit>();
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCountry = new ConcurrentSkipListMap<>();
static {
// load one data provider by system property
try {
try {
String clsName = System.getProperty(
var clsName = System.getProperty(
"org.joda.money.CurrencyUnitDataProvider",
"org.joda.money.DefaultCurrencyUnitDataProvider");
Class<? extends CurrencyUnitDataProvider> cls =
Expand Down Expand Up @@ -207,7 +207,7 @@ public static synchronized CurrencyUnit registerCurrency(
if (currencyCode.length() != 3) {
throw new IllegalArgumentException("Invalid string code, must be length 3");
}
if (CODE.matcher(currencyCode).matches() == false) {
if (!CODE.matcher(currencyCode).matches()) {
throw new IllegalArgumentException("Invalid string code, must be ASCII upper-case letters");
}
if (numericCurrencyCode < -1 || numericCurrencyCode > 999) {
Expand All @@ -218,7 +218,7 @@ public static synchronized CurrencyUnit registerCurrency(
}
MoneyUtils.checkNotNull(countryCodes, "Country codes must not be null");

CurrencyUnit currency = new CurrencyUnit(currencyCode, (short) numericCurrencyCode, (short) decimalPlaces);
var currency = new CurrencyUnit(currencyCode, (short) numericCurrencyCode, (short) decimalPlaces);
if (force) {
currenciesByCode.remove(currencyCode);
currenciesByNumericCode.remove(numericCurrencyCode);
Expand Down Expand Up @@ -354,7 +354,7 @@ public static CurrencyUnit of(Currency currency) {
@FromString
public static CurrencyUnit of(String currencyCode) {
MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
CurrencyUnit currency = currenciesByCode.get(currencyCode);
var currency = currenciesByCode.get(currencyCode);
if (currency == null) {
throw new IllegalCurrencyException("Unknown currency '" + currencyCode + '\'');
}
Expand All @@ -373,21 +373,17 @@ public static CurrencyUnit of(String currencyCode) {
*/
public static CurrencyUnit ofNumericCode(String numericCurrencyCode) {
MoneyUtils.checkNotNull(numericCurrencyCode, "Currency code must not be null");
switch (numericCurrencyCode.length()) {
case 1:
return ofNumericCode(numericCurrencyCode.charAt(0) - '0');
case 2:
return ofNumericCode(
(numericCurrencyCode.charAt(0) - '0') * 10 +
numericCurrencyCode.charAt(1) - '0');
case 3:
return ofNumericCode(
(numericCurrencyCode.charAt(0) - '0') * 100 +
(numericCurrencyCode.charAt(1) - '0') * 10 +
numericCurrencyCode.charAt(2) - '0');
default:
throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\'');
}
return switch (numericCurrencyCode.length()) {
case 1 -> ofNumericCode(numericCurrencyCode.charAt(0) - '0');
case 2 -> ofNumericCode(
(numericCurrencyCode.charAt(0) - '0') * 10 +
numericCurrencyCode.charAt(1) - '0');
case 3 -> ofNumericCode(
(numericCurrencyCode.charAt(0) - '0') * 100 +
(numericCurrencyCode.charAt(1) - '0') * 10 +
numericCurrencyCode.charAt(2) - '0');
default -> throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\'');
};
}

/**
Expand All @@ -400,7 +396,7 @@ public static CurrencyUnit ofNumericCode(String numericCurrencyCode) {
* @throws IllegalCurrencyException if the currency is unknown
*/
public static CurrencyUnit ofNumericCode(int numericCurrencyCode) {
CurrencyUnit currency = currenciesByNumericCode.get(numericCurrencyCode);
var currency = currenciesByNumericCode.get(numericCurrencyCode);
if (currency == null) {
throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\'');
}
Expand All @@ -418,7 +414,7 @@ public static CurrencyUnit ofNumericCode(int numericCurrencyCode) {
*/
public static CurrencyUnit of(Locale locale) {
MoneyUtils.checkNotNull(locale, "Locale must not be null");
CurrencyUnit currency = currenciesByCountry.get(locale.getCountry());
var currency = currenciesByCountry.get(locale.getCountry());
if (currency == null) {
throw new IllegalCurrencyException("No currency found for locale '" + locale + '\'');
}
Expand All @@ -437,7 +433,7 @@ public static CurrencyUnit of(Locale locale) {
*/
public static CurrencyUnit ofCountry(String countryCode) {
MoneyUtils.checkNotNull(countryCode, "Country code must not be null");
CurrencyUnit currency = currenciesByCountry.get(countryCode);
var currency = currenciesByCountry.get(countryCode);
if (currency == null) {
throw new IllegalCurrencyException("No currency found for country '" + countryCode + '\'');
}
Expand All @@ -447,7 +443,7 @@ public static CurrencyUnit ofCountry(String countryCode) {
//-----------------------------------------------------------------------
/**
* Constructor, creating a new currency instance.
*
*
* @param code the three-letter currency code, not null
* @param numericCode the numeric currency code, from 0 to 999, -1 if none
* @param decimalPlaces the decimal places, not null
Expand All @@ -461,7 +457,7 @@ public static CurrencyUnit ofCountry(String countryCode) {

/**
* Block malicious data streams.
*
*
* @param ois the input stream, not null
* @throws InvalidObjectException if an error occurs
*/
Expand All @@ -471,7 +467,7 @@ private void readObject(ObjectInputStream ois) throws InvalidObjectException {

/**
* Uses a serialization delegate.
*
*
* @return the replacing object, never null
*/
private Object writeReplace() {
Expand All @@ -483,7 +479,7 @@ private Object writeReplace() {
* Gets the ISO-4217 three-letter currency code.
* <p>
* Each currency is uniquely identified by a three-letter upper-case code, based on ISO-4217.
*
*
* @return the three-letter upper-case currency code, never null
*/
public String getCode() {
Expand All @@ -494,7 +490,7 @@ public String getCode() {
* Gets the ISO-4217 numeric currency code.
* <p>
* The numeric code is an alternative to the standard string-based code.
*
*
* @return the numeric currency code, -1 if no numeric code
*/
public int getNumericCode() {
Expand All @@ -506,14 +502,14 @@ public int getNumericCode() {
* <p>
* This formats the numeric code as a three digit string prefixed by zeroes if necessary.
* If there is no valid code, then an empty string is returned.
*
*
* @return the three digit numeric currency code, empty is no code, never null
*/
public String getNumeric3Code() {
if (numericCode < 0) {
return "";
}
String str = Integer.toString(numericCode);
var str = Integer.toString(numericCode);
if (str.length() == 1) {
return "00" + str;
}
Expand All @@ -529,11 +525,11 @@ public String getNumeric3Code() {
* A currency is typically valid in one or more countries.
* The codes are typically defined by ISO-3166.
* An empty set indicates that no the currency is not associated with a country code.
*
*
* @return the country codes, may be empty, not null
*/
public Set<String> getCountryCodes() {
Set<String> countryCodes = new HashSet<String>();
Set<String> countryCodes = new HashSet<>();
for (Entry<String, CurrencyUnit> entry : currenciesByCountry.entrySet()) {
if (this.equals(entry.getValue())) {
countryCodes.add(entry.getKey());
Expand All @@ -549,7 +545,7 @@ public Set<String> getCountryCodes() {
* Different currencies have different numbers of decimal places by default.
* For example, 'GBP' has 2 decimal places, but 'JPY' has zero.
* Pseudo-currencies will return zero.
*
*
* @return the decimal places, from 0 to 9 (normally 0, 2 or 3)
*/
public int getDecimalPlaces() {
Expand All @@ -558,7 +554,7 @@ public int getDecimalPlaces() {

/**
* Checks if this is a pseudo-currency.
*
*
* @return true if this is a pseudo-currency
*/
public boolean isPseudoCurrency() {
Expand All @@ -573,7 +569,7 @@ public boolean isPseudoCurrency() {
* is returned.
* <p>
* This method matches the API of {@link Currency}.
*
*
* @return the JDK currency instance, never null
*/
public String getSymbol() {
Expand All @@ -595,7 +591,7 @@ public String getSymbol() {
* is returned.
* <p>
* This method matches the API of {@link Currency}.
*
*
* @param locale the locale to get the symbol for, not null
* @return the JDK currency instance, never null
*/
Expand All @@ -617,7 +613,7 @@ public String getSymbol(Locale locale) {
* Gets the JDK currency instance equivalent to this currency.
* <p>
* This attempts to convert a {@code CurrencyUnit} to a JDK {@code Currency}.
*
*
* @return the JDK currency instance, never null
* @throws IllegalArgumentException if no matching currency exists in the JDK
*/
Expand All @@ -628,7 +624,7 @@ public Currency toCurrency() {
//-----------------------------------------------------------------------
/**
* Compares this currency to another by alphabetical comparison of the code.
*
*
* @param other the other currency, not null
* @return negative if earlier alphabetically, 0 if equal, positive if greater alphabetically
*/
Expand All @@ -641,7 +637,7 @@ public int compareTo(CurrencyUnit other) {
* Checks if this currency equals another currency.
* <p>
* The comparison checks the 3 letter currency code.
*
*
* @param obj the other currency, null returns false
* @return true if equal
*/
Expand All @@ -658,7 +654,7 @@ public boolean equals(Object obj) {

/**
* Returns a suitable hash code for the currency.
*
*
* @return the hash code
*/
@Override
Expand All @@ -669,7 +665,7 @@ public int hashCode() {
//-----------------------------------------------------------------------
/**
* Gets the currency code as a string.
*
*
* @return the currency code, never null
*/
@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/joda/money/CurrencyUnitDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public abstract class CurrencyUnitDataProvider {

/**
* Registers all the currencies known by this provider.
*
*
* @throws Exception if an error occurs
*/
protected abstract void registerCurrencies() throws Exception;
Expand Down
Loading

0 comments on commit ef1d3af

Please sign in to comment.