From 4175fa6eece8e5e557710d2cf810695705bb5968 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Tue, 24 Nov 2015 15:18:13 +0100 Subject: Use Set instead of List for Animals --- src/ch/epfl/maze/physical/Daedalus.java | 46 ++++++++++++++++++++++++--------- src/ch/epfl/maze/physical/Maze.java | 16 ++++++------ src/ch/epfl/maze/physical/World.java | 16 +++++++++++- 3 files changed, 57 insertions(+), 21 deletions(-) (limited to 'src/ch') diff --git a/src/ch/epfl/maze/physical/Daedalus.java b/src/ch/epfl/maze/physical/Daedalus.java index 4670064..0179156 100644 --- a/src/ch/epfl/maze/physical/Daedalus.java +++ b/src/ch/epfl/maze/physical/Daedalus.java @@ -1,7 +1,9 @@ package ch.epfl.maze.physical; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -14,11 +16,11 @@ import java.util.stream.Stream; */ public final class Daedalus extends World { - private final List predators; - private final List predatorHistory; + private final Set predators; + private final Set predatorHistory; - private final List preys; - private final List preyHistory; + private final Set preys; + private final Set preyHistory; /** * Constructs a Daedalus with a labyrinth structure @@ -28,11 +30,11 @@ public final class Daedalus extends World { public Daedalus(int[][] labyrinth) { super(labyrinth); - this.predators = new ArrayList<>(); - this.predatorHistory = new ArrayList<>(); + this.predators = new HashSet<>(); + this.predatorHistory = new HashSet<>(); - this.preys = new ArrayList<>(); - this.preyHistory = new ArrayList<>(); + this.preys = new HashSet<>(); + this.preyHistory = new HashSet<>(); } @Override @@ -79,28 +81,48 @@ public final class Daedalus extends World { } @Override - public List getAnimals() { + public Set getAnimalSet() { return Stream .concat(this.predators.stream(), this.preys.stream()) - .collect(Collectors.toList()); + .collect(Collectors.toSet()); + } + + /** + * Returns a copy of the set of all current predators in the daedalus. + * + * @return A set of all predators in the daedalus + */ + public Set getPredatorSet() { + return new HashSet<>(this.predators); } /** * Returns a copy of the list of all current predators in the daedalus. * * @return A list of all predators in the daedalus + * @deprecated Use @code{Set getPredatorSet()} instead */ public List getPredators() { - return this.predators; + return new ArrayList<>(this.getPredatorSet()); + } + + /** + * Returns a copy of the set of all current preys in the daedalus. + * + * @return A set of all preys in the daedalus + */ + public Set getPreySet() { + return new HashSet<>(this.preys); } /** * Returns a copy of the list of all current preys in the daedalus. * * @return A list of all preys in the daedalus + * @deprecated Use @code{Set getPreySet()} instead */ public List getPreys() { - return this.preys; + return new ArrayList<>(this.getPreySet()); } /** diff --git a/src/ch/epfl/maze/physical/Maze.java b/src/ch/epfl/maze/physical/Maze.java index d3ba645..d3273c8 100644 --- a/src/ch/epfl/maze/physical/Maze.java +++ b/src/ch/epfl/maze/physical/Maze.java @@ -1,7 +1,7 @@ package ch.epfl.maze.physical; -import java.util.ArrayList; -import java.util.List; +import java.util.HashSet; +import java.util.Set; /** * Maze in which an animal starts from a starting point and must find the exit. @@ -13,8 +13,8 @@ import java.util.List; */ public final class Maze extends World { - private final List animals; - private final List animalHistory; + private final Set animals; + private final Set animalHistory; /** * Constructs a Maze with a labyrinth structure. @@ -24,8 +24,8 @@ public final class Maze extends World { public Maze(int[][] labyrinth) { super(labyrinth); - this.animals = new ArrayList<>(); - this.animalHistory = new ArrayList<>(); + this.animals = new HashSet<>(); + this.animalHistory = new HashSet<>(); } @Override @@ -34,8 +34,8 @@ public final class Maze extends World { } @Override - public List getAnimals() { - return this.animals; + public Set getAnimalSet() { + return new HashSet<>(this.animals); } /** diff --git a/src/ch/epfl/maze/physical/World.java b/src/ch/epfl/maze/physical/World.java index 7d33472..9730876 100644 --- a/src/ch/epfl/maze/physical/World.java +++ b/src/ch/epfl/maze/physical/World.java @@ -5,6 +5,7 @@ import ch.epfl.maze.util.Vector2D; import java.util.ArrayList; import java.util.List; +import java.util.Set; /** * World that is represented by a labyrinth of tiles in which an {@code Animal} @@ -69,12 +70,25 @@ public abstract class World { */ abstract public void reset(); + /** + * Returns a copy of the set of all current animals in the world. + * + * @return A set of all animals in the world + */ + public Set getAnimalSet() { + return null; + } + /** * Returns a copy of the list of all current animals in the world. * * @return A list of all animals in the world + * @implNote Not abstract for compatibility purpose (in order not to break tests) + * @deprecated Use getAnimalSet() instead */ - abstract public List getAnimals(); + public List getAnimals() { + return new ArrayList<>(this.getAnimalSet()); + } /** * Checks in a safe way the tile number at position (x, y) in the labyrinth. -- cgit v1.2.3