@Test public void testIfInvalidMove(){ Matrix testMatrix = getTestMatrix(MoveValidator.MIN_SIZE_FOR_SCRAMBLING); for(Map.Entry<ScrambleMoves, ScrambleMove> curEntry : validMoves.entrySet()){ MoveValidator.throwIfInvalidMove(testMatrix, curEntry.getValue()); MoveValidator.throwIfInvalidMove(testMatrix, curEntry.getValue(), curEntry.getKey()); } for(Map.Entry<Integer, ScrambleMove> curEntry : invalidMoves.entrySet()){ try { MoveValidator.throwIfInvalidMove(testMatrix, curEntry.getValue()); Assert.fail(); }catch (IllegalArgumentException|IndexOutOfBoundsException e){ } } for(Map.Entry<ScrambleMoves, ScrambleMove> curEntry : invalidMovesBadType.entrySet()){ try { MoveValidator.throwIfInvalidMove(testMatrix, curEntry.getValue(), curEntry.getKey()); Assert.fail(); }catch (IllegalArgumentException|IndexOutOfBoundsException e){ } } }
public static void throwIfInvalidMove(Matrix matrix, ScrambleMove move) { switch (move.move) { case SWAP: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X2), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y2), Plane.Y); if ( move.getArg(ScrambleConstants.Swap.X1) == move.getArg(ScrambleConstants.Swap.X2) && move.getArg(ScrambleConstants.Swap.Y1) == move.getArg(ScrambleConstants.Swap.Y2) ) { throw new IllegalArgumentException("Can't swap a node's value with itself."); } break; case SWAP_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL2), Plane.Y); break; case SWAP_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL2), Plane.X); break; case SLIDE_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideRow.ROWCOL), Plane.Y); break; case SLIDE_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideCol.ROWCOL), Plane.X); break; case ROT_BOX: if (!(move.getArg(ScrambleConstants.RotateBox.ROTNUM) >= -3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) <= 3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) != 0)) { throw new IllegalArgumentException("Invalid number of rotations given. Given: " + move.getArg(ScrambleConstants.RotateBox.ROTNUM)); } MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.X), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.Y), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.X) - 1, Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.Y) - 1, Plane.Y); if (move.getArg(ScrambleConstants.RotateBox.SIZE) < MIN_SIZE_FOR_ROTATION) { throw new IllegalArgumentException("Invalid size of sub matrix."); } break; default: throw new IllegalArgumentException("Unsupported move passed to validator."); } }
MoveValidator { public static void throwIfInvalidMove(Matrix matrix, ScrambleMove move) { switch (move.move) { case SWAP: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X2), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y2), Plane.Y); if ( move.getArg(ScrambleConstants.Swap.X1) == move.getArg(ScrambleConstants.Swap.X2) && move.getArg(ScrambleConstants.Swap.Y1) == move.getArg(ScrambleConstants.Swap.Y2) ) { throw new IllegalArgumentException("Can't swap a node's value with itself."); } break; case SWAP_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL2), Plane.Y); break; case SWAP_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL2), Plane.X); break; case SLIDE_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideRow.ROWCOL), Plane.Y); break; case SLIDE_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideCol.ROWCOL), Plane.X); break; case ROT_BOX: if (!(move.getArg(ScrambleConstants.RotateBox.ROTNUM) >= -3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) <= 3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) != 0)) { throw new IllegalArgumentException("Invalid number of rotations given. Given: " + move.getArg(ScrambleConstants.RotateBox.ROTNUM)); } MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.X), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.Y), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.X) - 1, Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.Y) - 1, Plane.Y); if (move.getArg(ScrambleConstants.RotateBox.SIZE) < MIN_SIZE_FOR_ROTATION) { throw new IllegalArgumentException("Invalid size of sub matrix."); } break; default: throw new IllegalArgumentException("Unsupported move passed to validator."); } } }
MoveValidator { public static void throwIfInvalidMove(Matrix matrix, ScrambleMove move) { switch (move.move) { case SWAP: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X2), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y2), Plane.Y); if ( move.getArg(ScrambleConstants.Swap.X1) == move.getArg(ScrambleConstants.Swap.X2) && move.getArg(ScrambleConstants.Swap.Y1) == move.getArg(ScrambleConstants.Swap.Y2) ) { throw new IllegalArgumentException("Can't swap a node's value with itself."); } break; case SWAP_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL2), Plane.Y); break; case SWAP_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL2), Plane.X); break; case SLIDE_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideRow.ROWCOL), Plane.Y); break; case SLIDE_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideCol.ROWCOL), Plane.X); break; case ROT_BOX: if (!(move.getArg(ScrambleConstants.RotateBox.ROTNUM) >= -3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) <= 3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) != 0)) { throw new IllegalArgumentException("Invalid number of rotations given. Given: " + move.getArg(ScrambleConstants.RotateBox.ROTNUM)); } MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.X), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.Y), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.X) - 1, Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.Y) - 1, Plane.Y); if (move.getArg(ScrambleConstants.RotateBox.SIZE) < MIN_SIZE_FOR_ROTATION) { throw new IllegalArgumentException("Invalid size of sub matrix."); } break; default: throw new IllegalArgumentException("Unsupported move passed to validator."); } }  }
MoveValidator { public static void throwIfInvalidMove(Matrix matrix, ScrambleMove move) { switch (move.move) { case SWAP: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X2), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y2), Plane.Y); if ( move.getArg(ScrambleConstants.Swap.X1) == move.getArg(ScrambleConstants.Swap.X2) && move.getArg(ScrambleConstants.Swap.Y1) == move.getArg(ScrambleConstants.Swap.Y2) ) { throw new IllegalArgumentException("Can't swap a node's value with itself."); } break; case SWAP_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL2), Plane.Y); break; case SWAP_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL2), Plane.X); break; case SLIDE_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideRow.ROWCOL), Plane.Y); break; case SLIDE_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideCol.ROWCOL), Plane.X); break; case ROT_BOX: if (!(move.getArg(ScrambleConstants.RotateBox.ROTNUM) >= -3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) <= 3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) != 0)) { throw new IllegalArgumentException("Invalid number of rotations given. Given: " + move.getArg(ScrambleConstants.RotateBox.ROTNUM)); } MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.X), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.Y), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.X) - 1, Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.Y) - 1, Plane.Y); if (move.getArg(ScrambleConstants.RotateBox.SIZE) < MIN_SIZE_FOR_ROTATION) { throw new IllegalArgumentException("Invalid size of sub matrix."); } break; default: throw new IllegalArgumentException("Unsupported move passed to validator."); } }  static boolean matrixIsTooSmallForScrambling(Matrix matrix); static void throwIfMatrixTooSmallForScrambling(Matrix matrix); static void throwIfInvalidMove(Matrix matrix, ScrambleMove move); static void throwIfInvalidMove(Matrix matrix, ScrambleMove move, ScrambleMoves moveToBe); }
MoveValidator { public static void throwIfInvalidMove(Matrix matrix, ScrambleMove move) { switch (move.move) { case SWAP: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.X2), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.Swap.Y2), Plane.Y); if ( move.getArg(ScrambleConstants.Swap.X1) == move.getArg(ScrambleConstants.Swap.X2) && move.getArg(ScrambleConstants.Swap.Y1) == move.getArg(ScrambleConstants.Swap.Y2) ) { throw new IllegalArgumentException("Can't swap a node's value with itself."); } break; case SWAP_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL1), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapRow.ROWCOL2), Plane.Y); break; case SWAP_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL1), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SwapCol.ROWCOL2), Plane.X); break; case SLIDE_ROW: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideRow.ROWCOL), Plane.Y); break; case SLIDE_COL: MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.SlideCol.ROWCOL), Plane.X); break; case ROT_BOX: if (!(move.getArg(ScrambleConstants.RotateBox.ROTNUM) >= -3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) <= 3 && move.getArg(ScrambleConstants.RotateBox.ROTNUM) != 0)) { throw new IllegalArgumentException("Invalid number of rotations given. Given: " + move.getArg(ScrambleConstants.RotateBox.ROTNUM)); } MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.X), Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.Y), Plane.Y); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.X) - 1, Plane.X); MatrixValidator.throwIfBadIndex(matrix, move.getArg(ScrambleConstants.RotateBox.SIZE) + move.getArg(ScrambleConstants.RotateBox.Y) - 1, Plane.Y); if (move.getArg(ScrambleConstants.RotateBox.SIZE) < MIN_SIZE_FOR_ROTATION) { throw new IllegalArgumentException("Invalid size of sub matrix."); } break; default: throw new IllegalArgumentException("Unsupported move passed to validator."); } }  static boolean matrixIsTooSmallForScrambling(Matrix matrix); static void throwIfMatrixTooSmallForScrambling(Matrix matrix); static void throwIfInvalidMove(Matrix matrix, ScrambleMove move); static void throwIfInvalidMove(Matrix matrix, ScrambleMove move, ScrambleMoves moveToBe); static final long MIN_SIZE_FOR_SCRAMBLING; static final long MIN_SIZE_FOR_ROTATION; }