rlm@10
|
1 /**
|
rlm@10
|
2 * Copyright (c) Rich Hickey. All rights reserved.
|
rlm@10
|
3 * The use and distribution terms for this software are covered by the
|
rlm@10
|
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
5 * which can be found in the file epl-v10.html at the root of this distribution.
|
rlm@10
|
6 * By using this software in any fashion, you are agreeing to be bound by
|
rlm@10
|
7 * the terms of this license.
|
rlm@10
|
8 * You must not remove this notice, or any other, from this software.
|
rlm@10
|
9 **/
|
rlm@10
|
10
|
rlm@10
|
11 /* rich Mar 3, 2008 */
|
rlm@10
|
12
|
rlm@10
|
13 package clojure.lang;
|
rlm@10
|
14
|
rlm@10
|
15 import java.io.IOException;
|
rlm@10
|
16 import java.io.NotSerializableException;
|
rlm@10
|
17 import java.util.Enumeration;
|
rlm@10
|
18
|
rlm@10
|
19 public class EnumerationSeq extends ASeq{
|
rlm@10
|
20 final Enumeration iter;
|
rlm@10
|
21 final State state;
|
rlm@10
|
22
|
rlm@10
|
23 static class State{
|
rlm@10
|
24 volatile Object val;
|
rlm@10
|
25 volatile Object _rest;
|
rlm@10
|
26 }
|
rlm@10
|
27
|
rlm@10
|
28 public static EnumerationSeq create(Enumeration iter){
|
rlm@10
|
29 if(iter.hasMoreElements())
|
rlm@10
|
30 return new EnumerationSeq(iter);
|
rlm@10
|
31 return null;
|
rlm@10
|
32 }
|
rlm@10
|
33
|
rlm@10
|
34 EnumerationSeq(Enumeration iter){
|
rlm@10
|
35 this.iter = iter;
|
rlm@10
|
36 state = new State();
|
rlm@10
|
37 this.state.val = state;
|
rlm@10
|
38 this.state._rest = state;
|
rlm@10
|
39 }
|
rlm@10
|
40
|
rlm@10
|
41 EnumerationSeq(IPersistentMap meta, Enumeration iter, State state){
|
rlm@10
|
42 super(meta);
|
rlm@10
|
43 this.iter = iter;
|
rlm@10
|
44 this.state = state;
|
rlm@10
|
45 }
|
rlm@10
|
46
|
rlm@10
|
47 public Object first(){
|
rlm@10
|
48 if(state.val == state)
|
rlm@10
|
49 synchronized(state)
|
rlm@10
|
50 {
|
rlm@10
|
51 if(state.val == state)
|
rlm@10
|
52 state.val = iter.nextElement();
|
rlm@10
|
53 }
|
rlm@10
|
54 return state.val;
|
rlm@10
|
55 }
|
rlm@10
|
56
|
rlm@10
|
57 public ISeq next(){
|
rlm@10
|
58 if(state._rest == state)
|
rlm@10
|
59 synchronized(state)
|
rlm@10
|
60 {
|
rlm@10
|
61 if(state._rest == state)
|
rlm@10
|
62 {
|
rlm@10
|
63 first();
|
rlm@10
|
64 state._rest = create(iter);
|
rlm@10
|
65 }
|
rlm@10
|
66 }
|
rlm@10
|
67 return (ISeq) state._rest;
|
rlm@10
|
68 }
|
rlm@10
|
69
|
rlm@10
|
70 public EnumerationSeq withMeta(IPersistentMap meta){
|
rlm@10
|
71 return new EnumerationSeq(meta, iter, state);
|
rlm@10
|
72 }
|
rlm@10
|
73
|
rlm@10
|
74 private void writeObject (java.io.ObjectOutputStream out) throws IOException {
|
rlm@10
|
75 throw new NotSerializableException(getClass().getName());
|
rlm@10
|
76 }
|
rlm@10
|
77
|
rlm@10
|
78 }
|