From a73178cc0c313b675d370672c6bd2a5b8d18d817 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 25 Apr 2016 14:57:21 +0200 Subject: Reinforce immutability --- src/ch/epfl/xblast/Lists.java | 23 ++++++++++++++++++++++ src/ch/epfl/xblast/SubCell.java | 2 +- src/ch/epfl/xblast/server/Board.java | 5 ++--- src/ch/epfl/xblast/server/Bomb.java | 8 ++++---- src/ch/epfl/xblast/server/GameState.java | 10 +++++----- .../epfl/xblast/server/painter/BoardPainter.java | 3 ++- 6 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index 22d9d06..ef17f05 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java @@ -12,6 +12,29 @@ import java.util.stream.Stream; */ public final class Lists { + /** + * Returns an immutable copy of a list. + * + * @param l a list + * @param the element type + * @return the immutable list + */ + public static List immutableList(List l) { + return Collections.unmodifiableList(new ArrayList<>(l)); + } + + /** + * Returns an immutable copy of a map. + * + * @param m a mab + * @param the key type + * @param the value type + * @return the immutable copy + */ + public static Map immutableMap(Map m) { + return Collections.unmodifiableMap(new HashMap<>(m)); + } + /** * Returns a symmetric version of the list, without repeating the last element of the input list. * For instance, mirrored([kay]) will return [kayak]. diff --git a/src/ch/epfl/xblast/SubCell.java b/src/ch/epfl/xblast/SubCell.java index 51cbbb4..4278fab 100644 --- a/src/ch/epfl/xblast/SubCell.java +++ b/src/ch/epfl/xblast/SubCell.java @@ -31,7 +31,7 @@ public final class SubCell { /** * The coordinates of the SubCell. */ - private int x, y; + private final int x, y; /** * Instantiates a new SubCell with the given coordinates. diff --git a/src/ch/epfl/xblast/server/Board.java b/src/ch/epfl/xblast/server/Board.java index a18422a..4519781 100644 --- a/src/ch/epfl/xblast/server/Board.java +++ b/src/ch/epfl/xblast/server/Board.java @@ -4,7 +4,6 @@ import ch.epfl.cs108.Sq; import ch.epfl.xblast.Cell; import ch.epfl.xblast.Lists; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -41,7 +40,7 @@ public final class Board { /** * List containing all the blocks of the board. */ - private List> blocks; + private final List> blocks; /** * Instantiates a new Board with the given sequence of blocks. @@ -53,7 +52,7 @@ public final class Board { if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE) throw new IllegalArgumentException(); - this.blocks = new ArrayList<>(blocks); + this.blocks = Lists.immutableList(blocks); } /** diff --git a/src/ch/epfl/xblast/server/Bomb.java b/src/ch/epfl/xblast/server/Bomb.java index 535573f..57c271f 100644 --- a/src/ch/epfl/xblast/server/Bomb.java +++ b/src/ch/epfl/xblast/server/Bomb.java @@ -28,22 +28,22 @@ public final class Bomb { /** * Owner of the Bomb. */ - private PlayerID ownerId; + private final PlayerID ownerId; /** * Position of the Bomb. */ - private Cell position; + private final Cell position; /** * Fuse of the Bomb. */ - private Sq fuseLengths; + private final Sq fuseLengths; /** * Range of the Bomb. */ - private int range; + private final int range; /** * Instantiates a new Bomb. diff --git a/src/ch/epfl/xblast/server/GameState.java b/src/ch/epfl/xblast/server/GameState.java index 44fd5d1..abd07b3 100644 --- a/src/ch/epfl/xblast/server/GameState.java +++ b/src/ch/epfl/xblast/server/GameState.java @@ -52,7 +52,7 @@ public final class GameState { * @param players list of the players */ public GameState(Board board, List players) { - this(0, board, players, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); + this(0, board, players, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); } /** @@ -72,11 +72,11 @@ public final class GameState { this.board = Objects.requireNonNull(board); if (players.size() != PlayerID.values().length) throw new IllegalArgumentException(); - this.players = new ArrayList<>(players); + this.players = Lists.immutableList(players); - this.bombs = new ArrayList<>(Objects.requireNonNull(bombs)); - this.explosions = new ArrayList<>(Objects.requireNonNull(explosions)); - this.blasts = new ArrayList<>(Objects.requireNonNull(blasts)); + this.bombs = Lists.immutableList(Objects.requireNonNull(bombs)); + this.explosions = Lists.immutableList(Objects.requireNonNull(explosions)); + this.blasts = Lists.immutableList(Objects.requireNonNull(blasts)); } /** diff --git a/src/ch/epfl/xblast/server/painter/BoardPainter.java b/src/ch/epfl/xblast/server/painter/BoardPainter.java index 8378cce..34d0974 100644 --- a/src/ch/epfl/xblast/server/painter/BoardPainter.java +++ b/src/ch/epfl/xblast/server/painter/BoardPainter.java @@ -2,6 +2,7 @@ package ch.epfl.xblast.server.painter; import ch.epfl.xblast.Cell; import ch.epfl.xblast.Direction; +import ch.epfl.xblast.Lists; import ch.epfl.xblast.server.Block; import ch.epfl.xblast.server.Board; @@ -26,7 +27,7 @@ public final class BoardPainter { * @param shadowedFreeBlock a "shadowed" block image */ public BoardPainter(Map blocksMap, BlockImage shadowedFreeBlock) { - this.blocksMap = Collections.unmodifiableMap(blocksMap); + this.blocksMap = Lists.immutableMap(blocksMap); this.shadowedFreeBlock = shadowedFreeBlock; } -- cgit v1.2.3