mirror of
https://github.com/BentoBoxWorld/Level.git
synced 2024-12-27 11:37:39 +01:00
Use Java 9's takeWhile
This commit is contained in:
parent
383ede3d59
commit
4661bcd109
@ -1,38 +0,0 @@
|
|||||||
package world.bentobox.level;
|
|
||||||
|
|
||||||
import java.util.Spliterator;
|
|
||||||
import java.util.Spliterators;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Java 8 version of Java 9's forWhile
|
|
||||||
* https://www.baeldung.com/java-break-stream-foreach
|
|
||||||
* @author tastybento
|
|
||||||
*
|
|
||||||
* @param <T>
|
|
||||||
*/
|
|
||||||
public class CustomSpliterator<T> extends Spliterators.AbstractSpliterator<T> {
|
|
||||||
|
|
||||||
private Spliterator<T> splitr;
|
|
||||||
private Predicate<T> predicate;
|
|
||||||
private boolean isMatched = true;
|
|
||||||
|
|
||||||
public CustomSpliterator(Spliterator<T> splitr, Predicate<T> predicate) {
|
|
||||||
super(splitr.estimateSize(), 0);
|
|
||||||
this.splitr = splitr;
|
|
||||||
this.predicate = predicate;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public synchronized boolean tryAdvance(Consumer<? super T> consumer) {
|
|
||||||
boolean hadNext = splitr.tryAdvance(elem -> {
|
|
||||||
if (predicate.test(elem) && isMatched) {
|
|
||||||
consumer.accept(elem);
|
|
||||||
} else {
|
|
||||||
isMatched = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return hadNext && isMatched;
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,10 +14,8 @@ import java.util.TreeMap;
|
|||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.function.Predicate;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
import java.util.stream.StreamSupport;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
@ -444,18 +442,7 @@ public class LevelsManager {
|
|||||||
.filter(e -> addon.getIslands().isOwner(world, e.getKey()))
|
.filter(e -> addon.getIslands().isOwner(world, e.getKey()))
|
||||||
.filter(l -> l.getValue() > 0)
|
.filter(l -> l.getValue() > 0)
|
||||||
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
|
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()));
|
||||||
return takeWhile(stream, x -> !x.getKey().equals(uuid)).map(Map.Entry::getKey).collect(Collectors.toList()).size() + 1;
|
return stream.takeWhile(x -> !x.getKey().equals(uuid)).map(Map.Entry::getKey).collect(Collectors.toList()).size() + 1;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Java 8's version of Java 9's takeWhile
|
|
||||||
* @param stream
|
|
||||||
* @param predicate
|
|
||||||
* @return stream
|
|
||||||
*/
|
|
||||||
public static <T> Stream<T> takeWhile(Stream<T> stream, Predicate<T> predicate) {
|
|
||||||
CustomSpliterator<T> customSpliterator = new CustomSpliterator<>(stream.spliterator(), predicate);
|
|
||||||
return StreamSupport.stream(customSpliterator, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user