-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Monetary.java
514 lines (472 loc) · 24.2 KB
/
Monetary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/*
* CREDIT SUISSE IS WILLING TO LICENSE THIS SPECIFICATION TO YOU ONLY UPON THE
* CONDITION THAT YOU ACCEPT ALL OF THE TERMS CONTAINED IN THIS AGREEMENT.
* PLEASE READ THE TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY. BY
* DOWNLOADING THIS SPECIFICATION, YOU ACCEPT THE TERMS AND CONDITIONS OF THE
* AGREEMENT. IF YOU ARE NOT WILLING TO BE BOUND BY IT, SELECT THE "DECLINE"
* BUTTON AT THE BOTTOM OF THIS PAGE.
*
* Specification: JSR-354 Money and Currency API ("Specification")
*
* Copyright (c) 2012-2015, Credit Suisse All rights reserved.
*/
package javax.money;
import javax.money.spi.Bootstrap;
import javax.money.spi.CurrencyProviderSpi;
import javax.money.spi.MonetaryAmountsSingletonQuerySpi;
import javax.money.spi.MonetaryAmountsSingletonSpi;
import javax.money.spi.MonetaryCurrenciesSingletonSpi;
import javax.money.spi.MonetaryRoundingsSingletonSpi;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Factory singleton for {@link CurrencyUnit}, {@link javax.money.MonetaryAmount} and
* {@link javax.money.MonetaryRounding} instances as provided by the
* different registered SPI instances.
* <p/>
* This class is thread safe.
*
* @author Anatole Tresch
*/
public final class Monetary {
/**
* The used {@link javax.money.spi.MonetaryCurrenciesSingletonSpi} instance.
*/
private static MonetaryCurrenciesSingletonSpi MONETARY_CURRENCIES_SINGLETON_SPI() {
try {
return Optional.ofNullable(Bootstrap
.getService(MonetaryCurrenciesSingletonSpi.class)).orElseGet(
DefaultMonetaryCurrenciesSingletonSpi::new);
} catch (Exception e) {
Logger.getLogger(Monetary.class.getName())
.log(Level.INFO, "Failed to load MonetaryCurrenciesSingletonSpi, using default.", e);
return new DefaultMonetaryCurrenciesSingletonSpi();
}
}
/**
* The used {@link javax.money.spi.MonetaryAmountsSingletonSpi} instance.
*/
private static MonetaryAmountsSingletonSpi monetaryAmountsSingletonSpi() {
try {
return Bootstrap.getService(MonetaryAmountsSingletonSpi.class);
} catch (Exception e) {
Logger.getLogger(Monetary.class.getName())
.log(Level.SEVERE, "Failed to load MonetaryAmountsSingletonSpi.", e);
return null;
}
}
/**
* The used {@link javax.money.spi.MonetaryAmountsSingletonSpi} instance.
*/
private static MonetaryAmountsSingletonQuerySpi MONETARY_AMOUNTS_SINGLETON_QUERY_SPI() {
try {
return Bootstrap.getService(MonetaryAmountsSingletonQuerySpi.class);
} catch (Exception e) {
Logger.getLogger(Monetary.class.getName()).log(Level.SEVERE, "Failed to load " +
"MonetaryAmountsSingletonQuerySpi, " +
"query functionality will not be " +
"available.", e);
return null;
}
}
/**
* The used {@link javax.money.spi.MonetaryCurrenciesSingletonSpi} instance.
*/
private static MonetaryRoundingsSingletonSpi MONETARY_ROUNDINGS_SINGLETON_SPI() {
try {
return Optional.ofNullable(Bootstrap
.getService(MonetaryRoundingsSingletonSpi.class))
.orElseGet(DefaultMonetaryRoundingsSingletonSpi::new);
} catch (Exception e) {
Logger.getLogger(Monetary.class.getName())
.log(Level.SEVERE, "Failed to load MonetaryCurrenciesSingletonSpi, using default.", e);
return new DefaultMonetaryRoundingsSingletonSpi();
}
}
/**
* Required for deserialization only.
*/
private Monetary() {
}
/**
* Allows to access the names of the current registered providers.
*
* @return the set of provider names, never {@code null}.
*/
public static Set<String> getRoundingProviderNames() {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getProviderNames();
}
/**
* Allows to access the default providers chain used if no provider chain was passed explicitly..
*
* @return the chained list of provider names, never {@code null}.
*/
public static List<String> getDefaultRoundingProviderChain() {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getDefaultProviderChain();
}
/**
* Creates a rounding that can be added as {@link MonetaryOperator} to
* chained calculations. The instance will lookup the concrete
* {@link MonetaryOperator} instance from the {@link Monetary}
* based on the input {@link MonetaryAmount}'s {@link CurrencyUnit}.
*
* @return the (shared) default rounding instance.
*/
public static MonetaryRounding getDefaultRounding() {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getDefaultRounding();
}
/**
* Creates an {@link MonetaryOperator} for rounding {@link MonetaryAmount}
* instances given a currency.
*
* @param currencyUnit The currency, which determines the required scale. As
* {@link java.math.RoundingMode}, by default, {@link java.math.RoundingMode#HALF_UP}
* is used.
* @param providers the providers and ordering to be used. By default providers and ordering as defined in
* #getDefaultProviders is used.
* @return a new instance {@link MonetaryOperator} implementing the
* rounding, never {@code null}.
*/
public static MonetaryRounding getRounding(CurrencyUnit currencyUnit, String... providers) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getRounding(currencyUnit, providers);
}
/**
* Access an {@link MonetaryOperator} for custom rounding
* {@link MonetaryAmount} instances.
*
* @param roundingName The rounding identifier.
* @param providers the providers and ordering to be used. By default providers and ordering as defined in
* #getDefaultProviders is used.
* @return the corresponding {@link MonetaryOperator} implementing the
* rounding, never {@code null}.
* @throws IllegalArgumentException if no such rounding is registered using a
* {@link javax.money.spi.RoundingProviderSpi} instance.
*/
public static MonetaryRounding getRounding(String roundingName, String... providers) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getRounding(roundingName, providers);
}
/**
* Access a {@link MonetaryRounding} using a possibly complex query.
*
* @param roundingQuery The {@link javax.money.RoundingQuery} that may contains arbitrary parameters to be
* evaluated.
* @return the corresponding {@link javax.money.MonetaryRounding}, never {@code null}.
* @throws IllegalArgumentException if no such rounding is registered using a
* {@link javax.money.spi.RoundingProviderSpi} instance.
*/
public static MonetaryRounding getRounding(RoundingQuery roundingQuery) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getRounding(roundingQuery);
}
/**
* Checks if a {@link MonetaryRounding} is available given a roundingId.
*
* @param roundingName The rounding identifier.
* @param providers the providers and ordering to be used. By default providers and ordering as defined in
* #getDefaultProviders is used.
* @return true, if a corresponding {@link javax.money.MonetaryRounding} is available.
* @throws IllegalArgumentException if no such rounding is registered using a
* {@link javax.money.spi.RoundingProviderSpi} instance.
*/
public static boolean isRoundingAvailable(String roundingName, String... providers) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.isRoundingAvailable(roundingName, providers);
}
/**
* Checks if a {@link MonetaryRounding} is available given a roundingId.
*
* @param currencyUnit The currency, which determines the required scale. As {@link java.math.RoundingMode},
* by default, {@link java.math.RoundingMode#HALF_UP} is used.
* @param providers the providers and ordering to be used. By default providers and ordering as defined in
* #getDefaultProviders is used.
* @return true, if a corresponding {@link javax.money.MonetaryRounding} is available.
* @throws IllegalArgumentException if no such rounding is registered using a
* {@link javax.money.spi.RoundingProviderSpi} instance.
*/
public static boolean isRoundingAvailable(CurrencyUnit currencyUnit, String... providers) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.isRoundingAvailable(currencyUnit, providers);
}
/**
* Checks if a {@link MonetaryRounding} matching the query is available.
*
* @param roundingQuery The {@link javax.money.RoundingQuery} that may contains arbitrary parameters to be
* evaluated.
* @return true, if a corresponding {@link javax.money.MonetaryRounding} is available.
* @throws IllegalArgumentException if no such rounding is registered using a
* {@link javax.money.spi.RoundingProviderSpi} instance.
*/
public static boolean isRoundingAvailable(RoundingQuery roundingQuery) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.isRoundingAvailable(roundingQuery);
}
/**
* Access multiple {@link MonetaryRounding} instances using a possibly complex query
*
* @param roundingQuery The {@link javax.money.RoundingQuery} that may contains arbitrary parameters to be
* evaluated.
* @return all {@link javax.money.MonetaryRounding} instances matching the query, never {@code null}.
*/
public static Collection<MonetaryRounding> getRoundings(RoundingQuery roundingQuery) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getRoundings(roundingQuery);
}
/**
* Allows to access the names of the current defined roundings.
*
* @param providers the providers and ordering to be used. By default providers and ordering as defined in
* #getDefaultProviders is used.
* @return the set of custom rounding ids, never {@code null}.
*/
public static Set<String> getRoundingNames(String... providers) {
return Optional.ofNullable(MONETARY_ROUNDINGS_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryRoundingsSpi loaded, query functionality is not available."))
.getRoundingNames(providers);
}
/**
* Access an {@link MonetaryAmountFactory} for the given {@link MonetaryAmount} implementation
* type.
*
* @param amountType {@link MonetaryAmount} implementation type, nor {@code null}.
* @return the corresponding {@link MonetaryAmountFactory}, never {@code null}.
* @throws MonetaryException if no {@link MonetaryAmountFactory} targeting the given {@link MonetaryAmount}
* implementation class is registered.
*/
public static <T extends MonetaryAmount> MonetaryAmountFactory<T> getAmountFactory(Class<T> amountType) {
Optional.ofNullable(monetaryAmountsSingletonSpi())
.orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded."));
MonetaryAmountFactory<T> factory = monetaryAmountsSingletonSpi().getAmountFactory(amountType);
return Optional.ofNullable(factory).orElseThrow(
() -> new MonetaryException("No AmountFactory available for type: " + amountType.getName()));
}
/**
* Access the default {@link MonetaryAmountFactory} as defined by
* {@link javax.money.spi.MonetaryAmountsSingletonSpi#getDefaultAmountFactory()}.
*
* @return the {@link MonetaryAmountFactory} corresponding to default amount type,
* never {@code null}.
* @throws MonetaryException if no {@link MonetaryAmountFactory} targeting the default amount type
* implementation class is registered.
*/
public static MonetaryAmountFactory<?> getDefaultAmountFactory() {
return Optional.ofNullable(monetaryAmountsSingletonSpi())
.orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded."))
.getDefaultAmountFactory();
}
/**
* Access all currently available {@link MonetaryAmount} implementation classes that are
* accessible from this {@link MonetaryAmount} singleton.
*
* @return all currently available {@link MonetaryAmount} implementation classes that have
* corresponding {@link MonetaryAmountFactory} instances provided, never {@code null}
*/
public static Collection<MonetaryAmountFactory<?>> getAmountFactories() {
return Optional.ofNullable(monetaryAmountsSingletonSpi())
.orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded."))
.getAmountFactories();
}
/**
* Access all currently available {@link MonetaryAmount} implementation classes that are
* accessible from this {@link MonetaryAmount} singleton.
*
* @return all currently available {@link MonetaryAmount} implementation classes that have
* corresponding {@link MonetaryAmountFactory} instances provided, never {@code null}
*/
public static Collection<Class<? extends MonetaryAmount>> getAmountTypes() {
return Optional.ofNullable(monetaryAmountsSingletonSpi())
.orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded.")).getAmountTypes();
}
/**
* Access the default {@link MonetaryAmount} implementation class that is
* accessible from this {@link MonetaryAmount} singleton.
*
* @return all current default {@link MonetaryAmount} implementation class, never {@code null}
*/
public static Class<? extends MonetaryAmount> getDefaultAmountType() {
return Optional.ofNullable(monetaryAmountsSingletonSpi())
.orElseThrow(() -> new MonetaryException("No MonetaryAmountsSingletonSpi loaded."))
.getDefaultAmountType();
}
/**
* Executes the query and returns the factory found, if there is only one factory.
* If multiple factories match the query, one is selected.
*
* @param query the factory query, not null.
* @return the factory found, or null.
*/
@SuppressWarnings("rawtypes")
public static MonetaryAmountFactory getAmountFactory(MonetaryAmountFactoryQuery query) {
return Optional.ofNullable(MONETARY_AMOUNTS_SINGLETON_QUERY_SPI()).orElseThrow(() -> new MonetaryException(
"No MonetaryAmountsSingletonQuerySpi loaded, query functionality is not available."))
.getAmountFactory(query);
}
/**
* Returns all factory instances that match the query.
*
* @param query the factory query, not null.
* @return the instances found, never null.
*/
public static Collection<MonetaryAmountFactory<?>> getAmountFactories(MonetaryAmountFactoryQuery query) {
return Optional.ofNullable(MONETARY_AMOUNTS_SINGLETON_QUERY_SPI()).orElseThrow(() -> new MonetaryException(
"No MonetaryAmountsSingletonQuerySpi loaded, query functionality is not available."))
.getAmountFactories(query);
}
/**
* Allows to check if any of the <i>get</i>XXX methods return non empty/non null results of {@link javax.money
* .MonetaryAmountFactory}.
*
* @param query the factory query, not null.
* @return true, if at least one {@link MonetaryAmountFactory} matches the query.
*/
public static boolean isAvailable(MonetaryAmountFactoryQuery query) {
return Optional.ofNullable(MONETARY_AMOUNTS_SINGLETON_QUERY_SPI()).orElseThrow(() -> new MonetaryException(
"No MonetaryAmountsSingletonQuerySpi loaded, query functionality is not available."))
.isAvailable(query);
}
/**
* Access a new instance based on the currency code. Currencies are
* available as provided by {@link CurrencyProviderSpi} instances registered
* with the {@link javax.money.spi.Bootstrap}.
*
* @param currencyCode the ISO currency code, not {@code null}.
* @param providers the (optional) specification of providers to consider.
* @return the corresponding {@link CurrencyUnit} instance.
* @throws UnknownCurrencyException if no such currency exists.
*/
public static CurrencyUnit getCurrency(String currencyCode, String... providers) {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getCurrency(currencyCode, providers);
}
/**
* Access a new instance based on the {@link Locale}. Currencies are
* available as provided by {@link CurrencyProviderSpi} instances registered
* with the {@link javax.money.spi.Bootstrap}.
*
* @param locale the target {@link Locale}, typically representing an ISO
* country, not {@code null}.
* @param providers the (optional) specification of providers to consider.
* @return the corresponding {@link CurrencyUnit} instance.
* @throws UnknownCurrencyException if no such currency exists.
*/
public static CurrencyUnit getCurrency(Locale locale, String... providers) {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getCurrency(locale, providers);
}
/**
* Access a new instance based on the {@link Locale}. Currencies are
* available as provided by {@link CurrencyProviderSpi} instances registered
* with the {@link javax.money.spi.Bootstrap}.
*
* @param locale the target {@link Locale}, typically representing an ISO
* country, not {@code null}.
* @param providers the (optional) specification of providers to consider.
* @return the corresponding {@link CurrencyUnit} instance.
* @throws UnknownCurrencyException if no such currency exists.
*/
public static Set<CurrencyUnit> getCurrencies(Locale locale, String... providers) {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getCurrencies(locale, providers);
}
/**
* Allows to check if a {@link CurrencyUnit} instance is defined, i.e.
* accessible from {@link Monetary#getCurrency(String, String...)}.
*
* @param code the currency code, not {@code null}.
* @param providers the (optional) specification of providers to consider.
* @return {@code true} if {@link Monetary#getCurrency(String, java.lang.String...)}
* would return a result for the given code.
*/
public static boolean isCurrencyAvailable(String code, String... providers) {
return Objects.nonNull(MONETARY_CURRENCIES_SINGLETON_SPI()) && MONETARY_CURRENCIES_SINGLETON_SPI().isCurrencyAvailable(code, providers);
}
/**
* Allows to check if a {@link javax.money.CurrencyUnit} instance is
* defined, i.e. accessible from {@link #getCurrency(String, String...)}.
*
* @param locale the target {@link Locale}, not {@code null}.
* @param providers the (optional) specification of providers to consider.
* @return {@code true} if {@link #getCurrencies(Locale, String...)} would return a
* result containing a currency with the given code.
*/
public static boolean isCurrencyAvailable(Locale locale, String... providers) {
return Objects.nonNull(MONETARY_CURRENCIES_SINGLETON_SPI()) && MONETARY_CURRENCIES_SINGLETON_SPI().isCurrencyAvailable(locale, providers);
}
/**
* Access all currencies known.
*
* @param providers the (optional) specification of providers to consider.
* @return the list of known currencies, never null.
*/
public static Collection<CurrencyUnit> getCurrencies(String... providers) {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getCurrencies(providers);
}
/**
* Query all currencies matching the given query.
*
* @param query The {@link javax.money.CurrencyQuery}, not null.
* @return the list of known currencies, never null.
*/
public static CurrencyUnit getCurrency(CurrencyQuery query) {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getCurrency(query);
}
/**
* Query all currencies matching the given query.
*
* @param query The {@link javax.money.CurrencyQuery}, not null.
* @return the list of known currencies, never null.
*/
public static Collection<CurrencyUnit> getCurrencies(CurrencyQuery query) {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getCurrencies(query);
}
/**
* Query all currencies matching the given query.
*
* @return the list of known currencies, never null.
*/
public static Set<String> getCurrencyProviderNames() {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getProviderNames();
}
/**
* Query the list and ordering of provider names modelling the default provider chain to be used, if no provider
* chain was explicitly set..
*
* @return the ordered list provider names, modelling the default provider chain used, never null.
*/
public static List<String> getDefaultCurrencyProviderChain() {
return Optional.ofNullable(MONETARY_CURRENCIES_SINGLETON_SPI()).orElseThrow(
() -> new MonetaryException("No MonetaryCurrenciesSingletonSpi loaded, check your system setup."))
.getDefaultProviderChain();
}
}