-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potion effect removing on death (#1374)
- Loading branch information
Showing
2 changed files
with
67 additions
and
9 deletions.
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
58 changes: 58 additions & 0 deletions
58
arclight-common/src/main/java/io/izzel/arclight/common/util/IteratorUtil.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,58 @@ | ||
package io.izzel.arclight.common.util; | ||
|
||
import java.util.Iterator; | ||
import java.util.function.Predicate; | ||
|
||
public class IteratorUtil { | ||
|
||
public static <T> Iterator<T> filter(Iterator<T> iterator, Predicate<T> predicate) { | ||
return new FilterIterator<>(iterator, predicate); | ||
} | ||
|
||
private static class FilterIterator<T> implements Iterator<T> { | ||
|
||
private final Iterator<T> iterator; | ||
private final Predicate<T> predicate; | ||
|
||
private boolean hasNext = false; | ||
private T next; | ||
|
||
private FilterIterator(Iterator<T> iterator, Predicate<T> predicate) { | ||
this.iterator = iterator; | ||
this.predicate = predicate; | ||
this.computeNext(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return hasNext; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
try { | ||
return this.next; | ||
} finally { | ||
computeNext(); | ||
} | ||
} | ||
|
||
private void computeNext() { | ||
while (iterator.hasNext()) { | ||
T next = iterator.next(); | ||
if (predicate.test(next)) { | ||
hasNext = true; | ||
this.next = next; | ||
return; | ||
} | ||
} | ||
hasNext = false; | ||
this.next = null; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
iterator.remove(); | ||
} | ||
} | ||
} |