aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/enum
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2008-08-16 22:48:23 +0000
committerscgilardi <scgilardi@gmail.com>2008-08-16 22:48:23 +0000
commit3dcb49711054c8f63313a6296cc23404d9e27294 (patch)
treee47ed2f55bceda2cb638f857aaa5188809e7aac4 /src/clojure/contrib/enum
parent1341206e051b4ad9473e8a2a3968f58510c628d7 (diff)
move namespace-directory-aware libs into src/clojure/contrib
Diffstat (limited to 'src/clojure/contrib/enum')
-rw-r--r--src/clojure/contrib/enum/enum.clj47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/clojure/contrib/enum/enum.clj b/src/clojure/contrib/enum/enum.clj
new file mode 100644
index 00000000..b417028d
--- /dev/null
+++ b/src/clojure/contrib/enum/enum.clj
@@ -0,0 +1,47 @@
+;;; enum.clj -- Java enum classes in Clojure
+
+;; by Stuart Sierra, http://www.stuartsierra.com/
+;; May 29, 2008
+
+;; Copyright (c) 2008 Stuart Sierra. All rights reserved. The use and
+;; distribution terms for this software are covered by the Common
+;; Public License 1.0 (http://www.opensource.org/licenses/cpl1.0.php)
+;; which can be found in the file CPL.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.
+
+
+;; This file helps define Java Enums, introduced in Java 1.5. Use it
+;; when you need to define an enum to pass to a Java method.
+;;
+;; This file depends on genclass.clj in the Clojure distribution.
+
+
+(clojure/in-ns 'clojure.contrib.enum)
+(clojure/refer 'clojure)
+
+(defmacro defenum
+ "Generates and loads a subclass of java.lang.Enum, then
+ defs symbols as enumerated instances of that class.
+
+ Example: (defenum my.package.MyEnum FOO BAR)
+ ;; FOO and BAR are now instances of MyEnum
+
+ Java equivalent: enum MyEnum { FOO, BAR };
+
+ Caveats:
+ 1. The generated class has no values() method.
+ 2. The generated class returns false for Class.isEnum().
+ 3. Enum.valueOf(Class, String) will not work.
+ 4. Redefining an enum is allowed, but enumeration resets
+ to zero."
+ [class & symbols]
+ ;; Can't load a class twice, so check first:
+ (try (. Class (forName (str class)))
+ (catch java.lang.ClassNotFoundException e
+ (gen-and-load-class (str class) :extends java.lang.Enum)))
+ (cons 'do
+ (map (fn [sym val]
+ `(def ~sym (new ~class ~(str sym) ~val)))
+ symbols (iterate inc 0))))