Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static <T> Function<Supplier<T>, Optional<T>> firstNonNullMapper() {
- Map<Integer, Optional<T>> firstValueBox = new ConcurrentHashMap<>();
- firstValueBox.put(1, Optional.empty());
- Set<Supplier<T>> footPrints = Collections.synchronizedSet(new HashSet<>());
- return supplier -> {
- firstValueBox.merge(
- 1,
- Optional.empty(),
- (a, b) ->
- Stream.of(a, b)
- .filter(Optional::isPresent)
- .findFirst()
- .orElseGet(() -> {
- Optional<T> t = Optional.ofNullable(supplier.get());
- t.ifPresent(unused -> footPrints.add(supplier));
- return t;
- })
- );
- return footPrints.contains(supplier) ? firstValueBox.get(1) : Optional.empty();
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement