-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
JavaStreamUtilities
with a predicate to test if a value is th…
- Loading branch information
Showing
3 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/de/julielab/java/utilities/JavaStreamUtilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.julielab.java.utilities; | ||
|
||
import java.util.function.Function; | ||
import java.util.function.Predicate; | ||
|
||
public class JavaStreamUtilities { | ||
/** | ||
* A predicate that tests true for all values that equal the first value it saw. | ||
* | ||
* @param valueFunction A function to retrieve the value. | ||
* @param <T> The input type. | ||
* @param <E> The value type for comparison. | ||
* @return The predicate. | ||
*/ | ||
public static <T, E> Predicate equalsFirstSeenValue(Function<T, E> valueFunction) { | ||
return new Predicate<T>() { | ||
private E firstValue = null; | ||
|
||
@Override | ||
public boolean test(T entry) { | ||
E value = valueFunction.apply(entry); | ||
if (firstValue == null) { | ||
firstValue = value; | ||
return true; | ||
} | ||
return value.equals(firstValue); | ||
} | ||
}; | ||
} | ||
} |
Binary file not shown.