-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_conversion.clj
122 lines (108 loc) · 4.11 KB
/
type_conversion.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
;;;;
;;;; Copyright (c) Ludger Solbach. 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 license.txt 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 org.soulspace.clj.java.type-conversion
"Functions for the coercion of Java types.")
;;;
;;; Functions for the coercion of Java types
;;;
(defmulti coerce
"Coerce clojure data types to java data types"
(fn [dest-class obj] [dest-class (class obj)]))
; to Boolean
(defmethod coerce [Boolean/TYPE Boolean] [_ ^Boolean obj]
obj)
; to Byte
(defmethod coerce [Byte/TYPE Byte] [_ ^Byte obj]
(Byte. (.byteValue obj)))
;(defmethod coerce [Byte/TYPE Character] [_ obj]
; (Byte. (.byteValue obj)))
(defmethod coerce [Byte/TYPE Short] [_ ^Short obj]
(Byte. (.byteValue obj)))
(defmethod coerce [Byte/TYPE Integer] [_ ^Integer obj]
(Byte. (.byteValue obj)))
(defmethod coerce [Byte/TYPE Long] [_ ^Long obj]
(Byte. (.byteValue obj)))
(defmethod coerce [Byte/TYPE Float] [_ ^Float obj]
(Byte. (.byteValue obj)))
(defmethod coerce [Byte/TYPE Double] [_ ^Double obj]
(Byte. (.byteValue obj)))
; to Character
;(defmethod coerce [Character/TYPE Byte] [_ obj]
; (Character. (.charValue obj)))
(defmethod coerce [Character/TYPE Character] [_ ^Character obj]
(Character. (.charValue obj)))
;(defmethod coerce [Character/TYPE Short] [_ obj]
; (Character. (.charValue obj)))
;(defmethod coerce [Character/TYPE Integer] [_ obj]
; (Character. (.charValue obj)))
;(defmethod coerce [Character/TYPE Long] [_ obj]
; (Character. (.charValue obj)))
; to Short
(defmethod coerce [Short/TYPE Byte] [_ ^Byte obj]
(Short. (.shortValue obj)))
;(defmethod coerce [Short/TYPE Character] [_ obj]
; (Short. (.shortValue obj)))
(defmethod coerce [Short/TYPE Short] [_ ^Short obj]
(Short. (.shortValue obj)))
(defmethod coerce [Short/TYPE Integer] [_ ^Integer obj]
(Short. (.shortValue obj)))
(defmethod coerce [Short/TYPE Long] [_ ^Long obj]
(Short. (.shortValue obj)))
(defmethod coerce [Short/TYPE Float] [_ ^Float obj]
(Short. (.shortValue obj)))
(defmethod coerce [Short/TYPE Double] [_ ^Double obj]
(Short. (.shortValue obj)))
; to Integer
(defmethod coerce [Integer/TYPE Byte] [_ ^Byte obj]
(Integer. (.intValue obj)))
;(defmethod coerce [Integer/TYPE Character] [_ obj]
; (Integer. (.intValue obj)))
(defmethod coerce [Integer/TYPE Short] [_ ^Short obj]
(Integer. (.intValue obj)))
(defmethod coerce [Integer/TYPE Integer] [_ ^Integer obj]
(Integer. (.intValue obj)))
(defmethod coerce [Integer/TYPE Long] [_ ^Long obj]
(Integer. (.intValue obj)))
(defmethod coerce [Integer/TYPE Float] [_ ^Float obj]
(Integer. (.intValue obj)))
(defmethod coerce [Integer/TYPE Double] [_ ^Double obj]
(Integer. (.intValue obj)))
; to Long
(defmethod coerce [Long/TYPE Byte] [_ ^Byte obj]
(Long. (.longValue obj)))
;(defmethod coerce [Long/TYPE Character] [_ obj]
; (Long. (.longValue obj)))
(defmethod coerce [Long/TYPE Short] [_ ^Short obj]
(Long. (.longValue obj)))
(defmethod coerce [Long/TYPE Integer] [_ ^Integer obj]
(Long. (.longValue obj)))
(defmethod coerce [Long/TYPE Long] [_ ^Long obj]
(Long. (.longValue obj)))
(defmethod coerce [Long/TYPE Float] [_ ^Float obj]
(Long. (.longValue obj)))
(defmethod coerce [Long/TYPE Double] [_ ^Double obj]
(Long. (.longValue obj)))
; to File
(defmethod coerce [java.io.File String] [_ ^String str]
(java.io.File. str))
; to List
(defmethod coerce [java.util.List clojure.lang.PersistentVector] [_ ^java.util.List v]
(java.util.ArrayList. v))
; to Set
(defmethod coerce [java.util.Set clojure.lang.IPersistentSet] [_ ^java.util.Set s]
(java.util.HashSet. s))
; to Map
(defmethod coerce [java.util.Map clojure.lang.IPersistentMap] [_ ^java.util.Map m]
(java.util.HashMap. m))
(defmethod coerce :default [dest-class obj]
;(println (str (.getSimpleName dest-class) " " (type obj)))
(cast dest-class obj))