Update Sector and SectorRepository to take advantage of Java 8.

This commit is contained in:
Major-
2014-07-03 01:36:53 +01:00
parent 9d8e7cda0a
commit 446caaaf2a
2 changed files with 16 additions and 21 deletions
+12 -18
View File
@@ -4,13 +4,14 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import org.apollo.game.model.Position;
import org.apollo.game.model.entity.Entity; import org.apollo.game.model.entity.Entity;
import org.apollo.game.model.entity.Entity.EntityType; import org.apollo.game.model.entity.Entity.EntityType;
import org.apollo.game.model.Position;
/** /**
* Represents an 8x8 area of the map. * An 8x8 area of the map.
* *
* @author Major * @author Major
*/ */
@@ -57,7 +58,8 @@ public final class Sector {
/** /**
* Adds a {@link Entity} from to sector. Note that this does not spawn the entity, or do any other action other than * Adds a {@link Entity} from to sector. Note that this does not spawn the entity, or do any other action other than
* register it to this sector. * register it to this sector. The return value of this method should also be monitored - if this method fails (i.e.
* returns {@code false}), the entity should <strong>not</strong> be registered in the world.
* *
* @param entity The entity. * @param entity The entity.
* @return {@code true} if the entity was added successfully, otherwise {@code false}. * @return {@code true} if the entity was added successfully, otherwise {@code false}.
@@ -84,8 +86,7 @@ public final class Sector {
* @return {@code true} if this sector contains the entity, otherwise {@code false}. * @return {@code true} if this sector contains the entity, otherwise {@code false}.
*/ */
public boolean contains(Entity entity) { public boolean contains(Entity entity) {
List<Entity> entities = this.entities.get(entity.getPosition()); return entities.get(entity.getPosition()).contains(entity);
return entities.contains(entity);
} }
/** /**
@@ -116,14 +117,8 @@ public final class Sector {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Entity> List<T> getEntities(Position position, EntityType type) { public <T extends Entity> List<T> getEntities(Position position, EntityType type) {
List<Entity> entities = getEntities(position); return (List<T>) entities.get(position).stream().filter((e) -> e.getEntityType() == type)
List<T> filtered = new ArrayList<>(); .collect(Collectors.toList());
for (Entity entity : entities) {
if (entity.getEntityType() == type) {
filtered.add((T) entity);
}
}
return filtered;
} }
/** /**
@@ -132,9 +127,7 @@ public final class Sector {
* @param entity The entity that was updated. * @param entity The entity that was updated.
*/ */
public void notifyListeners(Entity entity) { public void notifyListeners(Entity entity) {
for (SectorListener listener : listeners) { listeners.forEach((l) -> l.execute(this, entity));
listener.execute(this, entity);
}
} }
/** /**
@@ -144,9 +137,10 @@ public final class Sector {
* @return {@code true} if the entity was removed, otherwise {@code false}. * @return {@code true} if the entity was removed, otherwise {@code false}.
*/ */
public boolean removeEntity(Entity entity) { public boolean removeEntity(Entity entity) {
List<Entity> entities = this.entities.get(entity.getPosition()); Position position = entity.getPosition();
List<Entity> entities = this.entities.get(position);
if (entities == null) { if (entities == null) {
this.entities.put(entity.getPosition(), new ArrayList<Entity>()); this.entities.put(position, new ArrayList<>());
return false; return false;
} }
@@ -19,7 +19,7 @@ public final class SectorRepository {
private final boolean permitRemoval; private final boolean permitRemoval;
/** /**
* A {@link Map} of {@link SectorCoordinates} that correspond to the appropriate {@link Sector}s.. * A {@link Map} of {@link SectorCoordinates} that correspond to the appropriate {@link Sector}s.
*/ */
private final Map<SectorCoordinates, Sector> sectors = new HashMap<>(); private final Map<SectorCoordinates, Sector> sectors = new HashMap<>();
@@ -81,7 +81,8 @@ public final class SectorRepository {
public Sector get(SectorCoordinates coordinates) { public Sector get(SectorCoordinates coordinates) {
Sector sector = sectors.get(coordinates); Sector sector = sectors.get(coordinates);
if (sector == null) { if (sector == null) {
add(sector = new Sector(coordinates)); sector = new Sector(coordinates);
add(sector);
} }
return sector; return sector;
} }