Skip to content

Commit 9f488df

Browse files
committed
Add new Func utils + treat nulls as empty collections
1 parent 2b5cfce commit 9f488df

File tree

1 file changed

+86
-0
lines changed
  • src/AndroidClient/client/src/main/java/net/servicestack/client

1 file changed

+86
-0
lines changed

src/AndroidClient/client/src/main/java/net/servicestack/client/Func.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import java.util.Iterator;
88

99
public class Func {
10+
public static interface Each<T> {
11+
public void apply(T t);
12+
}
13+
1014
public static interface Function<T,R> {
1115
public R apply(T t);
1216
}
@@ -17,15 +21,42 @@ public static interface Reducer<T,E> {
1721

1822
public static <T,R> ArrayList<R> map(Iterable<T> xs, Function<T,R> f) {
1923
ArrayList<R> to = new ArrayList<>();
24+
if (xs == null) return to;
25+
2026
for (T x : xs) {
2127
R ret = f.apply(x);
2228
to.add(ret);
2329
}
2430
return to;
2531
}
2632

33+
public static <T> void each(Iterable<T> xs, Each<T> f) {
34+
if (xs == null) return;
35+
for (T x : xs) {
36+
f.apply(x);
37+
}
38+
}
39+
40+
public static ArrayList<Integer> toList(int... xs){
41+
ArrayList<Integer> to = new ArrayList<>();
42+
for (int x : xs) {
43+
to.add(x);
44+
}
45+
return to;
46+
}
47+
48+
public static <T> ArrayList<T> toList(T... xs){
49+
ArrayList<T> to = new ArrayList<>();
50+
for (T x : xs) {
51+
to.add(x);
52+
}
53+
return to;
54+
}
55+
2756
public static <T> ArrayList<T> toList(Iterable<T> xs){
2857
ArrayList<T> to = new ArrayList<>();
58+
if (xs == null) return to;
59+
2960
for (T x : xs) {
3061
to.add(x);
3162
}
@@ -34,6 +65,8 @@ public static <T> ArrayList<T> toList(Iterable<T> xs){
3465

3566
public static <T> ArrayList<T> filter(Iterable<T> xs, Predicate<T> predicate){
3667
ArrayList<T> to = new ArrayList<>();
68+
if (xs == null) return to;
69+
3770
for (T x : xs) {
3871
if (predicate.apply(x)){
3972
to.add(x);
@@ -43,6 +76,8 @@ public static <T> ArrayList<T> filter(Iterable<T> xs, Predicate<T> predicate){
4376
}
4477

4578
public static <T> T first(Iterable<T> xs, Predicate<T> predicate){
79+
if (xs == null) return null;
80+
4681
for (T x : xs) {
4782
if (predicate.apply(x)){
4883
return x;
@@ -51,7 +86,22 @@ public static <T> T first(Iterable<T> xs, Predicate<T> predicate){
5186
return null;
5287
}
5388

89+
public static <T> T first(Iterable<T> xs){
90+
if (xs == null) return null;
91+
92+
for (T x : xs) {
93+
return x;
94+
}
95+
return null;
96+
}
97+
98+
public static <T> T first(T[] xs) {
99+
return xs == null || xs.length == 0 ? null : xs[0];
100+
}
101+
54102
public static <T> T last(Iterable<T> xs, Predicate<T> predicate){
103+
if (xs == null) return null;
104+
55105
for (T x : reverse(xs)) {
56106
if (predicate.apply(x)){
57107
return x;
@@ -60,13 +110,29 @@ public static <T> T last(Iterable<T> xs, Predicate<T> predicate){
60110
return null;
61111
}
62112

113+
public static <T> T last(Iterable<T> xs){
114+
if (xs == null) return null;
115+
116+
T last = null;
117+
for (T x : xs) {
118+
last = x;
119+
}
120+
return last;
121+
}
122+
123+
public static <T> T last(T[] xs) {
124+
return xs == null ? null : xs[xs.length - 1];
125+
}
126+
63127
public static <T> boolean contains(Iterable<T> xs, Predicate<T> predicate){
64128
return first(xs, predicate) != null;
65129
}
66130

67131
public static <T> ArrayList<T> skip(Iterable<T> xs, int skip){
68132
int i = 0;
69133
ArrayList<T> to = new ArrayList<>();
134+
if (xs == null) return to;
135+
70136
for (T x : xs) {
71137
if (i++ >= skip){
72138
to.add(x);
@@ -77,6 +143,8 @@ public static <T> ArrayList<T> skip(Iterable<T> xs, int skip){
77143

78144
public static <T> ArrayList<T> skip(Iterable<T> xs, Predicate<T> predicate){
79145
ArrayList<T> to = new ArrayList<>();
146+
if (xs == null) return to;
147+
80148
for (T x : xs) {
81149
if (predicate.apply(x)){
82150
to.add(x);
@@ -87,6 +155,8 @@ public static <T> ArrayList<T> skip(Iterable<T> xs, Predicate<T> predicate){
87155

88156
public static <T> ArrayList<T> take(Iterable<T> xs, Predicate<T> predicate){
89157
ArrayList<T> to = new ArrayList<>();
158+
if (xs == null) return to;
159+
90160
for (T x : xs) {
91161
if (predicate.apply(x)){
92162
return to;
@@ -99,6 +169,8 @@ public static <T> ArrayList<T> take(Iterable<T> xs, Predicate<T> predicate){
99169
public static <T> ArrayList<T> take(Iterable<T> xs, int take){
100170
int i = 0;
101171
ArrayList<T> to = new ArrayList<>();
172+
if (xs == null) return to;
173+
102174
for (T x : xs) {
103175
if (i++ >= take){
104176
return to;
@@ -109,6 +181,8 @@ public static <T> ArrayList<T> take(Iterable<T> xs, int take){
109181
}
110182

111183
public static <T> boolean any(Iterable<T> xs, Predicate<T> predicate){
184+
if (xs == null) return false;
185+
112186
for (T x : xs) {
113187
if (predicate.apply(x)){
114188
return true;
@@ -118,6 +192,8 @@ public static <T> boolean any(Iterable<T> xs, Predicate<T> predicate){
118192
}
119193

120194
public static <T> boolean all(Iterable<T> xs, Predicate<T> predicate){
195+
if (xs == null) return false;
196+
121197
for (T x : xs) {
122198
if (!predicate.apply(x)){
123199
return false;
@@ -128,6 +204,8 @@ public static <T> boolean all(Iterable<T> xs, Predicate<T> predicate){
128204

129205
public static <T> ArrayList<T> expand(Iterable<T>... xss){
130206
ArrayList<T> to = new ArrayList<>();
207+
if (xss == null) return to;
208+
131209
for (Iterable<T> xs : xss) {
132210
for (T x : xs){
133211
to.add(x);
@@ -137,6 +215,8 @@ public static <T> ArrayList<T> expand(Iterable<T>... xss){
137215
}
138216

139217
public static <T> T elementAt(Iterable<T> xs, int index){
218+
if (xs == null) return null;
219+
140220
int i = 0;
141221
for (T x : xs){
142222
if (i++ == index){
@@ -147,12 +227,16 @@ public static <T> T elementAt(Iterable<T> xs, int index){
147227
}
148228

149229
public static <T> ArrayList<T> reverse(Iterable<T> xs){
230+
if (xs == null) return new ArrayList<T>();
231+
150232
ArrayList<T> clone = toList(xs);
151233
Collections.reverse(clone);
152234
return clone;
153235
}
154236

155237
public static <T,E> E reduce(Iterable<T> xs, E initialValue, Reducer<T,E> reducer){
238+
if (xs == null) return initialValue;
239+
156240
E currentValue = initialValue;
157241
for (T x : xs){
158242
currentValue = reducer.reduce(currentValue, x);
@@ -166,6 +250,8 @@ public static <T,E> E reduceRight(Iterable<T> xs, E initialValue, Reducer<T,E> r
166250

167251
public static <T> String join(Iterable<T> xs, String separator){
168252
StringBuilder sb = new StringBuilder();
253+
if (xs == null) return sb.toString();
254+
169255
for (T x : xs){
170256
if (sb.length() > 0)
171257
sb.append(separator);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy