/
Geldbetrag.java
987 lines (913 loc) · 37.3 KB
/
Geldbetrag.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
/*
* 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 or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 18.07.2018 by oboehm (ob@oasd.de)
*/
package de.jfachwert.bank;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import de.jfachwert.Fachwert;
import de.jfachwert.pruefung.NullValidator;
import de.jfachwert.pruefung.NumberValidator;
import de.jfachwert.pruefung.exception.InvalidValueException;
import de.jfachwert.pruefung.exception.LocalizedMonetaryException;
import de.jfachwert.pruefung.exception.LocalizedValidationException;
import org.apache.commons.lang3.StringUtils;
import org.javamoney.moneta.spi.DefaultNumberValue;
import javax.money.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Currency;
/**
* Diese Klasse unterstuetzt sie JSR 354 und das{@link MonetaryAmount}
* Interface, das Bestandteil von Java 9 ist. Da in alten Anwendungen
* oftmals ein {@link BigDecimal} verwendet wurde, wird auch diese
* Schnittstelle weitgehende unterstützt. Einzige Unterschied ist
* die {@link MonetaryAmount#stripTrailingZeros()}-Methode, die einen anderen
* Rueckgabewert hat. Deswegen ist dies Klasse auch nicht von
* {@link BigDecimal} abgeleitet...
* <p>
* Im Gegensatz zur {@link org.javamoney.moneta.Money}- und
* {@link org.javamoney.moneta.FastMoney}-Klasse kann diese Klasse
* ueberschrieben werden, falls anderes Rundungsverhalten oder
* eine angepasste Implementierung benoetigt wird.
* </p>
*
* @author oboehm
* @since 0.8 (18.07.2018)
*/
@JsonSerialize(using = ToStringSerializer.class)
public class Geldbetrag implements MonetaryAmount, Fachwert {
private static final GeldbetragFactory FACTORY = new GeldbetragFactory();
/** Da 0-Betraege relativ haeufig vorkommen, spendieren wir dafuer eine eigene Konstante. */
public static final Geldbetrag ZERO = new Geldbetrag(BigDecimal.ZERO);
/** Der minimale Betrag, den wir unterstuetzen. */
public static final Geldbetrag MIN_VALUE = new Geldbetrag(BigDecimal.valueOf(Long.MIN_VALUE));
/** Der maximale Betrag, den wir unterstuetzen. */
public static final Geldbetrag MAX_VALUE = new Geldbetrag(BigDecimal.valueOf(Long.MAX_VALUE));
private final BigDecimal betrag;
private final Waehrung waehrung;
/**
* Erzeugt einen Geldbetrag in der aktuellen Landeswaehrung.
*
* @param betrag Geldbetrag, z.B. 1
*/
public Geldbetrag(long betrag) {
this(BigDecimal.valueOf(betrag));
}
/**
* Erzeugt einen Geldbetrag in der aktuellen Landeswaehrung.
*
* @param betrag Geldbetrag, z.B. 1.00
*/
public Geldbetrag(double betrag) {
this(BigDecimal.valueOf(betrag));
}
/**
* Erzeugt einen Geldbetrag in der aktuellen Landeswaehrung.
*
* @param betrag Geldbetrag, z.B. "1"
*/
public Geldbetrag(String betrag) {
this(Geldbetrag.valueOf(betrag));
}
/**
* Dies ist zum einen der CopyConstructor als Ersatz fuer eine
* clone-Methode, zum anderen wandelt es einen {@link MonetaryAmount}
* in ein GeldBetrag-Objekt.
*
* @param other der andere Geldbetrag
*/
public Geldbetrag(MonetaryAmount other) {
this(other.getNumber(), Currency.getInstance(other.getCurrency().getCurrencyCode()));
}
/**
* Erzeugt einen Geldbetrag in der aktuellen Landeswaehrung.
*
* @param betrag Geldbetrag, z.B. 1.00
*/
public Geldbetrag(Number betrag) {
this(betrag, Waehrung.DEFAULT_CURRENCY);
}
/**
* Erzeugt einen Geldbetrag in der angegebenen Waehrung.
*
* @param betrag Geldbetrag, z.B. 1.00
* @param currency Waehrung, z.B. Euro
*/
public Geldbetrag(Number betrag, Currency currency) {
this(betrag, Waehrung.of(currency));
}
/**
* Erzeugt einen Geldbetrag in der angegebenen Waehrung.
*
* @param betrag Geldbetrag, z.B. 1.00
* @param currency Waehrung, z.B. Euro
*/
public Geldbetrag(Number betrag, CurrencyUnit currency) {
this.betrag = validate(toBigDecimal(betrag), currency);
this.waehrung = Waehrung.of(currency);
}
/**
* Hierueber kann eine Geldbetrag ueber die Anzahl an Cents angelegt
* werden.
*
* @param cents Cent-Betrag, z.B. 42
* @return Geldbetrag, z.B. 0.42$
*/
public static Geldbetrag fromCent(long cents) {
return Geldbetrag.valueOf(cents).divide(100);
}
/**
* Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die
* Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine
* neues Objekt erzeugt, wenn es sich nicht vermeiden laesst.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @param other the other
* @return ein Geldbetrag
*/
public static Geldbetrag valueOf(String other) {
String trimmed = new NullValidator<String>().validate(other).trim();
String[] parts = StringUtils.splitByCharacterType(StringUtils.upperCase(trimmed));
if (parts.length == 0) {
throw new InvalidValueException(other, "money amount");
}
Currency cry = Waehrung.DEFAULT_CURRENCY;
String waehrung = parts[parts.length - 1];
if (!StringUtils.isNumericSpace(waehrung)) {
cry = toCurrency(waehrung);
trimmed = trimmed.substring(0, trimmed.length() - waehrung.length()).trim();
}
BigDecimal n = new BigDecimal(new NumberValidator().validate(trimmed));
return valueOf(n, cry);
}
private static Currency toCurrency(String waehrung) {
try {
return Currency.getInstance(waehrung);
} catch (IllegalArgumentException iae) {
for (Currency c : Currency.getAvailableCurrencies()) {
if (waehrung.equals(c.getSymbol())) {
return c;
}
}
throw new InvalidValueException(waehrung, "currency", iae);
}
}
/**
* Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die
* Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine
* neues Objekt erzeugt, wenn es sich nicht vermeiden laesst.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @param value Wert des andere Geldbetrags
* @return ein Geldbetrag
*/
public static Geldbetrag valueOf(long value) {
return valueOf(new Geldbetrag(value));
}
/**
* Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die
* Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine
* neues Objekt erzeugt, wenn es sich nicht vermeiden laesst.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @param value Wert des andere Geldbetrags
* @return ein Geldbetrag
*/
public static Geldbetrag valueOf(double value) {
return valueOf(new Geldbetrag(value));
}
/**
* Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die
* Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine
* neues Objekt erzeugt, wenn es sich nicht vermeiden laesst.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @param value Wert des andere Geldbetrags
* @param currency Waehrung des anderen Geldbetrags
* @return ein Geldbetrag
*/
public static Geldbetrag valueOf(Number value, Currency currency) {
return valueOf(new Geldbetrag(value, currency));
}
/**
* Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die
* Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine
* neues Objekt erzeugt, wenn es sich nicht vermeiden laesst.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @param value Wert des andere Geldbetrags
* @param currency Waehrung des anderen Geldbetrags
* @return ein Geldbetrag
*/
public static Geldbetrag valueOf(Number value, CurrencyUnit currency) {
return valueOf(new Geldbetrag(value, currency));
}
/**
* Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die
* Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine
* neues Objekt erzeugt, wenn es sich nicht vermeiden laesst.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @param other the other
* @return ein Geldbetrag
*/
public static Geldbetrag valueOf(MonetaryAmount other) {
if (other instanceof Geldbetrag) {
return (Geldbetrag) other;
}
BigDecimal value = other.getNumber().numberValue(BigDecimal.class);
if (value.equals(BigDecimal.ZERO)) {
return Geldbetrag.ZERO;
}
return new Geldbetrag(value).withCurrency(other.getCurrency());
}
/**
* Validiert die uebergebene Zahl, ob sie sich als Geldbetrag eignet.
*
* @param zahl als String
* @return die Zahl zur Weitervarabeitung
*/
public static String validate(String zahl) {
return Geldbetrag.valueOf(zahl).toString();
}
/**
* Validiert die uebergebene Zahl, ob die Praezision nicht zu hoch ist.
*
* @param zahl als String
* @return die Zahl zur Weitervarabeitung
*/
public static BigDecimal validate(BigDecimal zahl, CurrencyUnit currency) {
if (zahl.scale() > 4) {
throw new LocalizedValidationException("wrong precision: " + zahl);
}
if (zahl.scale() == 0) {
return zahl.setScale(currency.getDefaultFractionDigits(), RoundingMode.HALF_UP);
}
return zahl;
}
/**
* Liefert einen Geldbetrag mit der neuen gewuenschten Waehrung zurueck.
* Dabei findet <b>keine</b> Umrechnung statt.
* <p>
* Anmerkung: Der Prefix "with" kommt von der Namenskonvention in Scala
* fuer immutable Objekte.
* </p>
*
* @param unit die Waehrungseinheit
* @return Geldbetrag mit neuer Waehrung
*/
public Geldbetrag withCurrency(CurrencyUnit unit) {
return withCurrency(unit.getCurrencyCode());
}
/**
* Liefert einen Geldbetrag mit der neuen gewuenschten Waehrung zurueck.
* Dabei findet <b>keine</b> Umrechnung statt.
* <p>
* Anmerkung: Der Prefix "with" kommt von der Namenskonvention in Scala
* fuer immutable Objekte.
* </p>
*
* @param waehrung Waehrung
* @return Geldbetrag mit neuer Waehrung
*/
public Geldbetrag withCurrency(String waehrung) {
String normalized = waehrung.toUpperCase().trim();
if ("DM".equalsIgnoreCase(normalized)) {
normalized = "DEM";
}
return withCurrency(Currency.getInstance(normalized));
}
/**
* Liefert einen Geldbetrag mit der neuen gewuenschten Waehrung zurueck.
* Dabei findet <b>keine</b> Umrechnung statt.
* <p>
* Anmerkung: Der Prefix "with" kommt von der Namenskonvention in Scala
* fuer immutable Objekte.
* </p>
*
* @param currency Waehrung
* @return Geldbetrag mit neuer Waehrung
*/
public Geldbetrag withCurrency(Currency currency) {
return new Geldbetrag(this.getNumber(), currency);
}
/**
* Returns the {@link MonetaryContext} of this {@code MonetaryAmount}. The
* {@link MonetaryContext} provides additional information about the numeric representation and
* the numeric capabilities. This information can be used by code to determine situations where
* {@code MonetaryAmount} instances must be converted to avoid implicit truncation, which can
* lead to invalid results.
*
* @return the {@link MonetaryContext} of this {@code MonetaryAmount}, never {@code null} .
*/
@Override
public MonetaryContext getContext() {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Creates a new {@code MonetaryAmountFactory}, returning the same implementation type Hereby
* this given amount is used as a template, so reusing the {@link CurrencyUnit}, its numeric
* value, the algorithmic implementation as well as the current {@link MonetaryContext}.
* <p>
* This method is used for creating a new amount result after having done calculations that are
* not directly mappable to the default monetary arithmetics, e.g. waehrung conversion.
*
* @return the new {@code MonetaryAmountFactory} with the given {@link MonetaryAmount} as its
* default values.
*/
@Override
public GeldbetragFactory getFactory() {
return FACTORY;
}
/**
* Vergleicht zwei Instanzen von {@link MonetaryAmount}. Nicht signifikante
* Nachkommastellen werden dabei ignoriert.
*
* @param amount Betrag eines {@code MonetaryAmount}, mit dem verglichen werid
* @return {@code true} falls {@code amount > this}.
* @throws MonetaryException bei unterschiedlichen Waehrungen.
*/
@Override
public boolean isGreaterThan(MonetaryAmount amount) {
return this.compareTo(amount) > 0;
}
/**
* Vergleicht zwei Instanzen von {@link MonetaryAmount}. Nicht signifikante
* Nachkommastellen werden dabei ignoriert.
*
* @param amount Betrag eines {@code MonetaryAmount}, mit dem verglichen werid
* @return {@code true} falls {@code amount >= this}.
* @throws MonetaryException bei unterschiedlichen Waehrungen.
*/
@Override
public boolean isGreaterThanOrEqualTo(MonetaryAmount amount) {
return this.compareTo(amount) >= 0;
}
/**
* Vergleicht zwei Instanzen von {@link MonetaryAmount}. Nicht signifikante
* Nachkommastellen werden dabei ignoriert.
*
* @param amount Betrag eines {@code MonetaryAmount}, mit dem verglichen werid
* @return {@code true} falls {@code amount < this}.
* @throws MonetaryException bei unterschiedlichen Waehrungen.
*/
@Override
public boolean isLessThan(MonetaryAmount amount) {
return this.compareTo(amount) < 0;
}
/**
* Vergleicht zwei Instanzen von {@link MonetaryAmount}. Nicht signifikante
* Nachkommastellen werden dabei ignoriert.
*
* @param amount Betrag eines {@code MonetaryAmount}, mit dem verglichen werid
* @return {@code true} falls {@code amount <= this}.
* @throws MonetaryException bei unterschiedlichen Waehrungen.
*/
@Override
public boolean isLessThanOrEqualTo(MonetaryAmount amount) {
return this.compareTo(amount) <= 0;
}
/**
* Zwei Geldbetraege sind nur dann gleich, wenn sie die gleiche Waehrung
* und den gleichen Betrag haben.
*
* @param other der andere Geldbetrag oder MonetaryAmount
* @return true, falls Waehrung und Betrag gleich ist
* @throws MonetaryException wenn die Waehrungen nicht uebereinstimmen
*/
@Override
public boolean isEqualTo(MonetaryAmount other) {
checkCurrency(other);
return isNumberEqualTo(other.getNumber());
}
private boolean isNumberEqualTo(NumberValue value) {
BigDecimal otherValue = toBigDecimal(value);
BigDecimal thisValue = toBigDecimal(this.getNumber());
return thisValue.equals(otherValue);
}
private static BigDecimal toBigDecimal(NumberValue value) {
return value.numberValue(BigDecimal.class).setScale(2, RoundingMode.HALF_UP);
}
/**
* Returns the signum function of this {@code MonetaryAmount}.
*
* @return -1, 0, or 1 as the value of this {@code MonetaryAmount} is negative, zero, or
* positive.
*/
@Override
public int signum() {
return toBigDecimal(getNumber()).signum();
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this + amount</code>, and whose scale is
* <code>max(this.scale(),
* amount.scale()</code>.
*
* @param other value to be added to this {@code MonetaryAmount}.
* @return {@code this + amount}
* @throws ArithmeticException if the result exceeds the numeric capabilities of this implementation class, i.e.
* the {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public Geldbetrag add(MonetaryAmount other) {
checkCurrency(other);
if (other.isEqualTo(Geldbetrag.ZERO)) {
return this;
}
if (this.isEqualTo(Geldbetrag.ZERO)) {
return Geldbetrag.valueOf(other);
}
BigDecimal n = other.getNumber().numberValue(BigDecimal.class);
return new Geldbetrag(betrag.add(n));
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this -
* amount</code>, and whose scale is <code>max(this.scale(),
* subtrahend.scale()</code>.
*
* @param amount value to be subtracted from this {@code MonetaryAmount}.
* @return {@code this - amount}
* @throws ArithmeticException if the result exceeds the numeric capabilities of this implementation class, i.e.
* the {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public Geldbetrag subtract(MonetaryAmount amount) {
return add(amount.negate());
}
/**
* Returns a {@code MonetaryAmount} whose value is <tt>(this ×
* multiplicand)</tt>, and whose scale is <code>this.scale() +
* multiplicand.scale()</code>.
*
* @param multiplicand value to be multiplied by this {@code MonetaryAmount}.
* @return {@code this * multiplicand}
* @throws ArithmeticException if the result exceeds the numeric capabilities of this implementation class, i.e.
* the {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public MonetaryAmount multiply(long multiplicand) {
return multiply(BigDecimal.valueOf(multiplicand));
}
/**
* Returns a {@code MonetaryAmount} whose value is <tt>(this ×
* multiplicand)</tt>, and whose scale is <code>this.scale() +
* multiplicand.scale()</code>.
* By default the input value's scale will be rounded to
* accommodate the format capabilities, and no {@link ArithmeticException}
* is thrown if the input number's scale exceeds the capabilities.
*
* @param multiplicand value to be multiplied by this {@code MonetaryAmount}. If the multiplicand's scale exceeds
* the
* capabilities of the implementation, it may be rounded implicitly.
* @return {@code this * multiplicand}
* @throws ArithmeticException if the result exceeds the numeric capabilities of this implementation class, i.e.
* the {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public MonetaryAmount multiply(double multiplicand) {
return multiply(BigDecimal.valueOf(multiplicand));
}
/**
* Returns a {@code MonetaryAmount} whose value is <tt>(this ×
* multiplicand)</tt>, and whose scale is <code>this.scale() +
* multiplicand.scale()</code>.
*
* @param multiplicand value to be multiplied by this {@code MonetaryAmount}. If the multiplicand's scale exceeds
* the
* capabilities of the implementation, it may be rounded implicitly.
* @return {@code this * multiplicand}
* @throws ArithmeticException if the result exceeds the numeric capabilities of this implementation class, i.e.
* the {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public MonetaryAmount multiply(Number multiplicand) {
BigDecimal multiplied = betrag.multiply(toBigDecimal(multiplicand));
return Geldbetrag.valueOf(limitScale(multiplied), waehrung);
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this /
* divisor</code>, and whose preferred scale is <code>this.scale() -
* divisor.scale()</code>; if the exact quotient cannot be represented an {@code ArithmeticException}
* is thrown.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided.
* @return {@code this / divisor}
* @throws ArithmeticException if the exact quotient does not have a terminating decimal expansion, or if the
* result exceeds the numeric capabilities of this implementation class, i.e. the
* {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public Geldbetrag divide(long divisor) {
return divide(BigDecimal.valueOf(divisor));
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this /
* divisor</code>, and whose preferred scale is <code>this.scale() -
* divisor.scale()</code>; if the exact quotient cannot be represented an {@code ArithmeticException}
* is thrown.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided.
* @return {@code this / divisor}
* @throws ArithmeticException if the exact quotient does not have a terminating decimal expansion, or if the
* result exceeds the numeric capabilities of this implementation class, i.e. the
* {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public MonetaryAmount divide(double divisor) {
return divide(BigDecimal.valueOf(divisor));
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this /
* divisor</code>, and whose preferred scale is <code>this.scale() -
* divisor.scale()</code>; if the exact quotient cannot be represented an {@code ArithmeticException}
* is thrown.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided.
* @return {@code this / divisor}
* @throws ArithmeticException if the exact quotient does not have a terminating decimal expansion, or if the
* result exceeds the numeric capabilities of this implementation class, i.e. the
* {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public Geldbetrag divide(Number divisor) {
return Geldbetrag.valueOf(betrag.setScale(4, RoundingMode.HALF_UP).divide(toBigDecimal(divisor), RoundingMode.HALF_UP),
waehrung);
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this % divisor</code>.
* <p>
* <p>
* The remainder is given by
* <code>this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)</code> . Note that this
* is not the modulo operation (the result can be negative).
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided.
* @return {@code this % divisor}.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
*/
@Override
public Geldbetrag remainder(long divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this % divisor</code>.
* <p>
* <p>
* The remainder is given by
* <code>this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)</code> . Note that this
* is not the modulo operation (the result can be negative).
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided.
* @return {@code this % divisor}.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
*/
@Override
public Geldbetrag remainder(double divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>this % divisor</code>.
* <p>
* <p>
* The remainder is given by
* <code>this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)</code> . Note that this
* is not the modulo operation (the result can be negative).
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided.
* @return {@code this % divisor}.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
*/
@Override
public Geldbetrag remainder(Number divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a two-element {@code MonetaryAmount} array containing the result of
* {@code divideToIntegralValue} followed by the result of {@code remainder} on the two
* operands.
* <p>
* <p>
* Note that if both the integer quotient and remainder are needed, this method is faster than
* using the {@code divideToIntegralValue} and {@code remainder} methods separately because the
* division need only be carried out once.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided, and the remainder
* computed.
* @return a two element {@code MonetaryAmount} array: the quotient (the result of
* {@code divideToIntegralValue}) is the initial element and the remainder is the final
* element.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
* @see #divideToIntegralValue(long)
* @see #remainder(long)
*/
@Override
public MonetaryAmount[] divideAndRemainder(long divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a two-element {@code MonetaryAmount} array containing the result of
* {@code divideToIntegralValue} followed by the result of {@code remainder} on the two
* operands.
* <p>
* <p>
* Note that if both the integer quotient and remainder are needed, this method is faster than
* using the {@code divideToIntegralValue} and {@code remainder} methods separately because the
* division need only be carried out once.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided, and the remainder
* computed.
* @return a two element {@code MonetaryAmount} array: the quotient (the result of
* {@code divideToIntegralValue}) is the initial element and the remainder is the final
* element.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
* @see #divideToIntegralValue(double)
* @see #remainder(double)
*/
@Override
public MonetaryAmount[] divideAndRemainder(double divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a two-element {@code MonetaryAmount} array containing the result of
* {@code divideToIntegralValue} followed by the result of {@code remainder} on the two
* operands.
* <p>
* <p>
* Note that if both the integer quotient and remainder are needed, this method is faster than
* using the {@code divideToIntegralValue} and {@code remainder} methods separately because the
* division need only be carried out once.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided, and the remainder
* computed.
* @return a two element {@code MonetaryAmount} array: the quotient (the result of
* {@code divideToIntegralValue}) is the initial element and the remainder is the final
* element.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
* @see #divideToIntegralValue(Number)
* @see #remainder(Number)
*/
@Override
public MonetaryAmount[] divideAndRemainder(Number divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a {@code MonetaryAmount} whose value is the integer part of the quotient
* <code>this / divisor</code> rounded down. The preferred scale of the result is
* <code>this.scale() -
* divisor.scale()</code>.
*
* @param divisor value by which this {@code BigDecimal} is to be divided.
* @return The integer part of {@code this / divisor}.
* @throws ArithmeticException if {@code divisor==0}
* @see BigDecimal#divideToIntegralValue(BigDecimal)
*/
@Override
public Geldbetrag divideToIntegralValue(long divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a {@code MonetaryAmount} whose value is the integer part of the quotient
* <code>this / divisor</code> rounded down. The preferred scale of the result is
* <code>this.scale() - divisor.scale()</code>.
*
* @param divisor value by which this {@code BigDecimal} is to be divided.
* @return The integer part of {@code this / divisor}.
* @throws ArithmeticException if {@code divisor==0}
* @see BigDecimal#divideToIntegralValue(BigDecimal)
*/
@Override
public Geldbetrag divideToIntegralValue(double divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a {@code MonetaryAmount} whose value is the integer part of the quotient
* <code>this / divisor</code> rounded down. The preferred scale of the result is
* <code>this.scale() -
* divisor.scale()</code>.
*
* @param divisor value by which this {@code BigDecimal} is to be divided.
* @return The integer part of {@code this / divisor}.
* @throws ArithmeticException if {@code divisor==0}
* @see BigDecimal#divideToIntegralValue(BigDecimal)
*/
@Override
public Geldbetrag divideToIntegralValue(Number divisor) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a {@code MonetaryAmount} whose numerical value is equal to ( {@code this} *
* 10<sup>n</sup>). The scale of the result is <code>this.scale() - n</code>.
*
* @param power the power.
* @return the calculated amount value.
* @throws ArithmeticException if the scale would be outside the range of a 32-bit integer, or if the result
* exceeds the numeric capabilities of this implementation class, i.e. the
* {@link MonetaryContext} cannot be adapted as required.
*/
@Override
public Geldbetrag scaleByPowerOfTen(int power) {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Returns a {@code MonetaryAmount} whose value is the absolute value of this
* {@code MonetaryAmount}, and whose scale is {@code this.scale()}.
*
* @return <code>abs(this</code>
*/
@Override
public Geldbetrag abs() {
if (betrag.compareTo(BigDecimal.ZERO) < 0) {
return negate();
} else {
return this;
}
}
/**
* Returns a {@code MonetaryAmount} whose value is <code>-this</code>, and whose scale is
* {@code this.scale()}.
*
* @return {@code -this}.
*/
@Override
public Geldbetrag negate() {
return valueOf(betrag.negate(), waehrung);
}
/**
* Liefert immer eine positiven Geldbetrag.
*
* @return positiver Geldbetrag
* @see BigDecimal#plus()
*/
@Override
public Geldbetrag plus() {
if (betrag.compareTo(BigDecimal.ZERO) < 0) {
return negate();
} else {
return this;
}
}
/**
* Returns a {@code MonetaryAmount} which is numerically equal to this one but with any trailing
* zeros removed from the representation. For example, stripping the trailing zeros from the
* {@code MonetaryAmount} value {@code CHF 600.0}, which has [{@code BigInteger}, {@code scale}]
* components equals to [6000, 1], yields {@code 6E2} with [ {@code BigInteger}, {@code scale}]
* components equals to [6, -2]
*
* @return a numerically equal {@code MonetaryAmount} with any trailing zeros removed.
*/
@Override
public Geldbetrag stripTrailingZeros() {
throw new UnsupportedOperationException("not yet implemented");
}
/**
* Vergleicht die Zahlenwerter der beiden Geldbetraege. Aber nur, wenn es
* sich um die gleiche Waehrung handelt. Sonst wird eine
* {@link MonetaryException} ausgeloest.
* Compares this object with the specified object for order. Returns a
*
* @param other der andere Geldbetrag
* @return 0 bei Gleicheit; negative Zahl, wenn dieser Geldbetrag kleiner
* als der andere ist; sonst positive Zahl.
*/
@Override
public int compareTo(MonetaryAmount other) {
checkCurrency(other);
return betrag.compareTo(other.getNumber().numberValue(BigDecimal.class));
}
/**
* Vergleicht nur den Zahlenwert und ignoriert die Waehrung. Diese Methode
* ist aus Kompatibiltaetsgruenden zur BigDecimal-Klasse enthalten.
*
* @param other der andere Betrag
* @return 0 bei Gleicheit; negative Zahl, wenn die Zahle kleiner als die
* andere ist, sonst positive Zahl.
*/
public int compareTo(Number other) {
return this.compareTo(Geldbetrag.valueOf(other, waehrung));
}
/**
* Liefert die entsprechende Waehrungseinheit ({@link CurrencyUnit}).
*
* @return die entsprechende {@link CurrencyUnit}, not null.
*/
@Override
public CurrencyUnit getCurrency() {
return Monetary.getCurrency(waehrung.getCurrencyCode());
}
/**
* Liefert den entsprechenden {@link NumberValue}.
*
* @return der entsprechende {@link NumberValue}, not null.
*/
@Override
public NumberValue getNumber() {
return new DefaultNumberValue(betrag);
}
/**
* Liefert nur die Zahl als 'double' zurueck. Sie entspricht der
* gleichnamigen Methode aus {@link BigDecimal}.
*
* @return Zahl als 'double'
* @see BigDecimal#doubleValue()
*/
public double doubleValue() {
return betrag.doubleValue();
}
private static BigDecimal toBigDecimal(Number value) {
BigDecimal n = BigDecimal.valueOf(value.doubleValue());
if (value instanceof BigDecimal) {
n = (BigDecimal) value;
}
return n;
}
private static BigDecimal limitScale(BigDecimal value) {
return value.setScale(4, RoundingMode.HALF_UP);
}
/**
* Hash-Code.
*
* @return a hash code value for this object.
* @see Object#equals(Object)
* @see System#identityHashCode
*/
@Override
public int hashCode() {
return betrag.hashCode();
}
/**
* Zwei Betraege sind gleich, wenn Betrag und Waehrung gleich sind.
*
* @param obj the obj
* @return true, falls gleich
*/
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Geldbetrag)) {
return false;
}
Geldbetrag other = (Geldbetrag) obj;
if (hasNotSameCurrency(other)) return false;
return this.isEqualTo(other);
}
private boolean hasNotSameCurrency(MonetaryAmount other) {
return !this.getCurrency().equals(other.getCurrency());
}
private void checkCurrency(MonetaryAmount other) {
if (hasNotSameCurrency(other)) throw new LocalizedMonetaryException("different currencies", this, other);
}
/**
* Um anzuzeigen, dass es ein Geldbtrag ist, wird zusaetzlich noch das
* Waehrungszeichen (abhaengig von der eingestellten Locale) ausgegeben.
*
* @return z.B. "19.00 $"
* @see java.math.BigDecimal#toString()
*/
@Override
public String toString() {
return this.getNumber() + " " + waehrung.getSymbol();
}
}