Mercurial > lasercutter
comparison src/clojure/lang/LazySeq.java @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
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 **/ | |
10 | |
11 /* rich Jan 31, 2009 */ | |
12 | |
13 package clojure.lang; | |
14 | |
15 import java.util.*; | |
16 | |
17 public final class LazySeq extends Obj implements ISeq, List{ | |
18 | |
19 private IFn fn; | |
20 private Object sv; | |
21 private ISeq s; | |
22 | |
23 public LazySeq(IFn fn){ | |
24 this.fn = fn; | |
25 } | |
26 | |
27 private LazySeq(IPersistentMap meta, ISeq s){ | |
28 super(meta); | |
29 this.fn = null; | |
30 this.s = s; | |
31 } | |
32 | |
33 public Obj withMeta(IPersistentMap meta){ | |
34 return new LazySeq(meta, seq()); | |
35 } | |
36 | |
37 final synchronized Object sval(){ | |
38 if(fn != null) | |
39 { | |
40 try | |
41 { | |
42 sv = fn.invoke(); | |
43 fn = null; | |
44 } | |
45 catch(Exception e) | |
46 { | |
47 throw new RuntimeException(e); | |
48 } | |
49 } | |
50 if(sv != null) | |
51 return sv; | |
52 return s; | |
53 } | |
54 | |
55 final synchronized public ISeq seq(){ | |
56 sval(); | |
57 if(sv != null) | |
58 { | |
59 Object ls = sv; | |
60 sv = null; | |
61 while(ls instanceof LazySeq) | |
62 { | |
63 ls = ((LazySeq)ls).sval(); | |
64 } | |
65 s = RT.seq(ls); | |
66 } | |
67 return s; | |
68 } | |
69 | |
70 public int count(){ | |
71 int c = 0; | |
72 for(ISeq s = seq(); s != null; s = s.next()) | |
73 ++c; | |
74 return c; | |
75 } | |
76 | |
77 public Object first(){ | |
78 seq(); | |
79 if(s == null) | |
80 return null; | |
81 return s.first(); | |
82 } | |
83 | |
84 public ISeq next(){ | |
85 seq(); | |
86 if(s == null) | |
87 return null; | |
88 return s.next(); | |
89 } | |
90 | |
91 public ISeq more(){ | |
92 seq(); | |
93 if(s == null) | |
94 return PersistentList.EMPTY; | |
95 return s.more(); | |
96 } | |
97 | |
98 public ISeq cons(Object o){ | |
99 return RT.cons(o, seq()); | |
100 } | |
101 | |
102 public IPersistentCollection empty(){ | |
103 return PersistentList.EMPTY; | |
104 } | |
105 | |
106 public boolean equiv(Object o){ | |
107 return equals(o); | |
108 } | |
109 | |
110 public int hashCode(){ | |
111 return Util.hash(seq()); | |
112 } | |
113 | |
114 public boolean equals(Object o){ | |
115 ISeq s = seq(); | |
116 if(s != null) | |
117 return s.equiv(o); | |
118 else | |
119 return (o instanceof Sequential || o instanceof List) && RT.seq(o) == null; | |
120 } | |
121 | |
122 | |
123 // java.util.Collection implementation | |
124 | |
125 public Object[] toArray(){ | |
126 return RT.seqToArray(seq()); | |
127 } | |
128 | |
129 public boolean add(Object o){ | |
130 throw new UnsupportedOperationException(); | |
131 } | |
132 | |
133 public boolean remove(Object o){ | |
134 throw new UnsupportedOperationException(); | |
135 } | |
136 | |
137 public boolean addAll(Collection c){ | |
138 throw new UnsupportedOperationException(); | |
139 } | |
140 | |
141 public void clear(){ | |
142 throw new UnsupportedOperationException(); | |
143 } | |
144 | |
145 public boolean retainAll(Collection c){ | |
146 throw new UnsupportedOperationException(); | |
147 } | |
148 | |
149 public boolean removeAll(Collection c){ | |
150 throw new UnsupportedOperationException(); | |
151 } | |
152 | |
153 public boolean containsAll(Collection c){ | |
154 for(Object o : c) | |
155 { | |
156 if(!contains(o)) | |
157 return false; | |
158 } | |
159 return true; | |
160 } | |
161 | |
162 public Object[] toArray(Object[] a){ | |
163 if(a.length >= count()) | |
164 { | |
165 ISeq s = seq(); | |
166 for(int i = 0; s != null; ++i, s = s.next()) | |
167 { | |
168 a[i] = s.first(); | |
169 } | |
170 if(a.length > count()) | |
171 a[count()] = null; | |
172 return a; | |
173 } | |
174 else | |
175 return toArray(); | |
176 } | |
177 | |
178 public int size(){ | |
179 return count(); | |
180 } | |
181 | |
182 public boolean isEmpty(){ | |
183 return seq() == null; | |
184 } | |
185 | |
186 public boolean contains(Object o){ | |
187 for(ISeq s = seq(); s != null; s = s.next()) | |
188 { | |
189 if(Util.equiv(s.first(), o)) | |
190 return true; | |
191 } | |
192 return false; | |
193 } | |
194 | |
195 public Iterator iterator(){ | |
196 return new SeqIterator(seq()); | |
197 } | |
198 | |
199 //////////// List stuff ///////////////// | |
200 private List reify(){ | |
201 return new ArrayList(this); | |
202 } | |
203 | |
204 public List subList(int fromIndex, int toIndex){ | |
205 return reify().subList(fromIndex, toIndex); | |
206 } | |
207 | |
208 public Object set(int index, Object element){ | |
209 throw new UnsupportedOperationException(); | |
210 } | |
211 | |
212 public Object remove(int index){ | |
213 throw new UnsupportedOperationException(); | |
214 } | |
215 | |
216 public int indexOf(Object o){ | |
217 ISeq s = seq(); | |
218 for(int i = 0; s != null; s = s.next(), i++) | |
219 { | |
220 if(Util.equiv(s.first(), o)) | |
221 return i; | |
222 } | |
223 return -1; | |
224 } | |
225 | |
226 public int lastIndexOf(Object o){ | |
227 return reify().lastIndexOf(o); | |
228 } | |
229 | |
230 public ListIterator listIterator(){ | |
231 return reify().listIterator(); | |
232 } | |
233 | |
234 public ListIterator listIterator(int index){ | |
235 return reify().listIterator(index); | |
236 } | |
237 | |
238 public Object get(int index){ | |
239 return RT.nth(this, index); | |
240 } | |
241 | |
242 public void add(int index, Object element){ | |
243 throw new UnsupportedOperationException(); | |
244 } | |
245 | |
246 public boolean addAll(int index, Collection c){ | |
247 throw new UnsupportedOperationException(); | |
248 } | |
249 | |
250 | |
251 } |