Repository navigation
Expand file tree
/
Copy pathApplyTest.java
More file actions
93 lines (74 loc) · 2.14 KB
/
Copy pathApplyTest.java
File metadata and controls
93 lines (74 loc) · 2.14 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
package btp.oneP;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class ApplyTest {
public static void main(String[] args) throws NoSuchMethodException, SecurityException {
List<Shape> shapes = new ArrayList<Shape>();
for(int i=0;i<10;i++){
shapes.add(new Shape());
}
Apply.apply(shapes, Shape.class.getMethod("rotate"));
Apply.apply(shapes, Shape.class.getMethod("resize", int.class), 5);
List<Square> squares = new ArrayList<Square>();
for(int i=0;i<10;i++){
squares.add(new Square());
}
Apply.apply(squares, Square.class.getMethod("rotate"));
Apply.apply(squares, Square.class.getMethod("resize", int.class), 5);
Apply.apply(new FilledList<Shape>(Shape.class,5), Shape.class.getMethod("rotate"));
Apply.apply(new FilledList<Shape>(Square.class,18), Shape.class.getMethod("resize", int.class),15);
SimpleQueue<Shape> shapeQ = new SimpleQueue<Shape>();
for(int i=0;i<5;i++){
shapeQ.add(new Shape());
shapeQ.add(new Square());
}
Apply.apply(shapeQ, ShapeX.class.getMethod("rotate"));
}
}
class Apply{
public static <T,S extends Iterable<? extends T>> void apply(S seq,Method f,Object...args){
try{
for(T t:seq){
f.invoke(t, args);
}
}catch(Exception e){
throw new RuntimeException();
}
}
}
class Shape{
public void rotate(){
System.out.println(this+" rotate");
}
public void resize(int newSize){
System.out.println(this+" resize"+newSize);
}
}
class Square extends Shape{}
class FilledList<T> extends ArrayList<T>{
public FilledList(Class<? extends T> type,int size){
try{
for(int i = 0;i<size;i++){
this.add(type.newInstance());
}
}catch(Exception e){
throw new RuntimeException();
}
}
}
class SimpleQueue<T> implements Iterable<T>{
private LinkedList<T> storage = new LinkedList<T>();
public void add(T t){
this.storage.offer(t);
}
public T get(){
return this.storage.poll();
}
@Override
public Iterator<T> iterator() {
return this.storage.iterator();
}
}