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 package clojure.lang;
|
rlm@10
|
12
|
rlm@10
|
13 import java.io.IOException;
|
rlm@10
|
14 import java.io.NotSerializableException;
|
rlm@10
|
15 import java.util.Iterator;
|
rlm@10
|
16
|
rlm@10
|
17 public class IteratorSeq extends ASeq{
|
rlm@10
|
18 final Iterator iter;
|
rlm@10
|
19 final State state;
|
rlm@10
|
20
|
rlm@10
|
21 static class State{
|
rlm@10
|
22 volatile Object val;
|
rlm@10
|
23 volatile Object _rest;
|
rlm@10
|
24 }
|
rlm@10
|
25
|
rlm@10
|
26 public static IteratorSeq create(Iterator iter){
|
rlm@10
|
27 if(iter.hasNext())
|
rlm@10
|
28 return new IteratorSeq(iter);
|
rlm@10
|
29 return null;
|
rlm@10
|
30 }
|
rlm@10
|
31
|
rlm@10
|
32 IteratorSeq(Iterator iter){
|
rlm@10
|
33 this.iter = iter;
|
rlm@10
|
34 state = new State();
|
rlm@10
|
35 this.state.val = state;
|
rlm@10
|
36 this.state._rest = state;
|
rlm@10
|
37 }
|
rlm@10
|
38
|
rlm@10
|
39 IteratorSeq(IPersistentMap meta, Iterator iter, State state){
|
rlm@10
|
40 super(meta);
|
rlm@10
|
41 this.iter = iter;
|
rlm@10
|
42 this.state = state;
|
rlm@10
|
43 }
|
rlm@10
|
44
|
rlm@10
|
45 public Object first(){
|
rlm@10
|
46 if(state.val == state)
|
rlm@10
|
47 synchronized(state)
|
rlm@10
|
48 {
|
rlm@10
|
49 if(state.val == state)
|
rlm@10
|
50 state.val = iter.next();
|
rlm@10
|
51 }
|
rlm@10
|
52 return state.val;
|
rlm@10
|
53 }
|
rlm@10
|
54
|
rlm@10
|
55 public ISeq next(){
|
rlm@10
|
56 if(state._rest == state)
|
rlm@10
|
57 synchronized(state)
|
rlm@10
|
58 {
|
rlm@10
|
59 if(state._rest == state)
|
rlm@10
|
60 {
|
rlm@10
|
61 first();
|
rlm@10
|
62 state._rest = create(iter);
|
rlm@10
|
63 }
|
rlm@10
|
64 }
|
rlm@10
|
65 return (ISeq) state._rest;
|
rlm@10
|
66 }
|
rlm@10
|
67
|
rlm@10
|
68 public IteratorSeq withMeta(IPersistentMap meta){
|
rlm@10
|
69 return new IteratorSeq(meta, iter, state);
|
rlm@10
|
70 }
|
rlm@10
|
71
|
rlm@10
|
72 private void writeObject (java.io.ObjectOutputStream out) throws IOException {
|
rlm@10
|
73 throw new NotSerializableException(getClass().getName());
|
rlm@10
|
74 }
|
rlm@10
|
75 }
|