/
Waehrung.java
277 lines (250 loc) · 7.95 KB
/
Waehrung.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
/*
* Copyright (c) 2018 by Oliver Boehm
*
* 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 04.08.18 by oliver (ob@oasd.de)
*/
package de.jfachwert.bank;
import de.jfachwert.AbstractFachwert;
import de.jfachwert.pruefung.exception.InvalidValueException;
import org.apache.commons.collections4.map.ReferenceMap;
import javax.money.CurrencyContext;
import javax.money.CurrencyUnit;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Class Waehrung.
*
* @author <a href="ob@aosd.de">oliver</a>
* @since 1.0
*/
public class Waehrung extends AbstractFachwert<Currency> implements Comparable<CurrencyUnit>, CurrencyUnit {
private static final Logger LOG = Logger.getLogger(Waehrung.class.getName());
private static final Map<String, Waehrung> CACHE = new ReferenceMap<>();
/** Default-Waehrung, die durch die Landeseinstellung (Locale) vorgegeben wird. */
public static final Currency DEFAULT_CURRENCY = getDefaultCurrency();
/** Default-Waehrung, die durch die Landeseinstellung (Locale) vorgegeben wird. */
public static final Waehrung DEFAULT = new Waehrung(DEFAULT_CURRENCY);
static {
CACHE.put(DEFAULT_CURRENCY.getCurrencyCode(), DEFAULT);
}
/** Die Euro-Waehrung als Konstante. */
public static final Waehrung EUR = Waehrung.of("EUR");
/**
* Darueber kann eine Waehrung angelegt werden.
*
* @param code z.B. "EUR"
*/
public Waehrung(String code) {
this(toCurrency(code));
}
/**
* Darueber kann eine Waehrung angelegt werden.
*
* @param code Waehrung
*/
public Waehrung(Currency code) {
super(code);
}
/**
* Gibt die entsprechende Currency als Waehrung zurueck. Da die Anzahl der
* Waehrungen ueberschaubar ist, werden sie in einem dauerhaften Cache
* vorgehalten.
*
* @param currency Currency
* @return Waehrung
*/
public static Waehrung of(Currency currency) {
String key = currency.getCurrencyCode();
return CACHE.computeIfAbsent(key, t -> new Waehrung(currency));
}
/**
* Gibt die entsprechende Currency als Waehrung zurueck.
*
* @param currencyUnit CurrencyUnit
* @return Waehrung
*/
public static Waehrung of(CurrencyUnit currencyUnit) {
if (currencyUnit instanceof Waehrung) {
return (Waehrung) currencyUnit;
} else {
return of(currencyUnit.getCurrencyCode());
}
}
/**
* Gibt die entsprechende Currency als Waehrung zurueck.
*
* @param currency Waehrung, z.B. "EUR"
* @return Waehrung
*/
public static Waehrung of(String currency) {
return of(toCurrency(currency));
}
/**
* Ermittelt aus dem uebergebenen String die entsprechende
* {@link Currency}.
*
* @param name z.B. "EUR" oder auch ein einzelnes Symbol
* @return die entsprechende Waehrung
*/
public static Currency toCurrency(String name) {
try {
return Currency.getInstance(name);
} catch (IllegalArgumentException iae) {
if (name.length() <= 3) {
for (Currency c : Currency.getAvailableCurrencies()) {
if (matchesCurrency(name, c)) {
return c;
}
}
}
return toFallbackCurrency(name, iae);
}
}
private static boolean matchesCurrency(String name, Currency c) {
return name.equalsIgnoreCase(c.getCurrencyCode()) || name.equalsIgnoreCase(c.getSymbol());
}
private static Currency toFallbackCurrency(String name, IllegalArgumentException iae) {
if (name.equals("\u20ac")) {
return Currency.getInstance("EUR");
} else {
throw new IllegalArgumentException("cannot get currency for '" + name + "'", iae);
}
}
/**
* Validiert den uebergebenen Waehrungscode.
*
* @param code Waehrungscode als String
* @return Waehrungscode zur Weiterverarbeitung
*/
public static String validate(String code) {
try {
toCurrency(code);
} catch (IllegalArgumentException ex) {
throw new InvalidValueException(code, "currency");
}
return code;
}
/**
* Liefert die Currency zurueck.
*
* @return die Currency aus java.util.
*/
public Currency getCurrency() {
return getCode();
}
/**
* Liefert den Waehrungscode.
*
* @return z.B. "EUR"
*/
@Override
public String getCurrencyCode() {
return getCode().getCurrencyCode();
}
/**
* Liefert den numerischen Waehrungscode.
*
* @return z.B. 978 fuer EUro
*/
@Override
public int getNumericCode() {
return getCode().getNumericCode();
}
/**
* Liefert die Anzahl der Nachkommastellen einer Waehrung.
*
* @return meist 2, manchmal 0
*/
@Override
public int getDefaultFractionDigits() {
return getCode().getDefaultFractionDigits();
}
@Override
public CurrencyContext getContext() {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Liefert das Waehrungssymbol.
*
* @return z.B. "$"
*/
public String getSymbol() {
return getCode().getSymbol();
}
/**
* Lieft das Waehrungssymbol der uebergebenen Waehrungseinheit.
*
* @param cu Waehrungseinheit
* @return z.B. das Euro-Zeichen
*/
public static String getSymbol(CurrencyUnit cu) {
try {
return Waehrung.of(cu).getSymbol();
} catch (IllegalArgumentException ex) {
LOG.log(Level.WARNING, "Cannot get symbol for '" + cu + "':", ex);
return cu.getCurrencyCode();
}
}
/**
* Zum Vergleich wird der Waehrungscode herangezogen und alphabetisch
* verglichen.
*
* @param other die andere Waerhung
* @return eine negative Zahl wenn die ander Waehrung alphabetisch
* danach kommt.
*/
@Override
public int compareTo(CurrencyUnit other) {
return getCurrencyCode().compareTo(other.getCurrencyCode());
}
/**
* Als toString-Implementierung wird der Waehrungscode ausgegeben.
*
* @return z.B. "EUR"
*/
@Override
public String toString() {
return getCurrencyCode();
}
/**
* Ermittelt die Waehrung. Urspruenglich wurde die Default-Currency ueber
* <pre>
* Currency.getInstance(Locale.getDefault())
* </pre>
* ermittelt. Dies fuehrte aber auf der Sun zu Problemen, da dort
* die Currency fuer die Default-Locale folgende Exception hervorrief:
* <pre>
* java.lang.IllegalArgumentException
* at java.util.Currency.getInstance(Currency.java:384)
* at de.jfachwert.bank.Geldbetrag.<clinit>
* ...
* </pre>
*
* @return normalerweise die deutsche Currency
*/
private static Currency getDefaultCurrency() {
Locale[] locales = { Locale.getDefault(), Locale.GERMANY, Locale.GERMAN };
for (Locale loc : locales) {
try {
return Currency.getInstance(loc);
} catch (IllegalArgumentException iae) {
LOG.log(Level.INFO,
"No currency for locale '" + loc + "' available on this machine - will try next one.", iae);
}
}
return Currency.getAvailableCurrencies().iterator().next();
}
}