From 6781889388164d34749fb6eb05d3b4a5ed74aa1d Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 8 Jan 2018 14:11:11 +0100 Subject: Make Board immutable Signed-off-by: pacien --- src/main/java/fr/umlv/java/wallj/board/Board.java | 54 ++++++++++++++++------ src/main/java/fr/umlv/java/wallj/utils/Matrix.java | 28 +++++++++++ 2 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 src/main/java/fr/umlv/java/wallj/utils/Matrix.java (limited to 'src/main/java/fr/umlv') diff --git a/src/main/java/fr/umlv/java/wallj/board/Board.java b/src/main/java/fr/umlv/java/wallj/board/Board.java index ebe1a7e..dfcec88 100644 --- a/src/main/java/fr/umlv/java/wallj/board/Board.java +++ b/src/main/java/fr/umlv/java/wallj/board/Board.java @@ -1,16 +1,51 @@ package fr.umlv.java.wallj.board; import fr.umlv.java.wallj.model.BlockType; +import fr.umlv.java.wallj.utils.Matrix; /** - * A mutable BlockType matrix. + * An immutable BlockType matrix. */ public final class Board { + /** + * Board Builder + */ + public static final class Builder { + private final BlockType[][] map; + + /** + * @param width width in tiles + * @param height height in tiles + */ + public Builder(int width, int height) { + this.map = new BlockType[width][height]; + } + + /** + * @param pos the tile position vector + * @param type the BlockType to set + * @return the Builder + */ + public Builder setBlockTypeAt(TileVec2 pos, BlockType type) { + map[pos.getCol()][pos.getRow()] = type; + return this; + } + + /** + * @return the immutable Board + */ + public Board build() { + return new Board(map); + } + } + private final BlockType[][] map; - public Board(int width, int height) { - map = new BlockType[width][height]; + private Board(BlockType[][] map) { + int w = Matrix.getWidth(map), h = Matrix.getHeight(map); + this.map = new BlockType[w][h]; + for (int row = 0; row < w; ++row) System.arraycopy(map[row], 0, this.map[row], 0, h); } /** @@ -21,22 +56,11 @@ public final class Board { return map[pos.getCol()][pos.getRow()]; } - /** - * @param pos the tile position vector - * @param type the BlockType to set - * @return the overwritten element - */ - public BlockType setBlockTypeAt(TileVec2 pos, BlockType type) { - BlockType old = map[pos.getCol()][pos.getRow()]; - map[pos.getCol()][pos.getRow()] = type; - return old; - } - /** * @return the dimension of the Board */ public TileVec2 getDim() { - return TileVec2.of(map.length, map.length > 0 ? map[0].length : 0); + return TileVec2.of(Matrix.getWidth(map), Matrix.getHeight(map)); } } diff --git a/src/main/java/fr/umlv/java/wallj/utils/Matrix.java b/src/main/java/fr/umlv/java/wallj/utils/Matrix.java new file mode 100644 index 0000000..e23cb2a --- /dev/null +++ b/src/main/java/fr/umlv/java/wallj/utils/Matrix.java @@ -0,0 +1,28 @@ +package fr.umlv.java.wallj.utils; + +/** + * Utility functions for two dimension arrays. + */ +public final class Matrix { + + /** + * @param m the matrix + * @return the width of the matrix (0 if null) + */ + public static int getWidth(Object[][] m) { + return m != null && m.length > 0 ? m[0].length : 0; + } + + /** + * @param m the matrix + * @return the height of the matrix (0 if null) + */ + public static int getHeight(Object[][] m) { + return m != null ? m.length : 0; + } + + private Matrix() { + // static class + } + +} -- cgit v1.2.3