Blame view

src/Enumerable.java 1.87 KB
1b1e928cc   glaville   initial import of...
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
  import java.util.ArrayList;
  import java.util.Collection;
  
  public class Enumerable {
  	
  	public static <T, U> Collection<U> map(Collection<T> c, Filter<T, U> f) {
  		ArrayList<U> result = new ArrayList<U>();
  		
  		for (T e : c) {
  			result.add(f.apply(e));
  		}
  		
  		return result;
  	}
  	
  	public static <T, U> Collection<U> map(T[] c, Filter<T, U> f) {
  		ArrayList<U> result = new ArrayList<U>();
  		
  		for (T e : c) {
  			result.add(f.apply(e));
  		}
  		
  		return result;
  	}
  	
  	public static <T> Collection<T> select(Collection<T> c, Predicate<T> p) {
  		ArrayList<T> result = new ArrayList<T>();
  		
  		for (T e : c) {
  			if (p.apply(e)) {
  				result.add(e);
  			}
  		}
  		
  		return result;
  	}
  	
  	public static <T> Collection<T> select(T[] c, Predicate<T> p) {
  		ArrayList<T> result = new ArrayList<T>();
  		
  		for (T e : c) {
  			if (p.apply(e)) {
  				result.add(e);
  			}
  		}
  		
  		return result;
  	}
  	
  	public static <T> Collection<T> reject(Collection<T> c, Predicate<T> p) {
  		ArrayList<T> result = new ArrayList<T>();
  		
  		for (T e : c) {
  			if (! p.apply(e)) {
  				result.add(e);
  			}
  		}
  		
  		return result;
  	}
  	
  	public static <T> Collection<T> reject(T[] c, Predicate<T> p) {
  		ArrayList<T> result = new ArrayList<T>();
  		
  		for (T e : c) {
  			if (! p.apply(e)) {
  				result.add(e);
  			}
  		}
  		
  		return result;
  	}
  	
  	public static <T> boolean all(Collection<T> c, Predicate<T> p) {
  		for (T e : c) {
  			if (! p.apply(e)) {
  				return false;
  			}
  		}
  		
  		return true;
  	}
  	
  	public static <T> boolean all(T[] c, Predicate<T> p) {
  		for (T e : c) {
  			if (! p.apply(e)) {
  				return false;
  			}
  		}
  		
  		return true;
  	}
  	
  	public static <T> boolean any(Collection<T> c, Predicate<T> p) {
  		for (T e : c) {
  			if (p.apply(e)) {
  				return true;
  			}
  		}
  		
  		return false;
  	}
  	
  	public static <T> boolean any(T[] c, Predicate<T> p) {
  		for (T e : c) {
  			if (p.apply(e)) {
  				return true;
  			}
  		}
  		
  		return false;
  	}
  
  }