view src/clojure/lang/IteratorSeq.java @ 10:ef7dbbd6452c

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