From 6c9e5089e572d774870e86be1bc09ea703d0cb91 Mon Sep 17 00:00:00 2001 From: Vizaxo Date: Tue, 27 Jun 2017 01:49:05 +0100 Subject: [PATCH] Update getEntitiesWith method to iterate over all caches Also updates the implementation of said method to be a more concise stream-based operation. --- .../entity/internal/PojoEntityCache.java | 33 +++---------- .../entity/internal/PojoEntityManager.java | 46 +------------------ 2 files changed, 8 insertions(+), 71 deletions(-) diff --git a/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityCache.java b/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityCache.java index 90d8fbe02fa..e309eb33aff 100644 --- a/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityCache.java +++ b/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityCache.java @@ -17,9 +17,6 @@ import com.google.common.collect.Lists; import com.google.common.collect.MapMaker; -import gnu.trove.iterator.TLongObjectIterator; -import gnu.trove.list.TLongList; -import gnu.trove.list.array.TLongArrayList; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.terasology.entitySystem.Component; @@ -361,30 +358,12 @@ private EntityRef createEntityRef(long entityId) { @SafeVarargs @Override public final Iterable getEntitiesWith(Class... componentClasses) { - if (componentClasses.length == 0) { - return getAllEntities(); - } - TLongList idList = new TLongArrayList(); - TLongObjectIterator primeIterator = componentStore.componentIterator(componentClasses[0]); - if (primeIterator == null) { - return Collections.emptyList(); - } - - while (primeIterator.hasNext()) { - primeIterator.advance(); - long id = primeIterator.key(); - boolean discard = false; - for (int i = 1; i < componentClasses.length; ++i) { - if (componentStore.get(id, componentClasses[i]) == null) { - discard = true; - break; - } - } - if (!discard) { - idList.add(primeIterator.key()); - } - } - return () -> new EntityIterator(idList.iterator(), this); + return () -> entityStore.keySet().stream() + //Keep entities which have all of the required components + .filter(id -> Arrays.stream(componentClasses) + .allMatch(component -> componentStore.get(id, component) != null)) + .map(id -> createEntityRef(id)) + .iterator(); } @Override diff --git a/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityManager.java b/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityManager.java index f80245f49a0..79a4ef2bfcc 100644 --- a/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityManager.java +++ b/engine/src/main/java/org/terasology/entitySystem/entity/internal/PojoEntityManager.java @@ -23,7 +23,6 @@ import com.google.common.collect.Sets; import gnu.trove.iterator.TLongObjectIterator; import gnu.trove.list.TLongList; -import gnu.trove.list.array.TLongArrayList; import gnu.trove.set.TLongSet; import gnu.trove.set.hash.TLongHashSet; import org.slf4j.Logger; @@ -257,50 +256,9 @@ public Iterable getAllEntities() { @SafeVarargs @Override - //Todo: implement iterating over multiple caches public final Iterable getEntitiesWith(Class... componentClasses) { - if (componentClasses.length == 0) { - return getAllEntities(); - } - if (componentClasses.length == 1) { - return iterateEntities(componentClasses[0]); - } - TLongList idList = new TLongArrayList(); - TLongObjectIterator primeIterator = globalCache.getComponentStore().componentIterator(componentClasses[0]); - if (primeIterator == null) { - return Collections.emptyList(); - } - - while (primeIterator.hasNext()) { - primeIterator.advance(); - long id = primeIterator.key(); - boolean discard = false; - for (int i = 1; i < componentClasses.length; ++i) { - if (globalCache.getComponentStore().get(id, componentClasses[i]) == null) { - discard = true; - break; - } - } - if (!discard) { - idList.add(primeIterator.key()); - } - } - return new EntityIterable(idList); - } - - private Iterable iterateEntities(Class componentClass) { - TLongList idList = new TLongArrayList(); - TLongObjectIterator primeIterator = globalCache.getComponentStore().componentIterator(componentClass); - if (primeIterator == null) { - return Collections.emptyList(); - } - - while (primeIterator.hasNext()) { - primeIterator.advance(); - long id = primeIterator.key(); - idList.add(primeIterator.key()); - } - return new EntityIterable(idList); + return Iterables.concat(globalCache.getEntitiesWith(componentClasses), + sectorManager.getEntitiesWith(componentClasses)); } @Override