diff options
author | Pacien TRAN-GIRARD | 2016-04-25 14:57:21 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-04-25 15:08:37 +0200 |
commit | a73178cc0c313b675d370672c6bd2a5b8d18d817 (patch) | |
tree | e35a1c34e3b0ea5f0e75337bb3f8a3c555e89145 /src/ch | |
parent | 0134f15d18fc57affc8da7369d5d2b0deb021ce4 (diff) | |
download | xblast-a73178cc0c313b675d370672c6bd2a5b8d18d817.tar.gz |
Reinforce immutability
Diffstat (limited to 'src/ch')
-rw-r--r-- | src/ch/epfl/xblast/Lists.java | 23 | ||||
-rw-r--r-- | src/ch/epfl/xblast/SubCell.java | 2 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Board.java | 5 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/Bomb.java | 8 | ||||
-rw-r--r-- | src/ch/epfl/xblast/server/GameState.java | 10 | ||||
-rw-r--r-- | src/ch/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 | |||
@@ -13,6 +13,29 @@ import java.util.stream.Stream; | |||
13 | public final class Lists { | 13 | public final class Lists { |
14 | 14 | ||
15 | /** | 15 | /** |
16 | * Returns an immutable copy of a list. | ||
17 | * | ||
18 | * @param l a list | ||
19 | * @param <T> the element type | ||
20 | * @return the immutable list | ||
21 | */ | ||
22 | public static <T> List<T> immutableList(List<T> l) { | ||
23 | return Collections.unmodifiableList(new ArrayList<>(l)); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Returns an immutable copy of a map. | ||
28 | * | ||
29 | * @param m a mab | ||
30 | * @param <K> the key type | ||
31 | * @param <V> the value type | ||
32 | * @return the immutable copy | ||
33 | */ | ||
34 | public static <K, V> Map<K, V> immutableMap(Map<K, V> m) { | ||
35 | return Collections.unmodifiableMap(new HashMap<>(m)); | ||
36 | } | ||
37 | |||
38 | /** | ||
16 | * Returns a symmetric version of the list, without repeating the last element of the input list. | 39 | * Returns a symmetric version of the list, without repeating the last element of the input list. |
17 | * For instance, mirrored([kay]) will return [kayak]. | 40 | * For instance, mirrored([kay]) will return [kayak]. |
18 | * | 41 | * |
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 { | |||
31 | /** | 31 | /** |
32 | * The coordinates of the SubCell. | 32 | * The coordinates of the SubCell. |
33 | */ | 33 | */ |
34 | private int x, y; | 34 | private final int x, y; |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * Instantiates a new SubCell with the given coordinates. | 37 | * 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; | |||
4 | import ch.epfl.xblast.Cell; | 4 | import ch.epfl.xblast.Cell; |
5 | import ch.epfl.xblast.Lists; | 5 | import ch.epfl.xblast.Lists; |
6 | 6 | ||
7 | import java.util.ArrayList; | ||
8 | import java.util.Collection; | 7 | import java.util.Collection; |
9 | import java.util.Collections; | 8 | import java.util.Collections; |
10 | import java.util.List; | 9 | import java.util.List; |
@@ -41,7 +40,7 @@ public final class Board { | |||
41 | /** | 40 | /** |
42 | * List containing all the blocks of the board. | 41 | * List containing all the blocks of the board. |
43 | */ | 42 | */ |
44 | private List<Sq<Block>> blocks; | 43 | private final List<Sq<Block>> blocks; |
45 | 44 | ||
46 | /** | 45 | /** |
47 | * Instantiates a new Board with the given sequence of blocks. | 46 | * Instantiates a new Board with the given sequence of blocks. |
@@ -53,7 +52,7 @@ public final class Board { | |||
53 | if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE) | 52 | if (blocks == null || blocks.size() != BLOCKS_LIST_SIZE) |
54 | throw new IllegalArgumentException(); | 53 | throw new IllegalArgumentException(); |
55 | 54 | ||
56 | this.blocks = new ArrayList<>(blocks); | 55 | this.blocks = Lists.immutableList(blocks); |
57 | } | 56 | } |
58 | 57 | ||
59 | /** | 58 | /** |
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 { | |||
28 | /** | 28 | /** |
29 | * Owner of the Bomb. | 29 | * Owner of the Bomb. |
30 | */ | 30 | */ |
31 | private PlayerID ownerId; | 31 | private final PlayerID ownerId; |
32 | 32 | ||
33 | /** | 33 | /** |
34 | * Position of the Bomb. | 34 | * Position of the Bomb. |
35 | */ | 35 | */ |
36 | private Cell position; | 36 | private final Cell position; |
37 | 37 | ||
38 | /** | 38 | /** |
39 | * Fuse of the Bomb. | 39 | * Fuse of the Bomb. |
40 | */ | 40 | */ |
41 | private Sq<Integer> fuseLengths; | 41 | private final Sq<Integer> fuseLengths; |
42 | 42 | ||
43 | /** | 43 | /** |
44 | * Range of the Bomb. | 44 | * Range of the Bomb. |
45 | */ | 45 | */ |
46 | private int range; | 46 | private final int range; |
47 | 47 | ||
48 | /** | 48 | /** |
49 | * Instantiates a new Bomb. | 49 | * 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 { | |||
52 | * @param players list of the players | 52 | * @param players list of the players |
53 | */ | 53 | */ |
54 | public GameState(Board board, List<Player> players) { | 54 | public GameState(Board board, List<Player> players) { |
55 | this(0, board, players, new ArrayList<>(), new ArrayList<>(), new ArrayList<>()); | 55 | this(0, board, players, Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); |
56 | } | 56 | } |
57 | 57 | ||
58 | /** | 58 | /** |
@@ -72,11 +72,11 @@ public final class GameState { | |||
72 | this.board = Objects.requireNonNull(board); | 72 | this.board = Objects.requireNonNull(board); |
73 | 73 | ||
74 | if (players.size() != PlayerID.values().length) throw new IllegalArgumentException(); | 74 | if (players.size() != PlayerID.values().length) throw new IllegalArgumentException(); |
75 | this.players = new ArrayList<>(players); | 75 | this.players = Lists.immutableList(players); |
76 | 76 | ||
77 | this.bombs = new ArrayList<>(Objects.requireNonNull(bombs)); | 77 | this.bombs = Lists.immutableList(Objects.requireNonNull(bombs)); |
78 | this.explosions = new ArrayList<>(Objects.requireNonNull(explosions)); | 78 | this.explosions = Lists.immutableList(Objects.requireNonNull(explosions)); |
79 | this.blasts = new ArrayList<>(Objects.requireNonNull(blasts)); | 79 | this.blasts = Lists.immutableList(Objects.requireNonNull(blasts)); |
80 | } | 80 | } |
81 | 81 | ||
82 | /** | 82 | /** |
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; | |||
2 | 2 | ||
3 | import ch.epfl.xblast.Cell; | 3 | import ch.epfl.xblast.Cell; |
4 | import ch.epfl.xblast.Direction; | 4 | import ch.epfl.xblast.Direction; |
5 | import ch.epfl.xblast.Lists; | ||
5 | import ch.epfl.xblast.server.Block; | 6 | import ch.epfl.xblast.server.Block; |
6 | import ch.epfl.xblast.server.Board; | 7 | import ch.epfl.xblast.server.Board; |
7 | 8 | ||
@@ -26,7 +27,7 @@ public final class BoardPainter { | |||
26 | * @param shadowedFreeBlock a "shadowed" block image | 27 | * @param shadowedFreeBlock a "shadowed" block image |
27 | */ | 28 | */ |
28 | public BoardPainter(Map<Block, BlockImage> blocksMap, BlockImage shadowedFreeBlock) { | 29 | public BoardPainter(Map<Block, BlockImage> blocksMap, BlockImage shadowedFreeBlock) { |
29 | this.blocksMap = Collections.unmodifiableMap(blocksMap); | 30 | this.blocksMap = Lists.immutableMap(blocksMap); |
30 | this.shadowedFreeBlock = shadowedFreeBlock; | 31 | this.shadowedFreeBlock = shadowedFreeBlock; |
31 | } | 32 | } |
32 | 33 | ||