-
Notifications
You must be signed in to change notification settings - Fork 5
/
registry.clj
310 lines (276 loc) · 11.8 KB
/
registry.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
(ns io.randomseed.bankster.registry
^{:doc "Bankster, registry management."
:author "Paweł Wilk"
:added "1.0.0"}
(:refer-clojure :exclude [new get set! update])
(:require [clojure.string :as str]
[io.randomseed.bankster :as bankster]
[io.randomseed.bankster.util.map :as map]
[io.randomseed.bankster.util.fs :as fs]
[io.randomseed.bankster.util :refer :all])
(:import [io.randomseed.bankster Registry]
[io.randomseed.bankster Currency Registry]
[java.time LocalDateTime format.DateTimeFormatter]))
;;
;; Registry version generator.
;;
(defn default-version
{:tag String :added "1.0.0"}
[]
(. (LocalDateTime/now) format (DateTimeFormatter/ofPattern "YYYYMMddHHmmssSS")))
;;
;; Global, shared registry.
;;
(def ^{:tag clojure.lang.Atom :added "1.0.0"}
R
"Global registry object based on an Atom."
(atom (Registry. {} {} {} {} {} {} (default-version))))
(defn global
"Returns global registry object."
{:tag clojure.lang.Atom :added "1.0.0"}
[]
R)
(defn state
"Returns current state of a global registry."
{:tag Registry :added "1.0.0"}
[]
(deref R))
;;
;; Dynamic registry.
;;
(def ^{:dynamic true :tag Registry :added "1.0.0"}
*default*
"Registry, that if set to a truthy value (not nil and not false), will be used
instead of a global, shared registry."
nil)
;;
;; Registry state getter.
;;
(defmacro get
"Gets the current state of a global registry. If the dynamic variable *default* is
set to a truthy value, it will be used instead."
{:added "1.0.0"}
[]
`(or ^Registry *default* ^Registry (deref R)))
;;
;; Registry constructor.
;;
(defn new-registry
"Creates a new registry."
{:tag Registry :added "1.0.0"}
(^Registry []
(bankster/->Registry {} {} {} {} {} {} (default-version)))
(^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs
^String version]
(bankster/->Registry cur-id->cur
cur-nr->cur
ctr-id->cur
cur-id->ctr-ids
cur-id->localized
cur-code->curs
version))
(^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs]
(new-registry cur-id->cur
cur-nr->cur
ctr-id->cur
cur-id->ctr-ids
cur-code->curs
(default-version)))
(^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs
^String version]
(bankster/->Registry cur-id->cur
(map/map-keys-by-v :nr cur-id->cur)
ctr-id->cur
(map/invert-in-sets ctr-id->cur)
cur-id->localized
cur-code->curs
version))
(^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs]
(new-registry cur-id->cur
ctr-id->cur
cur-id->localized
cur-code->curs
(default-version)))
(^Registry [^clojure.lang.PersistentHashMap m]
(bankster/map->Registry m)))
(def ^{:tag Registry
:added "1.0.0"
:arglists '(^Registry []
^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs
^String version]
^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs]
^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs
^String version]
^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs]
^Registry [^clojure.lang.PersistentHashMap m])}
new
"Alias for new-registry."
new-registry)
;;
;; Registry operations.
;;
(defn registry?
"Returns true if the given object is a registry."
{:tag Boolean :added "1.0.0"}
[obj]
(instance? Registry obj))
(defn update
"Updates a registry with a function that should take a registry as its first argument
and return the updated one. It is a simple apply-based implementation provided for
the sake of symmetry with update! which operates on a global registry object."
{:tag Registry :added "1.0.0"}
[^Registry r ^clojure.lang.IFn fun & more]
(apply fun r more))
(defn set-state
"Sets current state of a global registry."
{:added "1.0.0" :tag Registry :private true}
(^Registry [^Registry registry]
(if (registry? registry)
(reset! R ^Registry registry)
(reset! R (new-registry ^clojure.lang.PersistentHashMap registry))))
(^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized]
(set-state (new-registry cur-id->cur
cur-nr->cur
ctr-id->cur
cur-id->ctr-ids
cur-id->localized
(default-version))))
(^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized
^String version]
(set-state (new-registry cur-id->cur
cur-nr->cur
ctr-id->cur
cur-id->ctr-ids
cur-id->localized
version))))
(def ^{:tag Registry :added "1.0.0"
:arglists '(^Registry [^Registry registry]
^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs]
^Registry [^clojure.lang.PersistentHashMap cur-id->cur
^clojure.lang.PersistentHashMap cur-nr->cur
^clojure.lang.PersistentHashMap ctr-id->cur
^clojure.lang.PersistentHashMap cur-id->ctr-ids
^clojure.lang.PersistentHashMap cur-id->localized
^clojure.lang.PersistentHashMap cur-code->curs
^String version])}
set!
"Sets current state of a global registry."
set-state)
(defn update!
"Updates a global registry using a function that should take a registry and return
the updated version of it."
{:tag Registry :added "1.0.0"}
[^clojure.lang.IFn fun & more]
(apply swap! R fun more))
;;
;; Contextual macro.
;;
(defmacro with
"Sets a registry in a lexical context of the body to be used instead of a global one
in functions which require the registry and it was not passed as an argument."
{:added "1.0.0"}
[^Registry registry & body]
`(binding [*default* ^Registry registry]
~@body))
;;
;; Getters and helpers.
;;
(defmacro currency-id->currency
"Returns the currency ID to currency map from a registry. If the registry is not
given the dynamic variable *default* is tried. If it is not set, current state of a
global registry is used instead."
{:added "1.0.0"}
([] `(.cur-id->cur (get)))
([registry] `(.cur-id->cur ^Registry ~registry)))
(defmacro currency-nr->currency
"Returns the currency number to currency map from a registry. If the registry is not
given the dynamic variable *default* is tried. If it is not set, current state of a
global registry is used instead."
{:added "1.0.0"}
([] `(.cur-nr->cur (get)))
([registry] `(.cur-nr->cur ^Registry ~registry)))
(defmacro currency-code->currencies
"Returns the currency short-code to currencies map from a registry. If the registry
is not given the dynamic variable *default* is tried. If it is not set, current
state of a global registry is used instead."
{:added "1.0.0"}
([] `(.cur-code->curs (get)))
([registry] `(.cur-code->curs ^Registry ~registry)))
(defmacro country-id->currency
"Returns the country ID to currency map from a registry. If the registry is not given
the dynamic variable *default* is tried. If it is not set, current state of a
global registry is used instead."
{:added "1.0.0"}
([] `(.ctr-id->cur (get)))
([registry] `(.ctr-id->cur ^Registry ~registry)))
(defmacro currency-id->country-ids
"Returns the currency ID to country IDs map from a registry. If the registry is not
given the dynamic variable *default* is tried. If it is not set, current state of a
global registry is used instead."
{:added "1.0.0"}
([] `(.cur-id->ctr-ids (get)))
([registry] `(.cur-id->ctr-ids ^Registry ~registry)))
(defmacro currency-id->localized
"Returns the currency ID to localized propertied map from a registry. If the registry
is not given the dynamic variable *default* is tried. If it is not set, current
state of a global registry is used instead."
{:added "1.0.0"}
([] `(.cur-id->localized (get)))
([registry] `(.cur-id->localized ^Registry ~registry)))
;;
;; Printing.
;;
(defmethod print-method Registry
[r w]
(let [sid (Integer/toHexString (System/identityHashCode r))]
(print-simple
(str "#Registry[{"
":currencies " (count (.cur-id->cur ^Registry r)) ", "
":countries " (count (.ctr-id->cur ^Registry r)) ", "
":version \"" (.version ^Registry r) "\"} 0x" sid "]")
w)))