# HG changeset patch # User Jaroslav Tulach # Date 1262901077 -3600 # Node ID c5fbddc4c590216f2f3aafb6d1534f43cf26621e # Parent 4b78d4f028b3f331ab2b397978e2199c65af3a68 Renaming Board.board2HashCode to getCode() diff -r 4b78d4f028b3 -r c5fbddc4c590 freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Thu Jan 07 22:34:17 2010 +0100 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java Thu Jan 07 22:51:17 2010 +0100 @@ -38,6 +38,7 @@ import cz.xelfi.quoridor.IllegalPositionException; import java.io.IOException; import java.io.InputStream; +import java.io.StringWriter; import java.net.URI; import java.util.Date; import java.util.HashMap; @@ -233,17 +234,27 @@ } String bCode = null; try{ - bCode = stat.path("openings").path(Board.board2HashCode(b)+".check").queryParam("loginID", user.getId()).accept(MediaType.TEXT_PLAIN).get(String.class); + bCode = stat.path("openings").path(b.getCode()+".check").queryParam("loginID", user.getId()).accept(MediaType.TEXT_PLAIN).get(String.class); }catch(Exception e){ bCode = null; } if(bCode == null || "".equals(bCode)) - v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture",b.boardToPicture()); + v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b)); else - v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture",b.boardToPicture(),"bCode", bCode); + v = viewable("game.fmt", doc, "message", msg, "format", format, "board", b,"textPicture", boardToPicture(b),"bCode", bCode); return resp.entity(v).build(); } + private static String boardToPicture(Board b) { + StringWriter w = new StringWriter(); + try { + b.write(w); + } catch (IOException ex) { + return ex.toString(); + } + return w.toString(); + } + @GET @Path("games/{id}/move") @Produces(MediaType.TEXT_HTML) @@ -418,7 +429,7 @@ @PathParam("code") String code, @QueryParam("fieldSize") @DefaultValue("40") int fieldSize ) throws IllegalPositionException { - Board b = new Board(code); + Board b = Board.valueOf(code); ResponseBuilder resp = Response.ok(); CacheControl cc = new CacheControl(); cc.setNoCache(true); diff -r 4b78d4f028b3 -r c5fbddc4c590 quoridor/src/main/java/cz/xelfi/quoridor/Board.java --- a/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Thu Jan 07 22:34:17 2010 +0100 +++ b/quoridor/src/main/java/cz/xelfi/quoridor/Board.java Thu Jan 07 22:51:17 2010 +0100 @@ -32,7 +32,6 @@ import java.io.IOException; import java.io.Reader; import java.io.StringReader; -import java.io.StringWriter; import java.io.Writer; import java.util.ArrayList; import java.util.Arrays; @@ -357,15 +356,6 @@ return new Board(board); } - public static Board picture2board(String board) throws IllegalPositionException { - try { - return read(new StringReader(board)); - } catch (IOException ex) { - // shall not happen, StringReader does not throw IOException - throw (IllegalPositionException)new IllegalPositionException(ex.getMessage()).initCause(ex); - } - } - private static int assertChar(String s, int pos, char... ch) throws IOException { if (s.length() >= pos) { for (int i = 0; i < ch.length; i++) { @@ -702,20 +692,9 @@ */ @Override public String toString() { - return Board.board2HashCode(this); + return getCode(); } - public String boardToPicture() { - StringWriter w = new StringWriter(); - try { - write(w); - } catch (IOException ex) { - return ex.toString(); - } - return w.toString(); - } - - // // Validation methods // @@ -872,7 +851,7 @@ return 17 * y + x; } - public Board(String hashCode) throws IllegalStateException{ + Board(String hashCode) throws IllegalStateException{ this.fences = new HashSet(); if((hashCode != null) && (hashCode.length() > 6)){ char[]c = hashCode.toCharArray(); @@ -920,7 +899,8 @@ } } - public static String board2HashCode(Board b){ + public final String getCode() { + Board b = this; StringBuilder sb = new StringBuilder(); for(Player p: b.getPlayers()){ sb.append((char)(p.getColumn() + 'A')); diff -r 4b78d4f028b3 -r c5fbddc4c590 quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java --- a/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Thu Jan 07 22:34:17 2010 +0100 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/BoardCase.java Thu Jan 07 22:51:17 2010 +0100 @@ -27,6 +27,8 @@ import cz.xelfi.quoridor.Fence.Orientation; import cz.xelfi.quoridor.Player.Direction; +import java.io.IOException; +import java.io.StringReader; import java.util.List; import junit.framework.TestCase; @@ -200,7 +202,7 @@ "\n" + " [S] \n"; - b = Board.picture2board(b).toString(); + b = picture2board(b).toString(); Board begin = Board.valueOf(b); try { @@ -498,4 +500,8 @@ assertFalse(f4.equals(f5)); assertFalse(f3.equals(f5)); } + static Board picture2board(String text) throws IOException, IllegalPositionException { + StringReader sr = new StringReader(text); + return Board.read(sr); + } } diff -r 4b78d4f028b3 -r c5fbddc4c590 quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java --- a/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java Thu Jan 07 22:34:17 2010 +0100 +++ b/quoridor/src/test/java/cz/xelfi/quoridor/SerializeTest.java Thu Jan 07 22:51:17 2010 +0100 @@ -27,7 +27,6 @@ import cz.xelfi.quoridor.Player.Direction; import java.io.IOException; -import java.io.StringReader; import java.io.StringWriter; /** Basic tests in simple configuration. @@ -72,7 +71,7 @@ StringWriter w = new StringWriter(); b.write(w); w.close(); - return Board.picture2board(w.toString()); + return picture2board(w.toString()); //return Board.valueOf(w.toString()); } diff -r 4b78d4f028b3 -r c5fbddc4c590 statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java --- a/statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java Thu Jan 07 22:34:17 2010 +0100 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTree.java Thu Jan 07 22:51:17 2010 +0100 @@ -69,8 +69,8 @@ mirrorParentNode = mirrorNode; board = board.apply(move.getMove()); mirrorBoard = mirrorBoard.apply(move.getMove().getMirrorMove()); - String hashCode = Board.board2HashCode(board); - String mirrorHashCode = Board.board2HashCode(mirrorBoard); + String hashCode = board.getCode(); + String mirrorHashCode = mirrorBoard.getCode(); node = nodes.get(hashCode); if(node == null){ node = new OpeningTreeNode(hashCode); diff -r 4b78d4f028b3 -r c5fbddc4c590 webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java --- a/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java Thu Jan 07 22:34:17 2010 +0100 +++ b/webidor/src/test/java/cz/xelfi/quoridor/webidor/QuoridorTest.java Thu Jan 07 22:51:17 2010 +0100 @@ -26,6 +26,8 @@ package cz.xelfi.quoridor.webidor; +import cz.xelfi.quoridor.IllegalPositionException; +import java.io.StringWriter; import com.sun.jersey.api.client.GenericType; import com.sun.jersey.api.client.UniformInterfaceException; import com.sun.jersey.core.header.MediaTypes; @@ -38,6 +40,7 @@ import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; +import java.io.StringReader; import java.util.List; import java.util.Map; import javax.ws.rs.core.MediaType; @@ -201,7 +204,7 @@ class GMap extends GenericType>{} String text = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_PLAIN).get(String.class); - text = (new Board(text)).boardToPicture(); + text = (boardToPicture(Board.valueOf(text))); if (text.indexOf("-----") == -1) { fail("Expecting board:\n" + text); } @@ -209,12 +212,25 @@ String sGame = webResource.path("games").path(s.getId()).accept(MediaType.TEXT_XML).get(String.class); assertNotNull("Game really returned", readGame); // assertEquals("Same game as in text representation", readGame.getBoard(), Board.valueOf(text)); - assertEquals("Same game as in text representation", readGame.getBoard(), Board.picture2board(text)); + assertEquals("Same game as in text representation", readGame.getBoard(), picture2board(text)); // assertEquals("It is same as text of our game", readGame.getBoard().toString(), text); - assertEquals("It is same as text of our game", readGame.getBoard().boardToPicture(), text); + assertEquals("It is same as text of our game", boardToPicture(readGame.getBoard()), text); assertEquals(Move.NORTH, readGame.getMoves().get(0).getMove()); assertEquals(Move.SOUTH, readGame.getMoves().get(1).getMove()); } + private static String boardToPicture(Board b) { + StringWriter w = new StringWriter(); + try { + b.write(w); + } catch (IOException ex) { + return ex.toString(); + } + return w.toString(); + } + private static Board picture2board(String text) throws IOException, IllegalPositionException { + StringReader sr = new StringReader(text); + return Board.read(sr); + } }