From bf36ed4a84c031a6d1b992584e5081412360aab1 Mon Sep 17 00:00:00 2001 From: Astesana Date: Tue, 14 May 2024 11:41:56 +0200 Subject: [PATCH] Fixes some Sonar complaints --- src/main/java/com/fathzer/jchess/ai/JChessEngine.java | 4 ++-- src/main/java/com/fathzer/jchess/ai/debug/NegaMaxSpy.java | 4 +--- .../jchess/chessutils/JChessBoardExplorerBuilder.java | 8 -------- src/main/java/com/fathzer/jchess/fen/FENParser.java | 3 +-- .../fathzer/jchess/pgn/MoveAlgebraicNotationBuilder.java | 5 ++--- .../java/com/fathzer/jchess/ai/MinimaxEngineTest.java | 3 +-- .../java/com/fathzer/jchess/generic/ChessBoardTest.java | 4 ++-- .../java/com/fathzer/jchess/generic/MovesBuilderTest.java | 3 +-- 8 files changed, 10 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/fathzer/jchess/ai/JChessEngine.java b/src/main/java/com/fathzer/jchess/ai/JChessEngine.java index bf94e2b..f4eed9e 100644 --- a/src/main/java/com/fathzer/jchess/ai/JChessEngine.java +++ b/src/main/java/com/fathzer/jchess/ai/JChessEngine.java @@ -71,7 +71,7 @@ public void logSearchEnd(Board board, SearchHistory result) { } public String toString(Collection> moves) { - return moves.stream().map(em -> toString(em)).collect(Collectors.joining(", ", "[", "]")); + return moves.stream().map(this::toString).collect(Collectors.joining(", ", "[", "]")); } public String toString(EvaluatedMove ev) { @@ -118,7 +118,7 @@ public void logMoveChosen(Board board, EvaluatedMove evaluatedMove) } else { log.info("Move chosen :{}", evaluatedMove.getContent().toString(board.getCoordinatesSystem())); final List pv = evaluatedMove.getPrincipalVariation(); - log.info("pv: {}", pv.stream().map(m -> m.toString(board.getCoordinatesSystem())).collect(Collectors.toList())); + log.info("pv: {}", pv.stream().map(m -> m.toString(board.getCoordinatesSystem())).toList()); } } } diff --git a/src/main/java/com/fathzer/jchess/ai/debug/NegaMaxSpy.java b/src/main/java/com/fathzer/jchess/ai/debug/NegaMaxSpy.java index 551af72..7491829 100644 --- a/src/main/java/com/fathzer/jchess/ai/debug/NegaMaxSpy.java +++ b/src/main/java/com/fathzer/jchess/ai/debug/NegaMaxSpy.java @@ -3,7 +3,6 @@ import static com.fathzer.games.ai.experimental.KeyBasedNegaMaxSpyFilter.*; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.IntStream; import com.fathzer.games.ai.AlphaBetaState; @@ -90,7 +89,6 @@ private List getMoves(TreeSearchStateStack> state) { .mapToObj(i->{ final Move mv = state.get(i).lastMove; return mv==null ? "?"+i: mv.toString(cs); - }) - .collect(Collectors.toList()); + }).toList(); } } \ No newline at end of file diff --git a/src/main/java/com/fathzer/jchess/chessutils/JChessBoardExplorerBuilder.java b/src/main/java/com/fathzer/jchess/chessutils/JChessBoardExplorerBuilder.java index 949aa1a..3389f44 100644 --- a/src/main/java/com/fathzer/jchess/chessutils/JChessBoardExplorerBuilder.java +++ b/src/main/java/com/fathzer/jchess/chessutils/JChessBoardExplorerBuilder.java @@ -1,7 +1,5 @@ package com.fathzer.jchess.chessutils; -import java.util.stream.IntStream; - import com.fathzer.chess.utils.adapters.BoardExplorer; import com.fathzer.chess.utils.adapters.BoardExplorerBuilder; import com.fathzer.jchess.Board; @@ -13,10 +11,4 @@ public interface JChessBoardExplorerBuilder extends BoardExplorerBuilder board) { return new JChessBoardExplorer(board); } - -// @Override -// default IntStream getPieces(Board board) { -// // TODO Auto-generated method stub -// return BoardExplorerBuilder.super.getPieces(board); -// } } diff --git a/src/main/java/com/fathzer/jchess/fen/FENParser.java b/src/main/java/com/fathzer/jchess/fen/FENParser.java index f3b367b..e9dc10e 100644 --- a/src/main/java/com/fathzer/jchess/fen/FENParser.java +++ b/src/main/java/com/fathzer/jchess/fen/FENParser.java @@ -28,7 +28,6 @@ import java.util.OptionalInt; import java.util.function.Predicate; import java.util.function.Supplier; -import java.util.stream.Collectors; import java.util.stream.IntStream; import com.fathzer.games.Color; @@ -152,7 +151,7 @@ private Collection getCastlings(String code) { if ("-".equals(code)) { return Collections.emptyList(); } else { - return code.chars().mapToObj(c -> toCastling((char)c)).collect(Collectors.toList()); + return code.chars().mapToObj(c -> toCastling((char)c)).toList(); } } diff --git a/src/main/java/com/fathzer/jchess/pgn/MoveAlgebraicNotationBuilder.java b/src/main/java/com/fathzer/jchess/pgn/MoveAlgebraicNotationBuilder.java index 85404be..32a021e 100644 --- a/src/main/java/com/fathzer/jchess/pgn/MoveAlgebraicNotationBuilder.java +++ b/src/main/java/com/fathzer/jchess/pgn/MoveAlgebraicNotationBuilder.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.Optional; import java.util.function.Function; -import java.util.stream.Collectors; import java.util.stream.StreamSupport; import com.fathzer.games.MoveGenerator.MoveConfidence; @@ -41,7 +40,7 @@ public String get(Board board, Move move) { // First, keep only moves with the right destination // This list will allow us to check if the move is valid and if it needs disambiguation final int to = move.getTo(); - final List candidates = StreamSupport.stream(state.spliterator(),false).filter(m -> m.getTo()==to).collect(Collectors.toList()); + final List candidates = StreamSupport.stream(state.spliterator(),false).filter(m -> m.getTo()==to).toList(); if (!checkValidMove(move, candidates)) { throw new IllegalArgumentException("Move "+moveToString(move, board)+" is not valid"); } @@ -114,7 +113,7 @@ private CharSequence encodeMove(Board board, Move move, List candida private String getAmbiguitiesRemoval(Board board, Move move, List candidates) { final int from = move.getFrom(); final PieceKind kind = board.getPiece(from).getKind(); - final List ambiguities = candidates.stream().filter(m -> m.getFrom()!=from && kind==board.getPiece(m.getFrom()).getKind()).collect(Collectors.toList()); + final List ambiguities = candidates.stream().filter(m -> m.getFrom()!=from && kind==board.getPiece(m.getFrom()).getKind()).toList(); if (ambiguities.isEmpty()) { return ""; } diff --git a/src/test/java/com/fathzer/jchess/ai/MinimaxEngineTest.java b/src/test/java/com/fathzer/jchess/ai/MinimaxEngineTest.java index 95d12ed..21265b2 100644 --- a/src/test/java/com/fathzer/jchess/ai/MinimaxEngineTest.java +++ b/src/test/java/com/fathzer/jchess/ai/MinimaxEngineTest.java @@ -6,7 +6,6 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -247,7 +246,7 @@ void bug20230911_chase() { spy.searchedKey = -3019684505475777408L; //d7d8 spy.searchedKey = 1283331931822092560L; //h6h8 e = ai.getBestMoves(Collections.singletonList(h1h6),new SearchParameters(8)).getList().get(0); - System.out.println("pv="+tt.collectPV(board, h1h6, 8).stream().map(m->m.toString(cs)).collect(Collectors.toList())); + System.out.println("pv="+tt.collectPV(board, h1h6, 8).stream().map(m->m.toString(cs)).toList()); assertEquals(Type.WIN, e.getEvaluation().getType()); assertEquals(4, e.getEvaluation().getCountToEnd()); } diff --git a/src/test/java/com/fathzer/jchess/generic/ChessBoardTest.java b/src/test/java/com/fathzer/jchess/generic/ChessBoardTest.java index 6f05b92..ba52584 100644 --- a/src/test/java/com/fathzer/jchess/generic/ChessBoardTest.java +++ b/src/test/java/com/fathzer/jchess/generic/ChessBoardTest.java @@ -298,7 +298,7 @@ private List getMoves(Board board, String from) { private List getMoves(CoordinatesSystem cs, List moves, String from) { final int fromIndex = cs.getIndex(from); - return moves.stream().filter(m-> m.getFrom()==fromIndex).collect(Collectors.toList()); + return moves.stream().filter(m-> m.getFrom()==fromIndex).toList(); } private Set getTo(CoordinatesSystem cs, List moves) { @@ -323,7 +323,7 @@ void kingsMoveGenerationTest() { // Can castle List moves = FENUtils.from("r3kb1r/ppp2ppp/2nqb2n/P2p4/2P1p3/6R1/1PQPPPPP/1NB1KBNR b Kkq - 1 8").getMoves(); - moves = moves.stream().filter(m->"e8".equals(cs.getAlgebraicNotation(m.getFrom()))).collect(Collectors.toList()); + moves = moves.stream().filter(m->"e8".equals(cs.getAlgebraicNotation(m.getFrom()))).toList(); assertEquals(Set.of("c8","d8", "d7", "e7"), getTo(cs, moves)); assertEquals(4, moves.size(), asString(moves, board)); } diff --git a/src/test/java/com/fathzer/jchess/generic/MovesBuilderTest.java b/src/test/java/com/fathzer/jchess/generic/MovesBuilderTest.java index 42f375f..d7246cd 100644 --- a/src/test/java/com/fathzer/jchess/generic/MovesBuilderTest.java +++ b/src/test/java/com/fathzer/jchess/generic/MovesBuilderTest.java @@ -4,7 +4,6 @@ import java.util.List; import java.util.Set; -import java.util.stream.Collectors; import org.junit.jupiter.api.Test; @@ -16,7 +15,7 @@ class MovesBuilderTest { private static List filterFrom(List moves, String coord, CoordinatesSystem cs) { final int index = cs.getIndex(coord); - return moves.stream().filter(m -> m.getFrom()==index).collect(Collectors.toList()); + return moves.stream().filter(m -> m.getFrom()==index).toList(); } @Test