-
Notifications
You must be signed in to change notification settings - Fork 5
/
money.clj
2256 lines (2017 loc) · 89.8 KB
/
money.clj
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
(ns io.randomseed.bankster.money
^{:doc "Bankster library, money operations."
:author "Paweł Wilk"
:added "1.0.0"}
(:refer-clojure :exclude [apply format compare cast rem
= == not= not== < > >= <=
+ - * / min max
pos? neg? zero?])
(:require [clojure.string]
[clojure.edn]
[trptr.java-wrapper.locale :as l]
[io.randomseed.bankster :refer :all]
[io.randomseed.bankster.scale :as scale]
[io.randomseed.bankster.currency :as currency]
[io.randomseed.bankster.registry :as registry]
[io.randomseed.bankster.util.map :as map]
[io.randomseed.bankster.util.fs :as fs]
[io.randomseed.bankster.util :refer :all])
(:import [io.randomseed.bankster Currency Registry Money]
[java.math MathContext RoundingMode]
[java.text NumberFormat DecimalFormat DecimalFormatSymbols]
[java.util Locale]))
;;
;; Accountable protocol.
;;
(defprotocol ^{:added "1.0.0"} Accountable
"This protocol is used to express (monetary) values using various numeric types and
currency representations."
(^{:tag io.randomseed.bankster.Money :added "1.0.0"}
value
[num] [currency num] [currency num rounding-mode]
"Creates new Money object for the given value which will become an amount. If the
currency is not given it will try to use the default one, taken from the
`*default-currency*` dynamic variable. Optional rounding-mode should be a rounding
mode used when the conversion to a scaled monetary amount requires rounding.
In its unary form, when the argument is not numeric, it will try to get the
currency object (identified by a string, a symbol or a keyword) from the default,
global registry of currencies.
For simple money creation the following macros may be convenient way to go: of,
of-major, of-minor.
Be careful about using number literals for big-scale amounts (16–17 digits). Use
either big decimal literals, e.g. 1234.45689101112M, or strings.")
(^{:tag io.randomseed.bankster.Money :added "1.1.0"}
cast
[money] [money currency] [money currency rounding-mode]
"Casts an existing Money object to another having a different currency, rescaling
the amount and optionally rounding it. This is useful when operating on multiple
currency registries compatible with different data sources or processing
engines. For simply ensuring that a currency is sourced in the right registry, use
`of-registry`."))
;;
;; Rescaling helper.
;;
(defn monetary-scale
"Rescales the given number n using `io.randomseed.bankster.scale/apply` on the
sc unless the scale is set to auto-scaled."
{:tag BigDecimal :private true :added "1.0.7"}
([n]
(scale/apply n))
([n sc]
(if (currency/val-auto-scaled? sc)
(scale/apply n)
(scale/apply n (int sc))))
([n sc ^RoundingMode rm]
(if (currency/val-auto-scaled? sc)
(scale/apply n)
(scale/apply n (int sc) ^RoundingMode rm))))
(defmacro currency-auto-scaled?
"Returns true if the given Currency object is auto-scaled."
{:private true :added "1.1.2"}
[c] `(clojure.core/== currency/auto-scaled (.scale ~c)))
(defn auto-scaled?
"Returns true if the given Money object is based on a currency which is auto-scaled."
{:tag Boolean :added "1.1.2"}
[^Money m] (currency-auto-scaled? ^Currency (.currency ^Money m)))
;;
;; Money generation macros.
;;
(def ^{:private true :tag clojure.lang.PersistentHashSet :added "1.0.0"}
amount?
#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9 \. \- \+})
(defn mk-bigdec
{:added "1.0.6" :private true}
[n]
(if (number? n) (bigdec n) n))
(defn currency+amount
"Splits the amount and currency."
{:private true :tag clojure.lang.PersistentVector :added "1.0.0"}
[amount]
(let [lseq (remove #{\_\ } (seq (if (keyword? amount)
(if-some [n (namespace amount)]
(str n "/" (name amount))
(name amount))
(if (number? amount)
(.toPlainString (bigdec amount))
(str amount)))))
fdigi? (amount? (first lseq))
[a b] (split-with (if fdigi? amount? (complement amount?)) lseq)
a (when (seq a) (clojure.core/apply str a))
b (when (seq b) (clojure.core/apply str b))]
(if fdigi? [b a] [a b])))
(defmacro currency-unit-strict
"Int."
{:tag Currency :added "1.0.0" :private true}
[a]
`(let [na# ~a] (when-not (number? na#) (currency/unit na#))))
(defn parse-int
"Internal parser with amount modifier."
{:tag Money :private true :added "1.0.0"}
(^Money [mod-fn amount]
(when (some? amount)
(if (instance? Money amount)
(parse-int mod-fn (.currency ^Money amount) (.amount ^Money amount))
(if-some [^Currency cur (currency-unit-strict amount)]
(parse-int mod-fn cur 0M)
(let [[cur am] (currency+amount amount)]
(parse-int mod-fn cur (or am 0M)))))))
(^Money [mod-fn currency amount]
(when (some? amount)
(if-some [^Currency c (currency-unit-strict currency)]
(Money. ^Currency c ^BigDecimal (monetary-scale (mod-fn amount)
(int (scale/of ^Currency c))))
(throw (ex-info
(str "Cannot create money amount without a valid currency and a default currency was not set.")
{:amount amount :currency currency})))))
(^Money [mod-fn ^Currency currency amount ^RoundingMode rounding-mode]
(when (some? amount)
(if-some [^Currency c (currency-unit-strict currency)]
(Money. ^Currency c ^BigDecimal (monetary-scale (mod-fn amount)
(int (scale/of ^Currency c))
^RoundingMode rounding-mode))
(throw (ex-info
(str "Cannot create money amount without a valid currency and a default currency was not set.")
{:amount amount :currency currency}))))))
(defn parse
"Internal parser without amount modifier."
{:tag Money :no-doc :true :added "1.0.0"}
(^Money [amount] (parse-int identity amount))
(^Money [currency amount] (parse-int identity currency amount))
(^Money [currency amount ^RoundingMode rounding-mode] (parse-int identity currency amount rounding-mode)))
(defn parse-major
"Internal parser with scale/integer as an amount modifier."
{:tag Money :no-doc :true :added "1.0.0"}
(^Money [amount] (parse-int scale/integer amount))
(^Money [currency amount] (parse-int scale/integer currency amount))
(^Money [currency amount ^RoundingMode rounding-mode] (parse-int scale/integer currency amount rounding-mode)))
(defn parse-minor
"Internal parser with scale/fractional as an amount modifier."
{:tag Money :no-doc :true :added "1.0.0"}
(^Money [amount] (parse-int scale/fractional amount))
(^Money [currency amount] (parse-int scale/fractional currency amount))
(^Money [currency amount ^RoundingMode rounding-mode] (parse-int scale/fractional currency amount rounding-mode)))
(defn of-gen
"Internal parser for of- macros."
{:no-doc true :added "1.0.0"}
([fun] `(~fun 0M))
([fun a]
(if (or (ident? a) (string? a) (number? a))
(let [[cur# am#] (currency+amount a)]
(if (or (nil? cur#) (nil? am#))
(let [[c# a#] (if (number? a) [nil (mk-bigdec a)] [a 0M])]
(if (nil? c#)
`(~fun ~a#)
(let [curs# (currency/parse-currency-code c#)]
(if (keyword? curs#)
`(~fun ~curs# ~a#)
`(let [cc# ~c#]
(if (instance? Money cc#)
(~fun cc#)
(~fun cc# ~a#)))))))
(if (nil? am#)
`(~fun ~cur#)
`(~fun ~cur# ~(mk-bigdec am#)))))
`(let [a# ~(mk-bigdec a)]
(if (or (number? a#) (instance? Money a#))
(~fun a#)
(~fun a# 0M)))))
([fun a b]
(if (or (ident? a) (string? a) (number? a))
(let [[cur# am#] (currency+amount a)]
(if (or (nil? cur#) (nil? am#))
(let [[c# a#] (if (number? a) [b a] [a b])]
`(~fun ~(currency/parse-currency-code c#) ~(mk-bigdec a#)))
(if (or (ident? b) (string? b) (number? b))
(let [rms# (scale/parse-rounding b)]
`(~fun ~cur# ~(mk-bigdec am#) ~rms#))
`(~fun ~cur# ~(mk-bigdec am#) ~b))))
(let [b (if (number? b) b (currency/parse-currency-code b))]
`(let [a# ~(mk-bigdec a) b# ~(mk-bigdec b)]
(if (number? a#)
(~fun b# a#)
(when (number? b#)
(~fun a# b#)))))))
([fun a b rounding-mode]
(let [rms# (scale/parse-rounding rounding-mode)]
(if (or (ident? a) (string? a) (number? a))
(let [[c# a#] (if (number? a) [b a] [a b])]
`(~parse ~(currency/parse-currency-code c#) ~(mk-bigdec a#) ~rms#))
(let [b (if (number? b) b (currency/parse-currency-code b))]
`(let [a# ~(mk-bigdec a) b# ~(mk-bigdec b)]
(if (number? a#)
(~parse b# a# ~rms#)
(when (number? b#)
(~parse a# b# ~rms#)))))))))
(defmacro of
"Returns the amount of money as a Money object consisting of a currency and a
value. Currency can be a currency object and for registered currencies: a keyword,
a symbol or a string (e.g. EUR, :EUR, \"PLN\" or crypto/ETH), or even a number (for
ISO-compliant currencies).
The given amount can be any numeric value or a string that can be converted to
java.math.BigDecimal.
When a number must be downscaled to fulfill the number of decimal places for a
currency, rounding mode must be given, which may be a symbol, a keyword or a string
of the following:
* CEILING - rounds towards positive infinity.
* DOWN - rounds towards zero.
* FLOOR - rounds towards negative infinity.
* HALF_DOWN - rounds towards nearest neighbor unless both neighbors are equidistant, in which case rounds down.
* HALF_EVEN - rounds towards the nearest neighbor unless both neighbors are equidistant, and if so, rounds towards the even.
* HALF_UP - rounds towards the nearest neighbor unless both neighbors are equidistant, and if so, rounds up.
* UP – rounds away from zero
* UNNECESSARY - asserts that the requested operation has an exact result, hence no rounding is necessary.
To create a monetary object using function, call `io.randomseed.bankster.money/value`.
Be careful about using number literals for big-scale amounts (16–17 digits). Use
either big decimal literals, e.g. 1234.45689101112M, or strings."
([] (of-gen parse))
([a] (of-gen parse a))
([a b] (of-gen parse a b))
([a b rounding-mode] (of-gen parse a b rounding-mode)))
(defmacro of-major
"Like io.randomseed.money/of but sets the amount of major part only."
([] (of-gen parse))
([a] (of-gen parse-major a))
([a b] (of-gen parse-major a b))
([a b rounding-mode] (of-gen parse-major a b rounding-mode)))
(defmacro of-minor
"Like io.randomseed.money/of but sets the amount of minor part only."
([] (of-gen parse))
([a] (of-gen parse-minor a))
([a b] (of-gen parse-minor a b))
([a b rounding-mode] (of-gen parse-minor a b rounding-mode)))
;;
;; Accountable implementation.
;;
(extend-protocol Accountable
Money
(value
(^Money [money]
money)
(^Money [money amount]
(when (some? amount)
(Money. ^Currency (.currency ^Money money)
^BigDecimal (scale/apply amount (int (.scale ^BigDecimal (.amount ^Money money)))))))
(^Money [money amount ^RoundingMode rounding]
(when (some? amount)
(Money. ^Currency (.currency ^Money money)
^BigDecimal (scale/apply amount (int (.scale ^BigDecimal (.amount ^Money money))) rounding)))))
(cast
(^Money [money]
money)
(^Money [money currency]
(let [^Currency cur (currency/unit currency)]
(Money. ^Currency cur
^BigDecimal (monetary-scale (.amount ^Money money) (int (.scale ^Currency cur))))))
(^Money [money currency ^RoundingMode rounding]
(let [^Currency cur (currency/unit currency)]
(Money. ^Currency cur
^BigDecimal (monetary-scale (.amount ^Money money) (int (.scale ^Currency cur)) rounding)))))
Currency
(value
(^Money [currency]
(Money. ^Currency currency
^BigDecimal (monetary-scale 0M (int (.scale ^Currency currency)))))
(^Money [currency amount]
(when (some? amount)
(Money. ^Currency currency
^BigDecimal (monetary-scale amount (int (.scale ^Currency currency))))))
(^Money [currency amount ^RoundingMode rounding]
(when (some? amount)
(Money. ^Currency currency
^BigDecimal (monetary-scale amount (int (.scale ^Currency currency)) rounding)))))
(cast
(^Money [currency]
(value currency))
(^Money [currency ^Money money]
(let [^Currency cur (currency/unit currency)]
(Money. ^Currency cur
^BigDecimal (monetary-scale (.amount ^Money money) (int (.scale ^Currency cur))))))
(^Money [currency ^Money money ^RoundingMode rounding]
(let [^Currency cur (currency/unit currency)]
(Money. ^Currency cur
^BigDecimal (monetary-scale (.amount ^Money money) (int (.scale ^Currency cur)) rounding)))))
clojure.lang.Symbol
(value
(^Money [currency-id] (value (currency/unit currency-id)))
(^Money [currency-id amount] (value (currency/unit currency-id) amount))
(^Money [currency-id amount rounding] (value (currency/unit currency-id) amount rounding)))
(cast
(^Money [currency-id] (value (currency/unit currency-id)))
(^Money [currency-id money] (cast (currency/unit currency-id) money))
(^Money [currency-id money rounding] (cast (currency/unit currency-id) money rounding)))
clojure.lang.Keyword
(value
(^Money [currency-id] (value (currency/unit currency-id)))
(^Money [currency-id amount] (value (currency/unit currency-id) amount))
(^Money [currency-id amount rounding] (value (currency/unit currency-id) amount rounding)))
(cast
(^Money [currency-id] (value (currency/unit currency-id)))
(^Money [currency-id money] (cast (currency/unit currency-id) money))
(^Money [currency-id money rounding] (cast (currency/unit currency-id) money rounding)))
String
(value
(^Money [currency-id] (value (currency/unit currency-id)))
(^Money [currency-id amount] (value (currency/unit currency-id) amount))
(^Money [currency-id amount rounding] (value (currency/unit currency-id) amount rounding)))
(cast
(^Money [currency-id] (value (currency/unit currency-id)))
(^Money [currency-id money] (cast (currency/unit currency-id) money))
(^Money [currency-id money rounding] (cast (currency/unit currency-id) money rounding)))
Number
(value
(^Money [amount] (value (currency/unit currency/*default*) amount))
(^Money [amount currency-id] (value (currency/unit currency-id) amount))
(^Money [amount currency-id rounding] (value (currency/unit currency-id) amount rounding)))
(cast
(^Money [amount] (value (currency/unit currency/*default*) amount))
(^Money [currency-id money] (cast (currency/unit currency-id) money))
(^Money [currency-id money rounding] (cast (currency/unit currency-id) money rounding)))
nil
(value
([c] nil)
([a b] (when (some? b) (parse b)))
([a c r] (when (some? c) (parse c nil r))))
(cast
([c] nil)
([a b] nil)
([a c r] nil))
clojure.lang.Sequential
(value
(^Money [v] (clojure.core/apply parse (first v) (rest v)))
(^Money [v r] (value (first v) (second v) r))
(^Money [v b r] (value (first v) b r)))
(cast
(^Money [v] (clojure.core/apply cast (first v) (rest v)))
(^Money [v r] (cast (first v) (second v) r))
(^Money [v b r] (cast (first v) b r)))
Object
(value
(^Money [a] (parse a))
(^Money [a c] (when (some? c) (parse a c)))
(^Money [a c r] (when (some? c) (parse a c r))))
(cast
(^Money [a] (cast (parse a)))
(^Money [a c] (cast (parse a) c))
(^Money [a c r] (cast (parse a) c r))))
(defn major-value
"Creates new Money object for the given value which will become a major part of the
amount. If the given number has fractional part it will be truncated. If the
currency is not given it should try to use the default one, taken from the
`io.randomseed.bankster.currency/*default*` dynamic variable. Optional
rounding-mode should be a rounding mode used when the conversion to a scaled
monetary amount requires rounding.
For simple money creation the following macros may be convenient way to go: of,
of-major, of-minor."
(^Money [a] (when-some [v (value a)] (major-value (.currency ^Money v) (.amount ^Money v))))
(^Money [currency amount] (value currency (scale/integer amount)))
(^Money [currency amount rounding-mode] (value currency (scale/integer amount) rounding-mode)))
(defn minor-value
"Creates new Money object for the given value which will become a minor part of the
amount. If the given number has fractional part it will be truncated. If the
currency is not given it should try to use the default one, taken from the
`io.randomseed.bankster.currency/*default*` dynamic variable. Optional
rounding-mode should be a rounding mode used when the conversion to a scaled
monetary amount requires rounding.
For simple money creation the following macros may be convenient way to go: of,
of-major, of-minor."
(^Money [a] (when-some [v (value a)] (minor-value (.currency ^Money v) (.amount ^Money v))))
(^Money [currency amount] (value currency (scale/fractional amount)))
(^Money [currency amount rounding-mode] (value currency (scale/fractional amount) rounding-mode)))
;;
;; Ensuring the right registry is used.
;;
(defn of-registry
"Ensures that a currency of the given money originates from the given registry. If
the registry is not given and a dynamic registry is not set, the default one is
used. Rescales the amount if needed to match the nominal scale. Optional
rounding-mode can be supplied to be used when downscaling is needed (nominal
currency from a registry has lower number of decimal places than the amount of
money).
Money can be expressed as a Money object or any other object that will create Money
when passed to the `value` function. Returns money."
{:tag Money :added "1.1.2"}
(^Money [money]
(of-registry (registry/get) money))
(^Money [^Registry registry money]
(let [^Money money (value money)
^Currency cur (currency/unit ^Currency (.id ^Currency (.currency ^Money money))
^Registry registry)]
(Money. ^Currency cur
^BigDecimal (monetary-scale (.amount ^Money money)
(int (.scale ^Currency cur))))))
(^Money [^Registry registry money ^RoundingMode rounding-mode]
(let [^Money money (value money)
^Currency cur (currency/unit ^Currency (.id ^Currency (.currency ^Money money))
^Registry registry)]
(Money. ^Currency cur
^BigDecimal (monetary-scale (.amount ^Money money)
(int (.scale ^Currency cur))
rounding-mode)))))
;;
;; Operating on an amount.
;;
(defn on-amount
"Performs an operation expressed with a function f on an amount of the given
money. Additional arguments will be passed to the f. Returns the money with the
amount updated. The function f must return a number. Short-circuits on nil as an
argument. Rescales the result to the existing scale of an amount, not the nominal
scale of a currency."
{:tag Money :added "1.1.2"}
(^Money [money f]
(when-some [money (value money)]
(let [^BigDecimal am (.amount ^Money money)]
(Money. ^Currency (.currency ^Money money)
^BigDecimal (monetary-scale (f am)
(int (.scale ^BigDecimal am)))))))
(^Money [money f a]
(when-some [money (value money)]
(let [^BigDecimal am (.amount ^Money money)]
(Money. ^Currency (.currency ^Money money)
^BigDecimal (monetary-scale (f am a)
(int (.scale ^BigDecimal am)))))))
(^Money [money f a b]
(when-some [money (value money)]
(let [^BigDecimal am (.amount ^Money money)]
(Money. ^Currency (.currency ^Money money)
^BigDecimal (monetary-scale (f am a b)
(int (.scale ^BigDecimal am)))))))
(^Money [money f a b & more]
(when-some [money (value money)]
(let [^BigDecimal am (.amount ^Money money)]
(Money. ^Currency (.currency ^Money money)
^BigDecimal (monetary-scale (clojure.core/apply f am a b more)
(int (.scale ^BigDecimal am))))))))
(def ^{:tag Money :added "1.2.7"
:arglists '([money f]
[money f a]
[money f a b]
[money f a b & more])}
apply
"Alias for on-amount."
on-amount)
(defn set-amount
"Sets the amount of the given monetary object. Rescales value to a scale of the given
monetary object, not the nominal scale of its currency."
{:tag Money :added "1.2.7"}
(^Money [money v]
(when-some [money (value money)]
(let [^BigDecimal v (scale/apply v)]
(Money. ^Currency (.currency ^Money money)
^BigDecimal (monetary-scale ^BigDecimal v
(int (.scale ^BigDecimal (.amount ^Money money))))))))
(^Money [money v ^RoundingMode rounding-mode]
(when-some [money (value money)]
(let [^BigDecimal v (scale/apply v)]
(Money. ^Currency (.currency ^Money money)
^BigDecimal (monetary-scale ^BigDecimal v
(int (.scale ^BigDecimal (.amount ^Money money)))
^RoundingMode rounding-mode))))))
;;
;; Scaling and rounding.
;;
(def ^{:private true :tag clojure.lang.PersistentArrayMap :added "1.0.0"}
rounding->context
"Mapping of rounding modes to MathContext objects."
{RoundingMode/UP (MathContext. (int 0) RoundingMode/UP)
RoundingMode/DOWN (MathContext. (int 0) RoundingMode/DOWN)
RoundingMode/CEILING (MathContext. (int 0) RoundingMode/CEILING)
RoundingMode/FLOOR (MathContext. (int 0) RoundingMode/FLOOR)
RoundingMode/HALF_DOWN (MathContext. (int 0) RoundingMode/HALF_DOWN)
RoundingMode/HALF_EVEN (MathContext. (int 0) RoundingMode/HALF_EVEN)
RoundingMode/HALF_UP (MathContext. (int 0) RoundingMode/HALF_UP)
RoundingMode/UNNECESSARY (MathContext. (int 0) RoundingMode/UNNECESSARY)})
(defn strip
"Strips trailing zeros from the amount of money. The internal scale for a currency
object is NOT updated.
Use with caution since it can make money object no longer compliant with a scale of
the registered currency."
{:tag Money :added "1.0.0"}
[^Money a]
(Money. ^Currency (.currency ^Money a)
^BigDecimal (.stripTrailingZeros ^BigDecimal (.amount ^Money a))))
;;
;; Properties.
;;
(defn amount
"Returns the amount of the given money. For more than one argument the money is
created ad-hoc using a and b objects passed to the function named value."
{:tag BigDecimal :added "1.0.0"}
(^BigDecimal [money] (when-some [m (value money)] (.amount ^Money m)))
(^BigDecimal [a b] (when-some [m (value a b)] (.amount ^Money m)))
(^BigDecimal [a b r] (when-some [m (value a b r)] (.amount ^Money m))))
(defn currency
"Returns the currency of the given money. For more than one argument the money is
created ad-hoc using a and b objects passed to the function named value."
{:tag Currency :added "1.0.0"}
(^Currency [money] (when-some [m (value money)] (.currency ^Money m)))
(^Currency [a b] (when-some [m (value a b)] (.currency ^Money m)))
(^Currency [a b r] (when-some [m (value a b r)] (.currency ^Money m))))
(defn stripped-amount
"Returns the amount of the given money with trailing zeros removed. For more than one
argument the money is created ad-hoc using a and b objects passed to the function
named value."
{:tag BigDecimal :added "1.0.0"}
(^BigDecimal [money] (when-some [m (value money)] (.stripTrailingZeros ^BigDecimal (.amount ^Money m))))
(^BigDecimal [a b] (when-some [m (value a b)] (.stripTrailingZeros ^BigDecimal (.amount ^Money m))))
(^BigDecimal [a b r] (when-some [m (value a b r)] (.stripTrailingZeros ^BigDecimal (.amount ^Money m)))))
(defn unparse
"Returns a vector with symbolic representations of amount and currency. Useful for
printing to EDN or displaying on a console. The letter M will be added to the
amount if its precision exceeds 15. For more than one argument the money is created
ad-hoc using a and b objects passed to the function named value."
{:tag clojure.lang.IPersistentVector :added "1.0.7"}
([money]
(when-some [m (value money)]
[(scale/to-clojure-symbol ^BigDecimal (.amount ^Money m))
(symbol (currency/id ^Currency (.currency ^Money m)))]))
([a b]
(when-some [m (value a b)]
[(scale/to-clojure-symbol ^BigDecimal (.amount ^Money m))
(symbol (currency/id ^Currency (.currency ^Money m)))]))
([a b r]
(when-some [m (value a b r)]
[(scale/to-clojure-symbol ^BigDecimal (.amount ^Money m))
(symbol (currency/id ^Currency (.currency ^Money m)))])))
;;
;; Monetary implementation.
;;
(extend-protocol currency/Monetary
Money
(of-id
(^Currency [money]
(currency/of-id ^Currency (.currency ^Money money)))
(^Currency [money ^Registry registry]
(currency/of-id ^Currency (.currency ^Money money) ^Registry registry)))
(unit
(^Currency [money]
(currency/unit ^Currency (.currency ^Money money)))
(^Currency [money ^Registry registry]
(currency/unit ^Currency (.currency ^Money money) ^Registry registry)))
(id
(^clojure.lang.Keyword [money]
(.id ^Currency (.currency ^Money money)))
(^clojure.lang.Keyword [money ^Registry registry]
(.id ^Currency (.currency ^Money money))))
(defined?
(^Boolean [money]
(contains? (registry/currency-id->currency)
(.id ^Currency (.currency ^Money money))))
(^Boolean [money ^Registry registry]
(contains? (registry/currency-id->currency registry)
(.id ^Currency (.currency ^Money money)))))
(present?
(^Boolean [money]
(let [id (.id ^Currency (.currency ^Money money))]
(if (namespace id)
(contains? (registry/currency-id->currency) id)
(contains? (registry/currency-code->currencies) id))))
(^Boolean [money ^Registry registry]
(let [id (.id ^Currency (.currency ^Money money))]
(if (namespace id)
(contains? (registry/currency-id->currency registry) id)
(contains? (registry/currency-code->currencies registry) id)))))
(same-ids?
(^Boolean [a b]
(clojure.core/= (.id ^Currency (.currency ^Money a)) (currency/id b)))
(^Boolean [a b ^Registry registry]
(clojure.core/= (.id ^Currency (.currency ^Money a)) (currency/id b registry)))))
;;
;; Scalable implementation.
;;
(extend-protocol scale/Scalable
Money
(^Boolean scalable? [m] true)
(^Boolean applied? [m] true)
(of [m] (.scale ^BigDecimal (.amount ^Money m)))
(^Money apply
(^Money [m]
(let [^Currency cur (.currency ^Money m)
sc (int (.scale ^Currency cur))]
(if (currency/val-auto-scaled? sc) m
(Money. ^Currency cur
(if-some [rm scale/*rounding-mode*]
^BigDecimal (.setScale ^BigDecimal (.amount ^Money m)
(int sc)
^RoundingMode rm)
^BigDecimal (.setScale ^BigDecimal (.amount ^Money m)
(int sc)))))))
(^Money [m scale]
(if-some [rm scale/*rounding-mode*]
(Money. ^Currency (.currency ^Money m)
^BigDecimal (.setScale ^BigDecimal (.amount ^Money m)
(int scale)
^RoundingMode rm))
(Money. ^Currency (.currency ^Money m)
^BigDecimal (.setScale ^BigDecimal (.amount ^Money m) (int scale)))))
(^Money [m scale ^RoundingMode rounding-mode]
(Money. ^Currency (.currency ^Money m)
^BigDecimal (.setScale ^BigDecimal (.amount^Money m)
(int scale)
^RoundingMode rounding-mode))))
(^BigDecimal amount
(^BigDecimal [money]
(.amount ^Money money))
(^BigDecimal [money scale]
(scale/apply ^BigDecimal (.amount ^Money money) (int scale)))
(^BigDecimal [money scale ^RoundingMode r]
(scale/apply ^BigDecimal (.amount ^Money money) (int scale) ^RoundingMode r))))
;;
;; Comparators.
;;
(declare same-currencies?)
(defn compare-amounts
"Compares two monetary amounts of the same currency, regardless of their
scales. Returns -1 if the second one is less than, 0 if equal to, and 1 if it is
greater than the first."
{:added "1.0.0" :tag 'int}
(^Boolean [^Money a] (int 0))
(^Boolean [^Money a ^Money b]
(when-not (same-currencies? a b)
(throw (ex-info "Cannot compare amounts of different currencies."
{:money-1 a :money-2 b})))
(int (.compareTo ^BigDecimal (.amount ^Money a) ^BigDecimal (.amount ^Money b)))))
(defn compare
"Compares two monetary amounts of the same currency and scale. Returns -1 if the
second one is less than, 0 if equal to, and 1 if it is greater than the first."
{:added "1.0.0" :tag 'int}
(^Boolean [^Money a] (int 0))
(^Boolean [^Money a ^Money b]
(let [^BigDecimal am-a (.amount ^Money a)
^BigDecimal am-b (.amount ^Money b)]
(when-not (same-currencies? a b)
(throw (ex-info "Cannot compare amounts of different currencies."
{:money-1 a :money-2 b})))
(when-not (clojure.core/= (.scale ^BigDecimal am-a) (.scale ^BigDecimal am-b))
(throw (ex-info "Cannot compare amounts having different decimal scales."
{:money-1 a :money-2 b})))
(int (.compareTo ^BigDecimal am-a ^BigDecimal am-b)))))
;;
;; Predicates.
;;
(defn money?
"Returns true if the given value is a kind of Money."
{:tag Boolean :added "1.0.0"}
[a]
(instance? Money a))
(defn rescaled?
"Returns true if the given monetary value has different scale than its currency (so
it was rescaled). Returns false if the scale is no different. Returns nil if the
currency does not have a fixed scale."
{:added "1.0.0"}
[a]
(let [csc (int (.scale ^Currency (.currency ^Money a)))]
(when-not (currency/val-auto-scaled? csc)
(clojure.core/not= csc (.scale ^BigDecimal (.amount ^Money a))))))
(defn same-currencies?
"Returns true if both currencies are the same for the given money objects."
{:tag Boolean :added "1.0.0"}
[^Money a ^Money b]
(clojure.core/= ^Currency (.currency ^Money a)
^Currency (.currency ^Money b)))
(defn same-currency-ids?
"Returns true if both currencies have the same IDs for the given money objects."
{:tag Boolean :added "1.0.0"}
[^Money a ^Money b]
(clojure.core/= (.id ^Currency (.currency ^Money a))
(.id ^Currency (.currency ^Money b))))
(defn eq?
"Return true if the money amounts and their currencies are equal. Note that
currencies with different scales or other properties are considered different. Use
eq-am? (aliased as ==) to compare amounts regardless of their scales."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] true)
(^Boolean [^Money a ^Money b]
(and (.equals (.amount ^Money a) (.amount ^Money b))
(same-currencies? a b)))
(^Boolean [^Money a ^Money b & more]
(if (eq? a b)
(if (next more)
(recur b (first more) (next more))
(eq? b (first more)))
false)))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
=
"Alias for eq?."
eq?)
(defn eq-am?
"Return true if the monetary amounts and their currencies are equal, regardless of
their scales."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] true)
(^Boolean [^Money a ^Money b]
(if-not (same-currencies? a b)
false
(let [^BigDecimal am-a (.amount ^Money a)
^BigDecimal am-b (.amount ^Money b)
sa (int (.scale am-a))
sb (int (.scale am-b))]
(if (clojure.core/= sa sb)
(.equals ^BigDecimal am-a ^BigDecimal am-b)
(if (clojure.core/< sa sb)
(.equals ^BigDecimal (scale/apply am-a sb) ^BigDecimal am-b)
(.equals ^BigDecimal am-a ^BigDecimal (scale/apply am-b sa)))))))
(^Boolean [^Money a ^Money b & more]
(if (eq-am? a b)
(if (next more)
(recur b (first more) (next more))
(eq-am? b (first more)))
false)))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
==
"Alias for eq-am?."
eq-am?)
(defn ne?
"Returns true if the money amounts or their currencies are different, regardless of
their scales. Note that currencies with different scales are considered different."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] false)
(^Boolean [^Money a ^Money b]
(not (eq? ^Money a ^Money b)))
(^Boolean [^Money a ^Money b & more]
(not (clojure.core/apply eq? ^Money a ^Money b more))))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
not=
"Alias for ne?."
ne?)
(defn ne-am?
"Returns true if the money amounts or their currencies are different, regardless of
their scales."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] false)
(^Boolean [^Money a ^Money b]
(not (eq-am? ^Money a ^Money b)))
(^Boolean [^Money a ^Money b & more]
(not (clojure.core/apply eq-am? ^Money a ^Money b more))))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
not==
"Alias for ne-am?."
ne-am?)
(defn gt?
"Returns non-nil if monetary amounts are in monotonically decreasing order,
otherwise false."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] true)
(^Boolean [^Money a ^Money b]
(pos-int? (compare-amounts a b)))
(^Boolean [^Money a ^Money b & more]
(if (gt? a b)
(if (next more)
(recur b (first more) (next more))
(gt? b (first more)))
false)))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
>
"Alias for gt?."
gt?)
(defn ge?
"Returns non-nil if monetary amounts are in monotonically non-increasing order,
otherwise false."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] true)
(^Boolean [^Money a ^Money b]
(clojure.core/>= (compare-amounts a b) 0))
(^Boolean [^Money a ^Money b & more]
(if (ge? a b)
(if (next more)
(recur b (first more) (next more))
(ge? b (first more)))
false)))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
>=
"Alias for ge?."
ge?)
(defn lt?
"Returns non-nil if monetary amounts are in monotonically increasing order,
otherwise false."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] true)
(^Boolean [^Money a ^Money b]
(neg-int? (compare-amounts a b)))
(^Boolean [^Money a ^Money b & more]
(if (lt? a b)
(if (next more)
(recur b (first more) (next more))
(lt? b (first more)))
false)))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
<
"Alias for lt?."
lt?)
(defn le?
"Returns non-nil if monetary amounts are in monotonically non-decreasing order,
otherwise false."
{:tag Boolean :added "1.0.0"}
(^Boolean [^Money a] true)
(^Boolean [^Money a ^Money b]
(clojure.core/<= (compare-amounts a b) 0))
(^Boolean [^Money a ^Money b & more]
(if (le? a b)
(if (next more)
(recur b (first more) (next more))
(le? b (first more)))
false)))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a]
^Boolean [^Money a ^Money b]
^Boolean [^Money a ^Money b & more])}
<=
"Alias for le?."
le?)
(defn is-zero?
"Returns true if the given monetary amount is a positive number."
{:tag Boolean :added "1.0.0"}
[^Money a]
(clojure.core/zero? (.compareTo 0M ^BigDecimal (.amount ^Money a))))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a])}
zero?
"Alias for is-zero?."
is-zero?)
(defn is-neg?
"Returns true if the given monetary amount is a negative number."
{:tag Boolean :added "1.0.0"}
[^Money a]
(pos-int? (.compareTo 0M ^BigDecimal (.amount ^Money a))))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a])}
neg?
"Alias for is-neg?."
is-neg?)
(defn is-pos?
"Returns true if the given monetary amount is a positive number."
{:tag Boolean :added "1.0.0"}
[^Money a]
(neg-int? (.compareTo 0M ^BigDecimal (.amount ^Money a))))
(def ^{:tag Money :added "1.2.0"
:arglists '(^Boolean [^Money a])}
pos?
"Alias for is-pos?."
is-pos?)