summaryrefslogtreecommitdiffstats
path: root/org.aspectj.matcher/src/org/aspectj/weaver/Iterators.java
blob: 1a2e820061a219c0ef65e036928b0c0bb771ea83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* *******************************************************************
 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     PARC     initial implementation 
 * ******************************************************************/

package org.aspectj.weaver;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

public final class Iterators {

	/**
	 * Private constructor, nobody should ever make one of these
	 */
	private Iterators() {
	}

	/**
	 * A getter represents a mapping function from Object to Iterator
	 */
	public interface Getter<A, B> {
		Iterator<B> get(A target);
	}

	/**
	 * A filter represents a mapping function from Iterator to Iterator
	 */
	public interface Filter<T> {
		Iterator<T> filter(Iterator<T> in);
	}

	/**
	 * Create a new filter F that, when wrapped around another iterator I, creates a new iterator I' that will return only those
	 * values of I that have not yet been returned by I', discarding duplicates.
	 */
	public static <T> Filter<T> dupFilter() {
		return new Filter<T>() {
			final Set<T> seen = new HashSet<T>(); // should have weak ptrs?

			public Iterator<T> filter(final Iterator<T> in) {
				return new Iterator<T>() {
					boolean fresh = false;
					T peek;

					public boolean hasNext() {
						if (fresh) {
							return true;
						}
						while (true) {
							if (!in.hasNext()) {
								return false;
							}
							peek = in.next();
							if (!seen.contains(peek)) {
								fresh = true;
								return true;
							} else {
								peek = null; // garbage collection
							}
						}
					}

					public T next() {
						if (!hasNext()) {
							throw new NoSuchElementException();
						}
						T ret = peek;
						seen.add(peek);
						peek = null;
						fresh = false;
						return ret;
					}

					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}

	/**
	 * Creates an iterator that will return the elements of a specified array, in order. Like Arrays.asList(o).iterator(), without
	 * all that pesky safety.
	 */

	public static <T> Iterator<T> array(final T[] o) {
		return new Iterator<T>() {
			int i = 0;
			int len = (o == null) ? 0 : o.length;

			public boolean hasNext() {
				return i < len;
			}

			public T next() {
				if (i < len) {
					return o[i++];
				} else {
					throw new NoSuchElementException();
				}
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	public static class ResolvedTypeArrayIterator implements Iterator<ResolvedType> {
		private ResolvedType[] array;
		private int index;
		private int len;
		private boolean wantGenerics;
		private List<String> alreadySeen; // type signatures

		public ResolvedTypeArrayIterator(ResolvedType[] array, List<String> alreadySeen, boolean wantGenerics) {
			assert array != null;
			this.array = array;
			this.wantGenerics = wantGenerics;
			this.len = array.length;
			this.index = 0;
			this.alreadySeen = alreadySeen;
			moveToNextNewOne();
		}

		private void moveToNextNewOne() {
			while (index < len) {
				ResolvedType interfaceType = array[index];
				if (!wantGenerics && interfaceType.isParameterizedOrGenericType()) {
					interfaceType = interfaceType.getRawType();
				}
				String signature = interfaceType.getSignature();
				if (!alreadySeen.contains(signature)) {
					break;
				}
				index++;
			}
		}

		public boolean hasNext() {
			return index < len;
		}

		public ResolvedType next() {
			if (index < len) {
				ResolvedType oo = array[index++];
				if (!wantGenerics && (oo.isParameterizedType() || oo.isGenericType())) {
					oo = oo.getRawType();
				}
				alreadySeen.add(oo.getSignature());
				moveToNextNewOne();
				return oo;
			} else {
				throw new NoSuchElementException();
			}
		}

		public void remove() {
			throw new UnsupportedOperationException();
		}
	}

	public static Iterator<ResolvedType> array(final ResolvedType[] o, final boolean genericsAware) {
		return new Iterator<ResolvedType>() {
			int i = 0;
			int len = (o == null) ? 0 : o.length;

			public boolean hasNext() {
				return i < len;
			}

			public ResolvedType next() {
				if (i < len) {
					ResolvedType oo = o[i++];
					if (!genericsAware && (oo.isParameterizedType() || oo.isGenericType())) {
						return oo.getRawType();
					}
					return oo;
				} else {
					throw new NoSuchElementException();
				}
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	/**
	 * creates an iterator I based on a base iterator A and a getter G. I returns, in order, forall (i in A), G(i).
	 */
	public static <A, B> Iterator<B> mapOver(final Iterator<A> a, final Getter<A, B> g) {
		return new Iterator<B>() {
			Iterator<B> delegate = new Iterator<B>() {
				public boolean hasNext() {
					if (!a.hasNext()) {
						return false;
					}
					A o = a.next();
					delegate = append1(g.get(o), this);
					return delegate.hasNext();
				}

				public B next() {
					if (!hasNext()) {
						throw new UnsupportedOperationException();
					}
					return delegate.next();
				}

				public void remove() {
					throw new UnsupportedOperationException();
				}
			};

			public boolean hasNext() {
				return delegate.hasNext();
			}

			public B next() {
				return delegate.next();
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	/**
	 * creates an iterator I based on a base iterator A and a getter G. I returns, in order, forall (i in I) i :: forall (i' in
	 * g(i)) recur(i', g)
	 */
	public static <A> Iterator<A> recur(final A a, final Getter<A, A> g) {
		return new Iterator<A>() {
			Iterator<A> delegate = one(a);

			public boolean hasNext() {
				return delegate.hasNext();
			}

			public A next() {
				A next = delegate.next();
				delegate = append(g.get(next), delegate);
				return next;
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	/**
	 * creates an iterator I based on base iterators A and B. Returns the elements returned by A followed by those returned by B. If
	 * B is empty, simply returns A, and if A is empty, simply returns B. Do NOT USE if b.hasNext() is not idempotent.
	 */
	public static <T> Iterator<T> append(final Iterator<T> a, final Iterator<T> b) {
		if (!b.hasNext()) {
			return a;
		}
		return append1(a, b);
	}

	/**
	 * creates an iterator I based on base iterators A and B. Returns the elements returned by A followed by those returned by B. If
	 * A is empty, simply returns B. Guaranteed not to call B.hasNext() until A is empty.
	 */
	public static <T> Iterator<T> append1(final Iterator<T> a, final Iterator<T> b) {
		if (!a.hasNext()) {
			return b;
		}
		return new Iterator<T>() {
			public boolean hasNext() {
				return a.hasNext() || b.hasNext();
			}

			public T next() {
				if (a.hasNext()) {
					return a.next();
				}
				if (b.hasNext()) {
					return b.next();
				}
				throw new NoSuchElementException();
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	/**
	 * creates an iterator I based on a base iterator A and an object O. Returns the elements returned by A, followed by O.
	 */
	public static <T> Iterator<T> snoc(final Iterator<T> first, final T last) {
		return new Iterator<T>() {
			T last1 = last;

			public boolean hasNext() {
				return first.hasNext() || last1 != null;
			}

			public T next() {
				if (first.hasNext()) {
					return first.next();
				} else if (last1 == null) {
					throw new NoSuchElementException();
				}
				T ret = last1;
				last1 = null;
				return ret;
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}

	/**
	 * creates an iterator I based on an object O. Returns O, once.
	 */
	public static <T> Iterator<T> one(final T it) {
		return new Iterator<T>() {
			boolean avail = true;

			public boolean hasNext() {
				return avail;
			}

			public T next() {
				if (!avail) {
					throw new NoSuchElementException();
				}
				avail = false;
				return it;
			}

			public void remove() {
				throw new UnsupportedOperationException();
			}
		};
	}
}