-
Notifications
You must be signed in to change notification settings - Fork 13
/
types.clj
216 lines (175 loc) · 7.68 KB
/
types.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
; Copyright (c) Alan Thompson. All rights reserved.
; The use and distribution terms for this software are covered by the Eclipse Public
; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can be found in the
; file epl-v10.html at the root of this distribution. By using this software in any
; fashion, you are agreeing to be bound by the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns tupelo.types
"Type conversion and detection."
(:refer-clojure :exclude [integer? float? double?])
(:require
[clojure.string :as str]
[schema.core :as s]))
(def ^:const UTF-8-Charset-Name "UTF-8")
; An instance of the java.lang.Class<XXXX[]> (e.g. java.lang.Class<Byte[]>).
(def ^:private class-boolean-array (.getClass (boolean-array 0)))
(def ^:private class-byte-array (.getClass (byte-array 0)))
(def ^:private class-char-array (.getClass (char-array 0)))
(def ^:private class-double-array (.getClass (double-array 0)))
(def ^:private class-float-array (.getClass (float-array 0)))
(def ^:private class-int-array (.getClass (int-array 0)))
(def ^:private class-long-array (.getClass (long-array 0)))
(def ^:private class-object-array (.getClass (object-array 0)))
(def ^:private class-short-array (.getClass (short-array 0)))
; As of Clojure 1.9.0-alpha5, boolean? is native to clojure
#_(defn ^{:deprecated "1.9.0-alpha5" } boolean?
"Returns true is the arg is a Boolean (true or false). Else returns false."
[arg]
(or (= true arg) (= false arg)))
; #todo change to using `instance?`
(defn boolean-array?
"Returns true is the arg is a boolean array, else false."
[arg]
(= class-boolean-array (.getClass arg)))
(defn byte-array?
"Returns true is the arg is a byte array, else false."
[arg]
(= class-byte-array (.getClass arg)))
(defn char-array?
"Returns true is the arg is a char array, else false."
[arg]
(= class-char-array (.getClass arg)))
(defn double-array?
"Returns true is the arg is a double array, else false."
[arg]
(= class-double-array (.getClass arg)))
(defn float-array?
"Returns true is the arg is a float array, else false."
[arg]
(= class-float-array (.getClass arg)))
(defn int-array?
"Returns true is the arg is a int array, else false."
[arg]
(= class-int-array (.getClass arg)))
(defn long-array?
"Returns true is the arg is a long array, else false."
[arg]
(= class-long-array (.getClass arg)))
(defn object-array?
"Returns true is the arg is a object array, else false."
[arg]
(= class-object-array (.getClass arg)))
(defn short-array?
"Returns true is the arg is a short array, else false."
[arg]
(= class-short-array (.getClass arg)))
;-----------------------------------------------------------------------------
(defn byte?
"Returns true is the arg is a Byte, else false."
[arg]
(instance? java.lang.Byte arg))
(defn short?
"Returns true is the arg is a Short, else false."
[arg]
(instance? java.lang.Short arg))
(defn integer?
"Returns true is the arg is a Integer, else false."
[arg]
(instance? java.lang.Integer arg))
(defn long?
"Returns true is the arg is a Long, else false."
[arg]
(instance? java.lang.Long arg))
(defn float?
"Returns true is the arg is a Float, else false."
[arg]
(instance? java.lang.Float arg))
(defn double?
"Returns true is the arg is a Double, else false."
[arg]
(instance? java.lang.Double arg))
(defn character?
"Returns true is the arg is a Character, else false."
[arg]
(instance? java.lang.Character arg))
;-----------------------------------------------------------------------------
(defn str->byte-array ; #todo move to tupelo.misc
"Converts a String to a byte array using the UTF-8 Charset"
[^String arg]
{:pre [ (string? arg) ]
:post [ (byte-array? %) ] }
[arg]
(.getBytes arg UTF-8-Charset-Name))
(defn byte-array->str ; #todo move to tupelo.misc
"Converts a byte array to a String using the UTF-8 Charset"
[arg]
{:pre [ (byte-array? arg) ]
:post [ (string? %) ] }
(String. arg UTF-8-Charset-Name))
;---------------------------------------------------------------------------------------------------
(def BYTE_UNSIGNED_MIN_VALUE 0)
(def BYTE_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 8)
(long)
(dec)))
(def SHORT_UNSIGNED_MIN_VALUE 0)
(def SHORT_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 16)
(long)
(dec)))
(def INTEGER_UNSIGNED_MIN_VALUE 0)
(def INTEGER_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 32)
(long)
(dec)))
(def LONG_UNSIGNED_MIN_VALUE 0)
(def LONG_UNSIGNED_MAX_VALUE (-> (biginteger 2)
(.pow 64)
(.subtract (biginteger 1))))
(defrecord ^:no-doc IntervalClosed ; #todo report defrecord "resolve" to Cursive
[lower-bound upper-bound]) ; #todo report to Cursive
(s/defn ^:no-doc within-interval-closed? :- s/Bool
"Returns true if val fits within an IntervalClosed."
[ic :- IntervalClosed
val :- s/Num]
(<= (:lower-bound ic) val (:upper-bound ic)))
(def ^:no-doc interval-closed-byte (->IntervalClosed Byte/MIN_VALUE Byte/MAX_VALUE)) ; #todo "resolve" report to Cursive
(def ^:no-doc interval-closed-byte-unsigned (->IntervalClosed BYTE_UNSIGNED_MIN_VALUE BYTE_UNSIGNED_MAX_VALUE))
(def ^:no-doc interval-closed-short (->IntervalClosed Short/MIN_VALUE Short/MAX_VALUE))
(def ^:no-doc interval-closed-short-unsigned (->IntervalClosed SHORT_UNSIGNED_MIN_VALUE SHORT_UNSIGNED_MAX_VALUE))
(def ^:no-doc interval-closed-integer (->IntervalClosed Integer/MIN_VALUE Integer/MAX_VALUE))
(def ^:no-doc interval-closed-integer-unsigned (->IntervalClosed INTEGER_UNSIGNED_MIN_VALUE INTEGER_UNSIGNED_MAX_VALUE))
(def ^:no-doc interval-closed-long (->IntervalClosed Long/MIN_VALUE Long/MAX_VALUE))
(def ^:no-doc interval-closed-long-unsigned (->IntervalClosed LONG_UNSIGNED_MIN_VALUE LONG_UNSIGNED_MAX_VALUE))
(s/defn within-bounds-byte? :- s/Bool
"Returns true if val fits within legal range for a byte (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-byte val))
(s/defn within-bounds-byte-unsigned? :- s/Bool
"Returns true if val fits within legal range for a byte (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-byte-unsigned val))
(s/defn within-bounds-short? :- s/Bool
"Returns true if val fits within legal range for a short (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-short val))
(s/defn within-bounds-short-unsigned? :- s/Bool
"Returns true if val fits within legal range for a short (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-short-unsigned val))
(s/defn within-bounds-integer? :- s/Bool
"Returns true if val fits within legal range for a integer (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-integer val))
(s/defn within-bounds-integer-unsigned? :- s/Bool
"Returns true if val fits within legal range for a integer (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-integer-unsigned val))
(s/defn within-bounds-long? :- s/Bool
"Returns true if val fits within legal range for a long (signed)."
[val :- s/Int]
(within-interval-closed? interval-closed-long val))
(s/defn within-bounds-long-unsigned? :- s/Bool
"Returns true if val fits within legal range for a long (unsigned)."
[val :- s/Int]
(within-interval-closed? interval-closed-long-unsigned val))