-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapterMethodIdiom.java
More file actions
49 lines (42 loc) · 1.02 KB
/
AdapterMethodIdiom.java
File metadata and controls
49 lines (42 loc) · 1.02 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
package chapter11;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
class ReversibleArrayList<T> extends ArrayList<T>{
public ReversibleArrayList(Collection<T> c){
super(c);
}
public Iterable<T> reversed(){
return new Iterable<T>() {
public Iterator<T> iterator(){
return new Iterator<T>() {
int current = size() - 1;
public boolean hasNext(){
return current > -1;
}
public T next(){
return get(current--);
}
public void remove(){
throw new UnsupportedOperationException();
}
};
}
};
}
}
public class AdapterMethodIdiom {
public static void main(String[] args){
ReversibleArrayList<String> ral =
new ReversibleArrayList<String>(
Arrays.asList("To be or not to be".split(" ")));
for(String s:ral){
System.out.print(s+" ");
}
System.out.println();
for(String s: ral.reversed()){
System.out.print(s+" ");
}
}
}