Fork me on GitHub
#clojure-europe
<
2022-11-26
>
Eugen09:11:16

hi, any idea how I can adapt Java generic code to clojure?

Cache<String, String> routeToAirline = new Cache2kBuilder<String, String>() {}
    .name("routeToAirline")
    .loader(new CacheLoader<String, String>() {
      @Override
      public String load(final String key) throws Exception {
        String[] port = key.split("-");
        return findFavoriteAirline(port[0], port[1]);
      }
    })
    .build();
My current code is like this:
(ns ro.ieugen.training.cache2k
  (:import (org.cache2k Cache2kBuilder)
           ( CacheLoader)))

(defn new-cache
  [f]
  (let [loader (reify CacheLoader
                 (load [_this key]
                   (f key)))]
    (-> (proxy [Cache2kBuilder] [])
        (.name "cache-example")
        (.loader loader)
        (.eternal true)
        (.entryCapacity 100)
        (.build))))
(comment
  (let [cache (new-cache #(str % "-key"))]
    (-> cache (.get "a")))
  )
but it does not work because the types are not known
clj꞉ro.ieugen.training.cache2k꞉> 
; Execution error (IllegalArgumentException) at org.cache2k.Cache2kBuilder/<init> (Cache2kBuilder.java:160).
; Use Cache2kBuilder.forUnknownTypes(), to construct a builder with no key and value types

Eugen09:11:10

I found the solution. use another API:

(ns ro.ieugen.training.cache2k
  (:import (org.cache2k Cache2kBuilder CacheManager)
           ( CacheLoader)))

(defn new-cache
  [f name]
  (let [loader (reify CacheLoader
                 (load [_this key]
                   (f key)))]
    (-> (Cache2kBuilder/of (class String) (class String))
        (.name name)
        (.loader loader)
        (.eternal true)
        (.entryCapacity 100) 
        (.build))))

(comment

  (CacheManager/closeAll)
  (new-cache #(str % "-key") "cache-example")
  (let [cache (-> (CacheManager/getInstance)
                  (.getCache "cache-example"))]
    (-> cache (.get "b")))
  )

Eugen09:11:22

(Cache2kBuilder/of (class String) (class String))

jkxyz13:11:45

(class String) will return java.lang.Class I think? I.e. the class of a Class instance. You can just use String as it refers to the class itself

Eugen14:11:42

I did not test that. I believe I can check with type or just println

pez09:11:48

God morgon!

Eugen09:11:51

good morning

genRaiy09:11:59

Good morning

🐑 4
javahippie10:11:02

Sooooooo soft

borkdude11:11:19

writing an article for the graalvm blog about babashka. I want to keep it accessible for Java programmers. If anyone wants to help proofread, let me know

Eugen14:11:15

I can take a look later today

borkdude14:11:46

@U011NGC5FFY Thanks, give me your github handle and I'll add you to the repo

borkdude14:11:00

I will send the article on Monday

borkdude14:11:39

I mean, the finished one to the graal blog

Eugen06:11:48

hi, I read the article and I think it's pretty good in general. Regarding to accessibility part for Java developers: I don't know. I would be inclined to say it's not very accessible - since it's not a lot of Java related stuff there. Most of the things are Clojure specific and a bit graalvm. I would say that maybe jshell would be worth mentioning and compared with the REPL ? (not sure jshell is used that much) . also, I would imagine tooling like maven and gradle are much more used in Java and more familiar since there where no jshell or graalvm until recently. That IMO led Java devs on the tooling path.

Eugen06:11:40

overall it's a good read