Skip to content

Commit e0759a8

Browse files
committed
Update Iterables.
1 parent 3a0a4dc commit e0759a8

3 files changed

Lines changed: 45 additions & 25 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,8 @@ public static <T, K extends Comparable<? super K>> Iterable<Map.Entry<K, List<T>
11401140

11411141
public static <T> boolean exists(Iterable<T> iterable, Predicate<? super T> predicate) { ... }
11421142

1143+
public static <T, U> Predicate<T> where(Function<? super T, U> transform, Predicate<U> condition) { ... }
1144+
11431145
public static <T, U extends Comparable<? super U>> Predicate<T> whereEqualTo(Function<? super T, U> transform, U value) { ... }
11441146
public static <T, U extends Comparable<? super U>> Predicate<T> whereNotEqualTo(Function<? super T, U> transform, U value) { ... }
11451147
public static <T, U extends Comparable<? super U>> Predicate<T> whereGreaterThan(Function<? super T, U> transform, U value) { ... }

kilo-client/src/main/java/org/httprpc/kilo/util/Iterables.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,40 @@ public static <T> boolean exists(Iterable<T> iterable, Predicate<? super T> pred
221221
return firstOf(filter(iterable, predicate)) != null;
222222
}
223223

224+
/**
225+
* Creates a "where" predicate.
226+
*
227+
* @param <T>
228+
* The element type.
229+
*
230+
* @param <U>
231+
* The value type.
232+
*
233+
* @param transform
234+
* The transform function.
235+
*
236+
* @param condition
237+
* The condition to test.
238+
*
239+
* @return
240+
* The comparison predicate.
241+
*/
242+
public static <T, U> Predicate<T> where(Function<? super T, U> transform, Predicate<U> condition) {
243+
if (transform == null || condition == null) {
244+
throw new IllegalArgumentException();
245+
}
246+
247+
return element -> {
248+
var result = transform.apply(element);
249+
250+
if (result != null) {
251+
return condition.test(result);
252+
} else {
253+
return false;
254+
}
255+
};
256+
}
257+
224258
/**
225259
* Creates a "where equal to" predicate.
226260
*
@@ -385,22 +419,6 @@ public static <T> Predicate<T> whereFalse(Function<? super T, Boolean> transform
385419
return where(transform, result -> !result);
386420
}
387421

388-
private static <T, U> Predicate<T> where(Function<? super T, U> transform, Predicate<U> condition) {
389-
if (transform == null) {
390-
throw new IllegalArgumentException();
391-
}
392-
393-
return element -> {
394-
var result = transform.apply(element);
395-
396-
if (result != null) {
397-
return condition.test(result);
398-
} else {
399-
return false;
400-
}
401-
};
402-
}
403-
404422
/**
405423
* Calculates a sum.
406424
*

kilo-client/src/test/java/org/httprpc/kilo/util/IterablesTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ public void testExists() {
119119
assertFalse(exists(values, value -> value > 3));
120120
}
121121

122+
@Test
123+
public void testWhere() {
124+
List<Supplier<String>> values = listOf(() -> "a", () -> "b", () -> "c", () -> null);
125+
126+
var result = countOf(filter(values, where(Supplier::get, value -> value.equalsIgnoreCase("A"))));
127+
128+
assertEquals(1, result);
129+
}
130+
122131
@Test
123132
public void testWhereEqualTo() {
124133
var values = listOf("a", "ab", "abc");
@@ -191,15 +200,6 @@ public void testWhereFalse() {
191200
assertEquals(2, result);
192201
}
193202

194-
@Test
195-
public void testOptionalPredicate() {
196-
List<Supplier<Boolean>> values = listOf(() -> true, () -> false, () -> false, () -> null);
197-
198-
var result = countOf(filter(values, whereFalse(Supplier::get)));
199-
200-
assertEquals(2, result);
201-
}
202-
203203
@Test
204204
public void testIsNull() {
205205
var values = listOf(1, null, 2, null, 3);

0 commit comments

Comments
 (0)