Skip to content

Commit ef1d3af

Browse files
committed
Use modern Java language features (#140)
1 parent f0adbd6 commit ef1d3af

35 files changed

Lines changed: 1609 additions & 1688 deletions

src/main/java/org/joda/money/BigMoney.java

Lines changed: 125 additions & 126 deletions
Large diffs are not rendered by default.

src/main/java/org/joda/money/BigMoneyProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public interface BigMoneyProvider {
4141
* preference to calling this method directly. It is also recommended that the
4242
* converted {@code BigMoney} is cached in a local variable instead of
4343
* performing the conversion multiple times.
44-
*
44+
*
4545
* @return the converted money instance, never null
4646
* @throws RuntimeException if conversion is not possible
4747
*/
48-
BigMoney toBigMoney();
48+
public abstract BigMoney toBigMoney();
4949

5050
}

src/main/java/org/joda/money/CurrencyMismatchException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class CurrencyMismatchException extends IllegalArgumentException {
3535

3636
/**
3737
* Constructor.
38-
*
38+
*
3939
* @param firstCurrency the first currency, may be null
4040
* @param secondCurrency the second currency, not null
4141
*/
@@ -50,7 +50,7 @@ public CurrencyMismatchException(CurrencyUnit firstCurrency, CurrencyUnit second
5050
//-----------------------------------------------------------------------
5151
/**
5252
* Gets the first currency at fault.
53-
*
53+
*
5454
* @return the currency at fault, may be null
5555
*/
5656
public CurrencyUnit getFirstCurrency() {
@@ -59,7 +59,7 @@ public CurrencyUnit getFirstCurrency() {
5959

6060
/**
6161
* Gets the second currency at fault.
62-
*
62+
*
6363
* @return the currency at fault, may be null
6464
*/
6565
public CurrencyUnit getSecondCurrency() {

src/main/java/org/joda/money/CurrencyUnit.java

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@ public final class CurrencyUnit implements Comparable<CurrencyUnit>, Serializabl
6060
/**
6161
* Map of registered currencies by text code.
6262
*/
63-
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCode = new ConcurrentSkipListMap<String, CurrencyUnit>();
63+
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCode = new ConcurrentSkipListMap<>();
6464
/**
6565
* Map of registered currencies by numeric code.
6666
*/
67-
private static final ConcurrentMap<Integer, CurrencyUnit> currenciesByNumericCode = new ConcurrentHashMap<Integer, CurrencyUnit>();
67+
private static final ConcurrentMap<Integer, CurrencyUnit> currenciesByNumericCode = new ConcurrentHashMap<>();
6868
/**
6969
* Map of registered currencies by country.
7070
*/
71-
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCountry = new ConcurrentSkipListMap<String, CurrencyUnit>();
71+
private static final ConcurrentMap<String, CurrencyUnit> currenciesByCountry = new ConcurrentSkipListMap<>();
7272
static {
7373
// load one data provider by system property
7474
try {
7575
try {
76-
String clsName = System.getProperty(
76+
var clsName = System.getProperty(
7777
"org.joda.money.CurrencyUnitDataProvider",
7878
"org.joda.money.DefaultCurrencyUnitDataProvider");
7979
Class<? extends CurrencyUnitDataProvider> cls =
@@ -207,7 +207,7 @@ public static synchronized CurrencyUnit registerCurrency(
207207
if (currencyCode.length() != 3) {
208208
throw new IllegalArgumentException("Invalid string code, must be length 3");
209209
}
210-
if (CODE.matcher(currencyCode).matches() == false) {
210+
if (!CODE.matcher(currencyCode).matches()) {
211211
throw new IllegalArgumentException("Invalid string code, must be ASCII upper-case letters");
212212
}
213213
if (numericCurrencyCode < -1 || numericCurrencyCode > 999) {
@@ -218,7 +218,7 @@ public static synchronized CurrencyUnit registerCurrency(
218218
}
219219
MoneyUtils.checkNotNull(countryCodes, "Country codes must not be null");
220220

221-
CurrencyUnit currency = new CurrencyUnit(currencyCode, (short) numericCurrencyCode, (short) decimalPlaces);
221+
var currency = new CurrencyUnit(currencyCode, (short) numericCurrencyCode, (short) decimalPlaces);
222222
if (force) {
223223
currenciesByCode.remove(currencyCode);
224224
currenciesByNumericCode.remove(numericCurrencyCode);
@@ -354,7 +354,7 @@ public static CurrencyUnit of(Currency currency) {
354354
@FromString
355355
public static CurrencyUnit of(String currencyCode) {
356356
MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null");
357-
CurrencyUnit currency = currenciesByCode.get(currencyCode);
357+
var currency = currenciesByCode.get(currencyCode);
358358
if (currency == null) {
359359
throw new IllegalCurrencyException("Unknown currency '" + currencyCode + '\'');
360360
}
@@ -373,21 +373,17 @@ public static CurrencyUnit of(String currencyCode) {
373373
*/
374374
public static CurrencyUnit ofNumericCode(String numericCurrencyCode) {
375375
MoneyUtils.checkNotNull(numericCurrencyCode, "Currency code must not be null");
376-
switch (numericCurrencyCode.length()) {
377-
case 1:
378-
return ofNumericCode(numericCurrencyCode.charAt(0) - '0');
379-
case 2:
380-
return ofNumericCode(
381-
(numericCurrencyCode.charAt(0) - '0') * 10 +
382-
numericCurrencyCode.charAt(1) - '0');
383-
case 3:
384-
return ofNumericCode(
385-
(numericCurrencyCode.charAt(0) - '0') * 100 +
386-
(numericCurrencyCode.charAt(1) - '0') * 10 +
387-
numericCurrencyCode.charAt(2) - '0');
388-
default:
389-
throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\'');
390-
}
376+
return switch (numericCurrencyCode.length()) {
377+
case 1 -> ofNumericCode(numericCurrencyCode.charAt(0) - '0');
378+
case 2 -> ofNumericCode(
379+
(numericCurrencyCode.charAt(0) - '0') * 10 +
380+
numericCurrencyCode.charAt(1) - '0');
381+
case 3 -> ofNumericCode(
382+
(numericCurrencyCode.charAt(0) - '0') * 100 +
383+
(numericCurrencyCode.charAt(1) - '0') * 10 +
384+
numericCurrencyCode.charAt(2) - '0');
385+
default -> throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\'');
386+
};
391387
}
392388

393389
/**
@@ -400,7 +396,7 @@ public static CurrencyUnit ofNumericCode(String numericCurrencyCode) {
400396
* @throws IllegalCurrencyException if the currency is unknown
401397
*/
402398
public static CurrencyUnit ofNumericCode(int numericCurrencyCode) {
403-
CurrencyUnit currency = currenciesByNumericCode.get(numericCurrencyCode);
399+
var currency = currenciesByNumericCode.get(numericCurrencyCode);
404400
if (currency == null) {
405401
throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\'');
406402
}
@@ -418,7 +414,7 @@ public static CurrencyUnit ofNumericCode(int numericCurrencyCode) {
418414
*/
419415
public static CurrencyUnit of(Locale locale) {
420416
MoneyUtils.checkNotNull(locale, "Locale must not be null");
421-
CurrencyUnit currency = currenciesByCountry.get(locale.getCountry());
417+
var currency = currenciesByCountry.get(locale.getCountry());
422418
if (currency == null) {
423419
throw new IllegalCurrencyException("No currency found for locale '" + locale + '\'');
424420
}
@@ -437,7 +433,7 @@ public static CurrencyUnit of(Locale locale) {
437433
*/
438434
public static CurrencyUnit ofCountry(String countryCode) {
439435
MoneyUtils.checkNotNull(countryCode, "Country code must not be null");
440-
CurrencyUnit currency = currenciesByCountry.get(countryCode);
436+
var currency = currenciesByCountry.get(countryCode);
441437
if (currency == null) {
442438
throw new IllegalCurrencyException("No currency found for country '" + countryCode + '\'');
443439
}
@@ -447,7 +443,7 @@ public static CurrencyUnit ofCountry(String countryCode) {
447443
//-----------------------------------------------------------------------
448444
/**
449445
* Constructor, creating a new currency instance.
450-
*
446+
*
451447
* @param code the three-letter currency code, not null
452448
* @param numericCode the numeric currency code, from 0 to 999, -1 if none
453449
* @param decimalPlaces the decimal places, not null
@@ -461,7 +457,7 @@ public static CurrencyUnit ofCountry(String countryCode) {
461457

462458
/**
463459
* Block malicious data streams.
464-
*
460+
*
465461
* @param ois the input stream, not null
466462
* @throws InvalidObjectException if an error occurs
467463
*/
@@ -471,7 +467,7 @@ private void readObject(ObjectInputStream ois) throws InvalidObjectException {
471467

472468
/**
473469
* Uses a serialization delegate.
474-
*
470+
*
475471
* @return the replacing object, never null
476472
*/
477473
private Object writeReplace() {
@@ -483,7 +479,7 @@ private Object writeReplace() {
483479
* Gets the ISO-4217 three-letter currency code.
484480
* <p>
485481
* Each currency is uniquely identified by a three-letter upper-case code, based on ISO-4217.
486-
*
482+
*
487483
* @return the three-letter upper-case currency code, never null
488484
*/
489485
public String getCode() {
@@ -494,7 +490,7 @@ public String getCode() {
494490
* Gets the ISO-4217 numeric currency code.
495491
* <p>
496492
* The numeric code is an alternative to the standard string-based code.
497-
*
493+
*
498494
* @return the numeric currency code, -1 if no numeric code
499495
*/
500496
public int getNumericCode() {
@@ -506,14 +502,14 @@ public int getNumericCode() {
506502
* <p>
507503
* This formats the numeric code as a three digit string prefixed by zeroes if necessary.
508504
* If there is no valid code, then an empty string is returned.
509-
*
505+
*
510506
* @return the three digit numeric currency code, empty is no code, never null
511507
*/
512508
public String getNumeric3Code() {
513509
if (numericCode < 0) {
514510
return "";
515511
}
516-
String str = Integer.toString(numericCode);
512+
var str = Integer.toString(numericCode);
517513
if (str.length() == 1) {
518514
return "00" + str;
519515
}
@@ -529,11 +525,11 @@ public String getNumeric3Code() {
529525
* A currency is typically valid in one or more countries.
530526
* The codes are typically defined by ISO-3166.
531527
* An empty set indicates that no the currency is not associated with a country code.
532-
*
528+
*
533529
* @return the country codes, may be empty, not null
534530
*/
535531
public Set<String> getCountryCodes() {
536-
Set<String> countryCodes = new HashSet<String>();
532+
Set<String> countryCodes = new HashSet<>();
537533
for (Entry<String, CurrencyUnit> entry : currenciesByCountry.entrySet()) {
538534
if (this.equals(entry.getValue())) {
539535
countryCodes.add(entry.getKey());
@@ -549,7 +545,7 @@ public Set<String> getCountryCodes() {
549545
* Different currencies have different numbers of decimal places by default.
550546
* For example, 'GBP' has 2 decimal places, but 'JPY' has zero.
551547
* Pseudo-currencies will return zero.
552-
*
548+
*
553549
* @return the decimal places, from 0 to 9 (normally 0, 2 or 3)
554550
*/
555551
public int getDecimalPlaces() {
@@ -558,7 +554,7 @@ public int getDecimalPlaces() {
558554

559555
/**
560556
* Checks if this is a pseudo-currency.
561-
*
557+
*
562558
* @return true if this is a pseudo-currency
563559
*/
564560
public boolean isPseudoCurrency() {
@@ -573,7 +569,7 @@ public boolean isPseudoCurrency() {
573569
* is returned.
574570
* <p>
575571
* This method matches the API of {@link Currency}.
576-
*
572+
*
577573
* @return the JDK currency instance, never null
578574
*/
579575
public String getSymbol() {
@@ -595,7 +591,7 @@ public String getSymbol() {
595591
* is returned.
596592
* <p>
597593
* This method matches the API of {@link Currency}.
598-
*
594+
*
599595
* @param locale the locale to get the symbol for, not null
600596
* @return the JDK currency instance, never null
601597
*/
@@ -617,7 +613,7 @@ public String getSymbol(Locale locale) {
617613
* Gets the JDK currency instance equivalent to this currency.
618614
* <p>
619615
* This attempts to convert a {@code CurrencyUnit} to a JDK {@code Currency}.
620-
*
616+
*
621617
* @return the JDK currency instance, never null
622618
* @throws IllegalArgumentException if no matching currency exists in the JDK
623619
*/
@@ -628,7 +624,7 @@ public Currency toCurrency() {
628624
//-----------------------------------------------------------------------
629625
/**
630626
* Compares this currency to another by alphabetical comparison of the code.
631-
*
627+
*
632628
* @param other the other currency, not null
633629
* @return negative if earlier alphabetically, 0 if equal, positive if greater alphabetically
634630
*/
@@ -641,7 +637,7 @@ public int compareTo(CurrencyUnit other) {
641637
* Checks if this currency equals another currency.
642638
* <p>
643639
* The comparison checks the 3 letter currency code.
644-
*
640+
*
645641
* @param obj the other currency, null returns false
646642
* @return true if equal
647643
*/
@@ -658,7 +654,7 @@ public boolean equals(Object obj) {
658654

659655
/**
660656
* Returns a suitable hash code for the currency.
661-
*
657+
*
662658
* @return the hash code
663659
*/
664660
@Override
@@ -669,7 +665,7 @@ public int hashCode() {
669665
//-----------------------------------------------------------------------
670666
/**
671667
* Gets the currency code as a string.
672-
*
668+
*
673669
* @return the currency code, never null
674670
*/
675671
@Override

src/main/java/org/joda/money/CurrencyUnitDataProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public abstract class CurrencyUnitDataProvider {
2222

2323
/**
2424
* Registers all the currencies known by this provider.
25-
*
25+
*
2626
* @throws Exception if an error occurs
2727
*/
2828
protected abstract void registerCurrencies() throws Exception;

0 commit comments

Comments
 (0)