proposed new contrib: java-utils

3 views
Skip to first unread message

Stuart Halloway

unread,
Apr 3, 2009, 10:17:33 AM4/3/09
to clo...@googlegroups.com, James Reeves, Stephen C. Gilardi
Hi all,

I would like to pull together functions that help with Java interop
and place them in a new contrib: java-utils. Some examples:

(1) Pull out SteveG's properties fn, currently hidden in the internals
of clojure.contrib.sql

(2) reflective helpers for accessing private and protected values

(3) coercions for Java's irritating types: File/String, URL/String,
IPAddress/String, etc.

(4) Maybe some of the utils currently hiding in Compojure (James,
would that be cool with you? Have you signed/would you sign the
contributor agreement?)

If this is interesting to others (or at least inoffensive) I will move
forward with it.

Are there other little nuggets of Java-interop code that I should also
consider?

Cheers,
Stu

Stephen C. Gilardi

unread,
Apr 3, 2009, 10:41:03 AM4/3/09
to clo...@googlegroups.com

On Apr 3, 2009, at 10:17 AM, Stuart Halloway wrote:

> If this is interesting to others (or at least inoffensive) I will move
> forward with it.

I'm in favor.

I'd also like to get your latest thinking on your suggestions from
some time ago about clojure.contrib.lazy-seqs. If they can be
rewritten as you suggested as functions that return a new sequence
rather than as "def'd" sequences, that's strictly more powerful and
would be an improvement. (One can always use a def'd var to hold such
a function if that's desired in a particular application.)

--Steve

Kyle Schaffrick

unread,
Apr 3, 2009, 11:18:45 AM4/3/09
to clo...@googlegroups.com
On Fri, 3 Apr 2009 10:17:33 -0400
Stuart Halloway <stuart....@gmail.com> wrote:
>
> Hi all,
>
> I would like to pull together functions that help with Java interop
> and place them in a new contrib: java-utils. Some examples:
>
> [...]

>
> If this is interesting to others (or at least inoffensive) I will
> move forward with it.
>

I like it. Sounds like a "Clojure for people who don't know Java APIs"
layer. I'm in that group for the most part.

I do acknowledge and see the value in arguments against doing excessive
wrapping of Java-land, but at the same time, the Java libraries seem
quite baroque at times and can be difficult to understand for people who
haven't been steeped in Java idioms. Sometimes I find myself thinking,
"what on earth do I need to make all these objects for!?" (*cough* NIO
*cough*). But I must remind myself that perhaps such architectures solve
problems that I haven't had to deal with coming from other languages.
It's a tough issue.

> Are there other little nuggets of Java-interop code that I should
> also consider?
>

I would love to see a non-blocking IO API that isn't psychotic. My brain
starts to liquify when I read the NIO API documentation, and from what I
can tell, even seasoned Java folks find it a bit much :)

-Kyle

Stuart Sierra

unread,
Apr 4, 2009, 7:24:46 PM4/4/09
to Clojure
On Apr 3, 10:17 am, Stuart Halloway <stuart.hallo...@gmail.com> wrote:
> (3) coercions for Java's irritating types: File/String, ...

clojure.contrib.duck-streams has "file" but not the others

-the other Stuart

Stuart Halloway

unread,
Apr 5, 2009, 4:27:42 PM4/5/09
to clo...@googlegroups.com
> I'd also like to get your latest thinking on your suggestions from
> some time ago about clojure.contrib.lazy-seqs. If they can be
> rewritten as you suggested as functions that return a new sequence
> rather than as "def'd" sequences, that's strictly more powerful and
> would be an improvement. (One can always use a def'd var to hold
> such a function if that's desired in a particular application.)

Steve,

At quick glance it looks to me that all three of the lazy-seqs should
be functions. If you want me to I'll change this and add tests.

Stuart

Stephen C. Gilardi

unread,
Apr 5, 2009, 4:29:15 PM4/5/09
to clo...@googlegroups.com

On Apr 5, 2009, at 4:27 PM, Stuart Halloway wrote:

> At quick glance it looks to me that all three of the lazy-seqs should
> be functions. If you want me to I'll change this and add tests.

Please do. Thanks.

--Steve

Stuart Halloway

unread,
Apr 6, 2009, 9:02:42 AM4/6/09
to clo...@googlegroups.com
r652 is a breaking change to contrib. (powers-of-2) and (fibs) are now
functions and do not hold their heads. primes is still a sequence
because it needs to hold past values for efficiency.

Stuart

Perry Trolard

unread,
Apr 6, 2009, 11:55:02 AM4/6/09
to Clojure
Hi Stuart,

I had occasion to wrap java.util.Properties. If there's interest, may
be a good candidate for c.c.java-utils.

It's pasted in below, & attached in the group at

http://clojure.googlegroups.com/web/props.clj

Feel free to change as you see fit.

Best,
Perry

----

(ns props
; Convenience lib for interacting with java.util.Properties
(:use [clojure.contrib.duck-streams :only (reader writer)]
[clojure.contrib.java-utils :only (the-str)])
(:import (java.util Properties)))

(defn props-to-map
"Convert Properties to map."
[p]
(into {} p))

(defn map-to-props
"Convert map to Properties."
{:tag Properties}
[m]
(let [p (Properties.)]
(doseq [[k v] m]
(.setProperty p (the-str k) (str v)))
p))

(defn read-props
"Read Properties from file into map. Uses duck-streams/reader to
read from file,
so duck-streams/*default-encoding* determines character decoding."
[file]
(props-to-map
(with-open [rdr (reader file)]
(doto (Properties.)
(.load rdr)))))

(defn write-props
"Write Properties from map into file. Uses duck-streams/writer to
write to file,
so duck-streams/*default-encoding* determines character encoding."
{:tag Properties}
([m file] (write-props m file nil))
([m file comments]
(with-open [wtr (writer file)]
(doto (map-to-props m)
(.store wtr comments)))))



Reply all
Reply to author
Forward
0 new messages