1package com.runehive.game.world.entity;
3import com.runehive.game.world.entity.mob.Mob;
4import com.runehive.util.RandomUtils;
7import java.util.function.Consumer;
8import java.util.function.Predicate;
9import java.util.stream.Collectors;
10import java.util.stream.IntStream;
11import java.util.stream.Stream;
12import java.util.stream.StreamSupport;
14import static com.google.common.base.Preconditions.*;
26public final class MobList<E
extends Mob> implements Iterable<E> {
65 checkPositionIndex(
curr,
list.capacity(),
"No elements left");
72 public void remove() {
73 checkState(
prev != -1,
"remove() can only be called once after each call to next()");
113 @SuppressWarnings(
"unchecked")
116 Stream<Integer> indexStream = IntStream.rangeClosed(1,
capacity).boxed();
117 indices =
new TreeSet<>(indexStream.collect(Collectors.toList()));
132 int first = index - 1;
134 if (first == second)
continue;
141 entities[first].setListIndex(first);
145 entities[second].setListIndex(second);
165 if (e ==
null)
continue;
167 if (++index >=
size)
return;
178 public Optional<E>
findFirst(Predicate<? super E> filter) {
180 if (filter.test(e)) {
181 return Optional.of(e);
184 return Optional.empty();
194 public Optional<E>
findLast(Predicate<? super E> filter) {
195 for (
int index =
capacity(); index > 1; index--) {
200 if (filter.test(
entity)) {
201 return Optional.of(
entity);
204 return Optional.empty();
213 public List<E>
findAll(Predicate<? super E> filter) {
214 List<E> list =
new ArrayList<>();
216 if (filter.test(e)) {
231 return Spliterators.spliterator(
entities, Spliterator.ORDERED | Spliterator.DISTINCT);
241 Integer index =
indices.pollFirst();
243 if (index ==
null || index ==
capacity()) {
253 entity.setListIndex(index);
265 checkArgument(
entity.getListIndex() != -1,
"index == -1");
279 public void remove(
int index) {
289 public E
get(
int index) {
303 checkArgument(
entity !=
null,
"index -> null EntityNode");
375 return StreamSupport.stream(
spliterator(),
false).filter(Objects::nonNull);
383 Spliterator<E> split = Spliterators.spliterator(
entities,
spliterator().characteristics() | Spliterator.IMMUTABLE);
384 return StreamSupport.stream(split,
true).filter(Objects::nonNull);
An Iterator implementation designed specifically MobLists.
final MobList< E > list
The MobList this Iterator is dedicated to.
boolean skipNullIndexes()
Forwards the curr marker until a non-null element is found.
EntityListIterator(MobList< E > list)
Creates a new EntityListIterator.
int curr
The current index.
int prev
The previous index.
Optional< E > findFirst(Predicate<? super E > filter)
Finds the first element that matches filter.
MobList(int capacity)
Creates a new MobList.
int size
The internal size of this list.
List< E > findAll(Predicate<? super E > filter)
Finds all elements that match filter.
final E[] entities
The entities contained within this list.
final TreeSet< Integer > indices
A queue that acts as a fs for indices.
void shuffle()
Shuffles this collections list of entities.
Spliterator< E > spliterator()
E[] toArray()
Please note that this function does not give direct access to the backing array but instead creates a...
Stream< E > parallelStream()
boolean add(E entity)
Adds entity to this list.
int largestIndex
The largest used index.
boolean contains(E entity)
Determines if this list contains entity.
Optional< E > findLast(Predicate<? super E > filter)
Finds the last element that matches filter.
void clear()
Calls withdraw() on every single Entity in this list.
void forEach(Consumer<? super E > action)
E element(int index)
Retrieves the element on index, the only difference between this and get(int) is that this method thr...
A static-util class that provides additional functionality for generating pseudo-random numbers.
static int inclusive(int min, int max)
Returns a pseudo-random int value between inclusive min and inclusive max.