diff options
author | Rich Hickey <richhickey@gmail.com> | 2008-03-03 15:57:46 +0000 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2008-03-03 15:57:46 +0000 |
commit | 1eb86f5103d7aa46b40d1cdfca343a0e30f53237 (patch) | |
tree | 7206bff8bbde66f2c80cb989acbdfbbe60efcb1c | |
parent | 68a15c23a2d76350e49451150a990d9141a4a526 (diff) |
added EnumerationSeq and support for Enumerations in seq
-rw-r--r-- | src/jvm/clojure/lang/EnumerationSeq.java | 71 | ||||
-rw-r--r-- | src/jvm/clojure/lang/RT.java | 7 |
2 files changed, 74 insertions, 4 deletions
diff --git a/src/jvm/clojure/lang/EnumerationSeq.java b/src/jvm/clojure/lang/EnumerationSeq.java new file mode 100644 index 00000000..daf62bc0 --- /dev/null +++ b/src/jvm/clojure/lang/EnumerationSeq.java @@ -0,0 +1,71 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Common Public License 1.0 (http://opensource.org/licenses/cpl.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. + **/ + +/* rich Mar 3, 2008 */ + +package clojure.lang; + +import java.util.Enumeration; + +public class EnumerationSeq extends ASeq{ +final Enumeration iter; +final State state; + +static class State{ + volatile Object val; + volatile Object _rest; +} + +public static EnumerationSeq create(Enumeration iter){ + if(iter.hasMoreElements()) + return new EnumerationSeq(iter); + return null; +} + +EnumerationSeq(Enumeration iter){ + this.iter = iter; + state = new State(); + this.state.val = state; + this.state._rest = state; +} + +EnumerationSeq(IPersistentMap meta, Enumeration iter, State state){ + super(meta); + this.iter = iter; + this.state = state; +} + +public Object first(){ + if(state.val == state) + synchronized(state) + { + if(state.val == state) + state.val = iter.nextElement(); + } + return state.val; +} + +public ISeq rest(){ + if(state._rest == state) + synchronized(state) + { + if(state._rest == state) + { + first(); + state._rest = create(iter); + } + } + return (ISeq) state._rest; +} + +public EnumerationSeq withMeta(IPersistentMap meta){ + return new EnumerationSeq(meta, iter, state); +} +} diff --git a/src/jvm/clojure/lang/RT.java b/src/jvm/clojure/lang/RT.java index 52bf88db..c61d8b8a 100644 --- a/src/jvm/clojure/lang/RT.java +++ b/src/jvm/clojure/lang/RT.java @@ -13,10 +13,7 @@ package clojure.lang; import java.util.concurrent.atomic.AtomicInteger; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Arrays; +import java.util.*; import java.util.regex.Matcher; import java.io.*; import java.lang.reflect.Array; @@ -331,6 +328,8 @@ static public ISeq seq(Object coll){ return StringSeq.create((String) coll); else if(coll instanceof Map) return seq(((Map) coll).entrySet()); + else if(coll instanceof Enumeration) + return EnumerationSeq.create(((Enumeration) coll)); else throw new IllegalAccessError("Don't know how to create ISeq from arg"); } |