forked from dm3/clojure.java-time
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cljc
More file actions
96 lines (83 loc) · 3.92 KB
/
util.cljc
File metadata and controls
96 lines (83 loc) · 3.92 KB
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
(ns java-time.util
(:require [clojure.string :as string]))
(defn ^:deprecated get-static-fields-of-type
[^Class klass, ^Class of-type]
(->> (seq (.getFields klass))
(map (fn [^java.lang.reflect.Field f]
(when (.isAssignableFrom of-type (.getType f))
[(.getName f) (.get f nil)])) )
(keep identity)
(into {})))
(defn dashize [camelcase]
(let [words (re-seq #"([^A-Z]+|[A-Z]+[^A-Z]*)" camelcase)]
(string/join "-" (map (comp string/lower-case first) words))))
(defmacro if-class [clstr then else]
(let [loaded (try (Class/forName clstr)
(catch Throwable e))]
(if #?(:bb (and (resolve (symbol clstr)) loaded)
:default loaded)
then
else)))
(defmacro when-class [clstr & body]
`(if-class ~clstr (do ~@body) nil))
(defmacro if-threeten-extra [then-body else-body]
`(if-class "org.threeten.extra.Temporals"
~then-body
~else-body))
(defmacro when-threeten-extra [& body]
`(if-threeten-extra (do ~@body) nil))
(defmacro when-joda-time-loaded
"Execute the `body` when Joda-Time classes are found on the classpath.
Take care - when AOT-compiling code using this macro, the Joda-Time classes
must be on the classpath at compile time!"
[& body]
(if (try (Class/forName "org.joda.time.DateTime")
(catch Throwable e))
`(do ~@body)))
;; From Medley, C Weavejester
(defn editable? [coll]
(instance? clojure.lang.IEditableCollection coll))
(defn reduce-map [f coll]
(if (editable? coll)
(persistent! (reduce-kv (f assoc!) (transient (empty coll)) coll))
(reduce-kv (f assoc) (empty coll) coll)))
(defn map-vals
"Maps a function over the values of an associative collection."
[f coll]
(reduce-map (fn [xf] (fn [m k v] (xf m k (f v)))) coll))
(defn map-kv
"Maps a function over the key/value pairs of an associate collection. Expects
a function that takes two arguments, the key and value, and returns the new
key and value as a collection of two elements."
[f coll]
(reduce-map (fn [xf] (fn [m k v] (let [[k v] (f k v)] (xf m k v)))) coll))
(defn class->TemporalAccessor-static-fields [^Class klass]
{:pre [(class? klass)]}
(case (.getName klass)
"java.time.Month" {"JUNE" java.time.Month/JUNE
"DECEMBER" java.time.Month/DECEMBER
"SEPTEMBER" java.time.Month/SEPTEMBER
"OCTOBER" java.time.Month/OCTOBER
"FEBRUARY" java.time.Month/FEBRUARY
"MAY" java.time.Month/MAY
"AUGUST" java.time.Month/AUGUST
"MARCH" java.time.Month/MARCH
"JANUARY" java.time.Month/JANUARY
"JULY" java.time.Month/JULY
"APRIL" java.time.Month/APRIL
"NOVEMBER" java.time.Month/NOVEMBER}
"java.time.DayOfWeek" {"MONDAY" java.time.DayOfWeek/MONDAY
"TUESDAY" java.time.DayOfWeek/TUESDAY
"WEDNESDAY" java.time.DayOfWeek/WEDNESDAY
"THURSDAY" java.time.DayOfWeek/THURSDAY
"FRIDAY" java.time.DayOfWeek/FRIDAY
"SATURDAY" java.time.DayOfWeek/SATURDAY
"SUNDAY" java.time.DayOfWeek/SUNDAY}
"org.threeten.extra.AmPm" (if-threeten-extra
{"AM" org.threeten.extra.AmPm/AM, "PM" org.threeten.extra.AmPm/PM}
(throw (ex-info "org.threeten.extra.AmPm not found")))
"org.threeten.extra.Quarter" (if-threeten-extra
{"Q1" org.threeten.extra.Quarter/Q1, "Q2" org.threeten.extra.Quarter/Q2, "Q3" org.threeten.extra.Quarter/Q3, "Q4" org.threeten.extra.Quarter/Q4}
(throw (ex-info "org.threeten.extra.Quarter not found")))
(throw (ex-info (str "TODO class->TemporalAccessor-static-fields " klass)
{}))))