-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTo.java
More file actions
435 lines (384 loc) · 12 KB
/
Copy pathTo.java
File metadata and controls
435 lines (384 loc) · 12 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.*;
/**
See docs and examples at https://github.com/curtcox/Loopy-Java/
*/
public class To<X>
extends Observable
implements Iterator<X>, Collection<X>, Enumeration<X>, Runnable, Callable, Observer
{
/**
* The iterator we defer to
*/
private Iterator<X> iterator;
/**
* The loop variable value
*/
public final X x;
/**
* The arguments for this To
*/
private final List<X> args = new ArrayList();
/**
* The constructor for the (generally anonymous) class that extends this one.
*/
private final Constructor<?> extending;
/**
* The formal parameters of the extending constructor.
*/
private final Class[] formals;
/**
* The type of argument that was passed in the constructor
*/
private final Type type;
/**
* The types of arguments that can be passed in the constructor.
*/
private enum Type { VARARG, ITERATOR, ITERABLE, ENUMERATION; }
/**
* This wraps an arg, so that it can be passed through the constructor.
*/
private static final class ArgWrapper implements Iterator, Iterable, Enumeration {
/**
* The single value we hold.
*/
final Object x;
final Constructor<?> extending;
final Class[] formals;
ArgWrapper(Object x, Constructor<?> extending, Class[] formals) {
this.x = x;
this.extending = extending;
this.formals = formals;
}
public Iterator iterator() { return this; }
public boolean hasMoreElements() { return true; }
public Object nextElement() { return x; }
public boolean hasNext() { return true; }
public Object next() { return x; }
public void remove() {}
} // ArgWrapper
/**
* Override this constructor for looping through individually specified values.
*/
protected To(X... xs) {
type = Type.VARARG;
if (xs==null || xs.length==0) {
throw badArgs(xs);
}
// ArgWrapper
// This was created by the last if block in this constructor.
// Reflectively create a new normal instance.
if (xs[0] instanceof ArgWrapper) {
ArgWrapper wrapper = (ArgWrapper) xs[0];
x = (X) wrapper.x;
extending = wrapper.extending;
formals = wrapper.formals;
return;
}
extending = extendingConstructor();
formals = extending.getParameterTypes();
// single value -- we can handle this
if (xs.length == 1) {
x = xs[0];
return;
}
// Multiple values
// This is the constructor our subclass will use.
if (xs.length > 1) {
for (int i=0; i<xs.length-1; i++) {
X arg = xs[i];
args.add(arg);
newInstanceWithArg(this,arg);
}
x = xs[xs.length - 1];
args.add(x);
return;
}
throw badArgs(xs);
}
/**
* Override this constructor for looping through individually specified values.
*/
protected To(Iterable<X> iterable) {
type = Type.ITERABLE;
if (iterable==null) {
throw badArgs();
}
extending = extendingConstructor();
formals = extending.getParameterTypes();
// ArgWrapper
// This was created by the last if block in this constructor.
// Reflectively create a new normal instance.
if (iterable instanceof ArgWrapper) {
ArgWrapper wrapper = (ArgWrapper) iterable;
x = (X) wrapper.x;
return;
}
// must not be empty
Iterator i = iterable.iterator();
if (!i.hasNext()) {
throw badArgs();
}
X arg = (X) i.next();
// single value -- we can handle this
if (!i.hasNext()) {
x = arg;
args.add(arg);
return;
}
// Multiple values
// This is the constructor our subclass will use.
for (; i.hasNext(); arg = (X) i.next()) {
args.add(arg);
if (i.hasNext()) {
newInstanceWithArg(this,arg);
}
}
args.add(arg);
x = arg;
}
/**
* Override this constructor for looping through individually specified values.
*/
protected To(Iterator<X> i) {
type = Type.ITERATOR;
if (i==null) {
throw badArgs();
}
// ArgWrapper
// This was created by the last if block in this constructor.
// Reflectively create a new normal instance.
if (i instanceof ArgWrapper) {
ArgWrapper wrapper = (ArgWrapper) i;
x = (X) wrapper.x;
extending = wrapper.extending;
formals = wrapper.formals;
return;
}
extending = extendingConstructor();
formals = extending.getParameterTypes();
// must not be empty
if (!i.hasNext()) {
throw badArgs();
}
X arg = i.next();
// single value -- we can handle this
if (!i.hasNext()) {
x = arg;
args.add(arg);
return;
}
// Multiple values
// This is the constructor our subclass will use.
for (; i.hasNext(); arg = i.next()) {
args.add(arg);
if (i.hasNext()) {
newInstanceWithArg(this,arg);
}
}
args.add(arg);
x = arg;
}
/**
* Override this constructor for looping through individually specified values.
*/
protected To(Enumeration<X> e) {
type = Type.ENUMERATION;
if (e==null) {
throw badArgs();
}
// ArgWrapper
// This was created by the last if block in this constructor.
// Reflectively create a new normal instance.
if (e instanceof ArgWrapper) {
ArgWrapper wrapper = (ArgWrapper) e;
x = (X) wrapper.x;
extending = wrapper.extending;
formals = wrapper.formals;
return;
}
extending = extendingConstructor();
formals = extending.getParameterTypes();
// must not be empty
if (!e.hasMoreElements()) {
throw badArgs();
}
X arg = e.nextElement();
// single value -- we can handle this
if (!e.hasMoreElements()) {
x = arg;
args.add(arg);
return;
}
// Multiple values
// This is the constructor our subclass will use.
for (; e.hasMoreElements(); arg = e.nextElement()) {
args.add(arg);
if (e.hasMoreElements()) {
newInstanceWithArg(this,arg);
}
}
args.add(arg);
x = arg;
}
private static IllegalArgumentException badArgs(Object... xs) {
return new IllegalArgumentException("" + xs);
}
/**
* Use the same constructor that is invoking us, to create a new instance.
* We do this in order to run whatever code is in the subclass instance
* initializer again. It can refer to x, which will have the given value.
*/
private void newInstanceWithArg(To to,Object x) {
try {
newInstanceWithArgWrapper0(to,x);
} catch (IllegalArgumentException e) {
String message = x + " not valid for constructor " + extending + " taking "+ Arrays.asList(formals);
throw new IllegalArgumentException(message,e);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
setChanged();
notifyObservers(x);
}
}
private To newInstanceWithArgWrapper0(To to,Object x)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
{
Field[] declaredFields = to.getClass().getDeclaredFields();
Object[] params = new Object[formals.length];
boolean hasThis = declaredFields.length==1;
boolean varArgs = extending.isVarArgs();
ArgWrapper wrapped = new ArgWrapper(x,extending,formals);
Object arg = varArgs ? new Object[] {wrapped} : wrapped;
if (hasThis) {
params[0] = declaredFields[0].get(to);
params[1] = arg;
} else {
params[0] = arg;
}
return (To) extending.newInstance(params);
}
/**
* Return the constructor of the class that is extending this one.
*/
private static Constructor<?> extendingConstructor() {
try {
return extendingConstructor0();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private static Constructor<?> extendingConstructor0() throws ClassNotFoundException {
Class<? extends To> c = (Class<To>) Class.forName(extendingClass());
Constructor<?> con = c.getDeclaredConstructors()[0];
return con;
}
/**
* Return the (usually anonymous) class that extends this one.
*/
private static String extendingClass() {
for (StackTraceElement e : Thread.currentThread().getStackTrace()) {
String c = e.getClassName();
if (isExtendingClass(c)) {
return c;
}
}
throw new IllegalStateException();
}
private static boolean isExtendingClass(String c) {
return c !=null &&
! c.equals(To.class.getName()) &&
! c.equals(Thread.class.getName());
}
/*
* The next set of methods are for Iteration and Enumeration.
*/
public boolean hasMoreElements() { return hasNext(); }
public X nextElement() { return next(); }
public boolean hasNext() {
if (iterator==null) {
iterator();
}
return iterator.hasNext();
}
final public void remove() { iterator.remove(); }
public X next() {
X x = (X) iterator.next();
newInstanceWithArg(this,x);
this.notifyObservers(x);
return x;
}
/**
* Run this To loop.
*/
public void run() {
for (Object x : args) {
newInstanceWithArg(this,x);
}
}
/**
* Call this To loop;
*/
public Object call() {
run();
return null;
}
/**
* Return an iterator that can be used to iterate through this loop.
*/
public Iterator<X> iterator() {
iterator = args.iterator();
return this;
}
// the next several methods are from Collection
public int size() {
return args.size();
}
public boolean isEmpty() {
return args.isEmpty();
}
public boolean contains(Object o) {
return args.contains(o);
}
public Object[] toArray() {
return args.toArray();
}
public Object[] toArray(Object[] a) {
return args.toArray(a);
}
public boolean add(X e) {
boolean flag = args.add(e);
notifyObservers(e);
return flag;
}
public boolean remove(Object o) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean containsAll(Collection c) {
return args.containsAll(c);
}
public boolean addAll(Collection<? extends X> c) {
boolean flag = args.addAll(c);
for (Object o : c) {
notifyObservers(o);
}
return flag;
}
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException("Not supported yet.");
}
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void clear() {
throw new UnsupportedOperationException("Not supported yet.");
}
public void update(Observable o, Object arg) {
if (!hasNext()) {
iterator();
}
next();
}
}