diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/lang/IteratorSeq.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,75 @@
     1.4 +/**
     1.5 + *   Copyright (c) Rich Hickey. All rights reserved.
     1.6 + *   The use and distribution terms for this software are covered by the
     1.7 + *   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.8 + *   which can be found in the file epl-v10.html at the root of this distribution.
     1.9 + *   By using this software in any fashion, you are agreeing to be bound by
    1.10 + * 	 the terms of this license.
    1.11 + *   You must not remove this notice, or any other, from this software.
    1.12 + **/
    1.13 +
    1.14 +package clojure.lang;
    1.15 +
    1.16 +import java.io.IOException;
    1.17 +import java.io.NotSerializableException;
    1.18 +import java.util.Iterator;
    1.19 +
    1.20 +public class IteratorSeq extends ASeq{
    1.21 +final Iterator iter;
    1.22 +final State state;
    1.23 +
    1.24 +    static class State{
    1.25 +	volatile Object val;
    1.26 +	volatile Object _rest;
    1.27 +}
    1.28 +
    1.29 +public static IteratorSeq create(Iterator iter){
    1.30 +	if(iter.hasNext())
    1.31 +		return new IteratorSeq(iter);
    1.32 +	return null;
    1.33 +}
    1.34 +
    1.35 +IteratorSeq(Iterator iter){
    1.36 +	this.iter = iter;
    1.37 +	state = new State();
    1.38 +	this.state.val = state;
    1.39 +	this.state._rest = state;
    1.40 +}
    1.41 +
    1.42 +IteratorSeq(IPersistentMap meta, Iterator iter, State state){
    1.43 +	super(meta);
    1.44 +	this.iter = iter;
    1.45 +	this.state = state;
    1.46 +}
    1.47 +
    1.48 +public Object first(){
    1.49 +	if(state.val == state)
    1.50 +		synchronized(state)
    1.51 +			{
    1.52 +			if(state.val == state)
    1.53 +				state.val = iter.next();
    1.54 +			}
    1.55 +	return state.val;
    1.56 +}
    1.57 +
    1.58 +public ISeq next(){
    1.59 +	if(state._rest == state)
    1.60 +		synchronized(state)
    1.61 +			{
    1.62 +			if(state._rest == state)
    1.63 +				{
    1.64 +				first();
    1.65 +				state._rest = create(iter);
    1.66 +				}
    1.67 +			}
    1.68 +	return (ISeq) state._rest;
    1.69 +}
    1.70 +
    1.71 +public IteratorSeq withMeta(IPersistentMap meta){
    1.72 +	return new IteratorSeq(meta, iter, state);
    1.73 +}
    1.74 +
    1.75 +private void writeObject (java.io.ObjectOutputStream out) throws IOException {
    1.76 +    throw new NotSerializableException(getClass().getName());
    1.77 +}
    1.78 +}