|
1 | 1 | package net.servicestack.client; |
2 | 2 |
|
| 3 | +import com.android.internal.util.Predicate; |
| 4 | + |
3 | 5 | import java.util.ArrayList; |
4 | | -import java.util.List; |
| 6 | +import java.util.Collections; |
5 | 7 |
|
6 | 8 | public class Func { |
7 | 9 | public static interface Function<T,R> { |
8 | 10 | public R apply(T t); |
9 | 11 | } |
10 | 12 |
|
11 | | - public static <T,R> ArrayList<R> map(List<T> xs, Function<T,R> f) { |
| 13 | + public static interface Reducer<T,E> { |
| 14 | + public E reduce(E prev, T item); |
| 15 | + } |
| 16 | + |
| 17 | + public static <T,R> ArrayList<R> map(Iterable<T> xs, Function<T,R> f) { |
12 | 18 | ArrayList<R> to = new ArrayList<>(); |
13 | 19 | for (T x : xs) { |
14 | 20 | R ret = f.apply(x); |
15 | 21 | to.add(ret); |
16 | 22 | } |
17 | 23 | return to; |
18 | 24 | } |
| 25 | + |
| 26 | + public static <T> ArrayList<T> toList(Iterable<T> xs){ |
| 27 | + ArrayList<T> to = new ArrayList<>(); |
| 28 | + for (T x : xs) { |
| 29 | + to.add(x); |
| 30 | + } |
| 31 | + return to; |
| 32 | + } |
| 33 | + |
| 34 | + public static <T> ArrayList<T> filter(Iterable<T> xs, Predicate<T> predicate){ |
| 35 | + ArrayList<T> to = new ArrayList<>(); |
| 36 | + for (T x : xs) { |
| 37 | + if (predicate.apply(x)){ |
| 38 | + to.add(x); |
| 39 | + } |
| 40 | + } |
| 41 | + return to; |
| 42 | + } |
| 43 | + |
| 44 | + public static <T> T first(Iterable<T> xs, Predicate<T> predicate){ |
| 45 | + for (T x : xs) { |
| 46 | + if (predicate.apply(x)){ |
| 47 | + return x; |
| 48 | + } |
| 49 | + } |
| 50 | + return null; |
| 51 | + } |
| 52 | + |
| 53 | + public static <T> T last(Iterable<T> xs, Predicate<T> predicate){ |
| 54 | + for (T x : reverse(xs)) { |
| 55 | + if (predicate.apply(x)){ |
| 56 | + return x; |
| 57 | + } |
| 58 | + } |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + public static <T> ArrayList<T> skip(Iterable<T> xs, int skip){ |
| 63 | + int i = 0; |
| 64 | + ArrayList<T> to = new ArrayList<>(); |
| 65 | + for (T x : xs) { |
| 66 | + if (i++ >= skip){ |
| 67 | + to.add(x); |
| 68 | + } |
| 69 | + } |
| 70 | + return to; |
| 71 | + } |
| 72 | + |
| 73 | + public static <T> ArrayList<T> skip(Iterable<T> xs, Predicate<T> predicate){ |
| 74 | + ArrayList<T> to = new ArrayList<>(); |
| 75 | + for (T x : xs) { |
| 76 | + if (predicate.apply(x)){ |
| 77 | + to.add(x); |
| 78 | + } |
| 79 | + } |
| 80 | + return to; |
| 81 | + } |
| 82 | + |
| 83 | + public static <T> ArrayList<T> take(Iterable<T> xs, Predicate<T> predicate){ |
| 84 | + ArrayList<T> to = new ArrayList<>(); |
| 85 | + for (T x : xs) { |
| 86 | + if (predicate.apply(x)){ |
| 87 | + return to; |
| 88 | + } |
| 89 | + to.add(x); |
| 90 | + } |
| 91 | + return to; |
| 92 | + } |
| 93 | + |
| 94 | + public static <T> ArrayList<T> take(Iterable<T> xs, int take){ |
| 95 | + int i = 0; |
| 96 | + ArrayList<T> to = new ArrayList<>(); |
| 97 | + for (T x : xs) { |
| 98 | + if (i++ >= take){ |
| 99 | + return to; |
| 100 | + } |
| 101 | + to.add(x); |
| 102 | + } |
| 103 | + return to; |
| 104 | + } |
| 105 | + |
| 106 | + public static <T> boolean any(Iterable<T> xs, Predicate<T> predicate){ |
| 107 | + for (T x : xs) { |
| 108 | + if (predicate.apply(x)){ |
| 109 | + return true; |
| 110 | + } |
| 111 | + } |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + public static <T> boolean all(Iterable<T> xs, Predicate<T> predicate){ |
| 116 | + for (T x : xs) { |
| 117 | + if (!predicate.apply(x)){ |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + public static <T> ArrayList<T> expand(Iterable<T>... xss){ |
| 125 | + ArrayList<T> to = new ArrayList<>(); |
| 126 | + for (Iterable<T> xs : xss) { |
| 127 | + for (T x : xs){ |
| 128 | + to.add(x); |
| 129 | + } |
| 130 | + } |
| 131 | + return to; |
| 132 | + } |
| 133 | + |
| 134 | + public static <T> T elementAt(Iterable<T> xs, int index){ |
| 135 | + int i = 0; |
| 136 | + for (T x : xs){ |
| 137 | + if (i++ == index){ |
| 138 | + return x; |
| 139 | + } |
| 140 | + } |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + public static <T> ArrayList<T> reverse(Iterable<T> xs){ |
| 145 | + ArrayList<T> clone = toList(xs); |
| 146 | + Collections.reverse(clone); |
| 147 | + return clone; |
| 148 | + } |
| 149 | + |
| 150 | + public static <T,E> E reduce(Iterable<T> xs, E initialValue, Reducer<T,E> reducer){ |
| 151 | + E currentValue = initialValue; |
| 152 | + for (T x : xs){ |
| 153 | + currentValue = reducer.reduce(currentValue, x); |
| 154 | + } |
| 155 | + return currentValue; |
| 156 | + } |
| 157 | + |
| 158 | + public static <T,E> E reduceRight(Iterable<T> xs, E initialValue, Reducer<T,E> reducer){ |
| 159 | + return reduce(reverse(xs), initialValue, reducer); |
| 160 | + } |
| 161 | + |
| 162 | + public static <T> String join(Iterable<T> xs, String separator){ |
| 163 | + StringBuilder sb = new StringBuilder(); |
| 164 | + for (T x : xs){ |
| 165 | + if (sb.length() > 0) |
| 166 | + sb.append(separator); |
| 167 | + sb.append(x); |
| 168 | + } |
| 169 | + return sb.toString(); |
| 170 | + } |
19 | 171 | } |
0 commit comments