diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-10-30 16:29:29 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-11-05 19:31:44 +0800 |
| commit | 8d1c2c2e5e6e513715101f5eabc39dff8fb4a35c (patch) | |
| tree | ffaef48b94a496ed99c21293ce980691943f2002 /api/src/main/java/me/shedaniel | |
| parent | 9535ea4f28dc628275aa7a5b840375928648c662 (diff) | |
| download | RoughlyEnoughItems-8d1c2c2e5e6e513715101f5eabc39dff8fb4a35c.tar.gz RoughlyEnoughItems-8d1c2c2e5e6e513715101f5eabc39dff8fb4a35c.tar.bz2 RoughlyEnoughItems-8d1c2c2e5e6e513715101f5eabc39dff8fb4a35c.zip | |
Fix SearchFilteringRule
Diffstat (limited to 'api/src/main/java/me/shedaniel')
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java | 69 |
1 files changed, 67 insertions, 2 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java index c80e0dc25..67894fb27 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java @@ -23,6 +23,7 @@ package me.shedaniel.rei.api.common.util; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.common.collect.UnmodifiableIterator; @@ -361,7 +362,7 @@ public class CollectionUtils { } public static <T> Iterable<List<T>> partition(List<T> list, int size) { - return () -> new UnmodifiableIterator<List<T>>() { + return () -> new UnmodifiableIterator<>() { int i = 0; int partitionSize = Mth.ceil(list.size() / (float) size); @@ -374,7 +375,7 @@ public class CollectionUtils { public List<T> next() { int cursor = i++ * size; int realSize = Math.min(list.size() - cursor, size); - return new AbstractList<T>() { + return new AbstractList<>() { @Override public T get(int index) { if (index < 0 || index >= realSize) @@ -414,6 +415,70 @@ public class CollectionUtils { }; } + public static <T> Iterable<Iterator<T>> partitionIterator(Iterator<T> iterator, int iteratorSize, int size) { + return partitionCollection(new AbstractCollection<>() { + + @Override + public Iterator<T> iterator() { + return iterator; + } + + @Override + public int size() { + return iteratorSize; + } + }, size); + } + + public static <T> Iterable<Iterator<T>> partitionCollection(Collection<T> collection, int size) { + if (collection instanceof List) { + return Iterables.transform(partition((List<T>) collection, size), List::iterator); + } + + return () -> new Iterator<>() { + int i = 0; + int partitionSize = Mth.ceil(collection.size() / (float) size); + int advanced = 0; + Iterator<T> iterator = collection.iterator(); + + @Override + public boolean hasNext() { + return i < partitionSize; + } + + @Override + public Iterator<T> next() { + int cursor = i++ * size; + int realSize = Math.min(collection.size() - cursor, size); + + if (advanced < cursor) { + for (int j = 0; j < cursor - advanced; j++) { + if (iterator.hasNext()) { + iterator.next(); + } else { + advanced = cursor; + return Collections.emptyIterator(); + } + } + advanced = cursor; + } + + return new Iterator<>() { + @Override + public boolean hasNext() { + return iterator.hasNext() && advanced < cursor + realSize; + } + + @Override + public T next() { + advanced++; + return iterator.next(); + } + }; + } + }; + } + public static Ingredient toIngredient(ItemStack stack) { return Ingredient.of(Stream.of(stack)); } |
