Skip to content

Commit

Permalink
Merge pull request #102 from dcolazin/apache-commons-dep
Browse files Browse the repository at this point in the history
use apache commons stringutils
  • Loading branch information
bhlangonijr authored Jan 30, 2023
2 parents 55ab421 + defdd22 commit 4df8d6f
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 73 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/github/bhlangonijr/chesslib/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.github.bhlangonijr.chesslib.move.MoveGenerator;
import com.github.bhlangonijr.chesslib.move.MoveList;
import com.github.bhlangonijr.chesslib.util.XorShiftRandom;
import org.apache.commons.lang3.StringUtils;

/**
* The definition of a chessboard position and its status. It exposes methods to manipulate the board, evolve the
Expand Down Expand Up @@ -777,7 +778,7 @@ public void loadFromFen(String fen) {
for (int i = 0; i < r.length(); i++) {
char c = r.charAt(i);
if (Character.isDigit(c)) {
file += Integer.parseInt(c + "");
file += Character.digit(c, 10);
} else {
Square sq = Square.encode(Rank.allRanks[rank], File.allFiles[file]);
setPiece(Piece.fromFenSymbol(String.valueOf(c)), sq);
Expand Down Expand Up @@ -809,7 +810,7 @@ public void loadFromFen(String fen) {
castleRight.put(Side.BLACK, CastleRight.NONE);
}

String[] flags = state.split(" ");
String[] flags = state.split(StringUtils.SPACE);

if (flags.length >= 3) {
String s = flags[2].toUpperCase().trim();
Expand Down Expand Up @@ -929,7 +930,7 @@ public String getFen(boolean includeCounters, boolean onlyOutputEnPassantIfCaptu
fen.append(" b");
}

String rights = "";
String rights = StringUtils.EMPTY;
if (CastleRight.KING_AND_QUEEN_SIDE.
equals(castleRight.get(Side.WHITE))) {
rights += "KQ";
Expand All @@ -952,26 +953,25 @@ public String getFen(boolean includeCounters, boolean onlyOutputEnPassantIfCaptu
rights += "q";
}

if (rights.equals("")) {
if (StringUtils.isEmpty(rights)) {
fen.append(" -");
} else {
fen.append(" " + rights);
fen.append(StringUtils.SPACE + rights);
}

if (Square.NONE.equals(getEnPassant())
|| (onlyOutputEnPassantIfCapturable
&& !pawnCanBeCapturedEnPassant())) {
fen.append(" -");
} else {
fen.append(" ");
fen.append(StringUtils.SPACE);
fen.append(getEnPassant().toString().toLowerCase());
}

if (includeCounters) {
fen.append(" ");
fen.append(StringUtils.SPACE);
fen.append(getHalfMoveCounter());

fen.append(" ");
fen.append(StringUtils.SPACE);
fen.append(getMoveCounter());
}

Expand Down Expand Up @@ -1525,8 +1525,8 @@ public void setEnableEvents(boolean enableEvents) {
* @see Board#getZobristKey()
*/
public String getPositionId() {
String[] parts = this.getFen(false).split(" ");
return parts[0] + " " + parts[1] + " " + parts[2] +
String[] parts = this.getFen(false).split(StringUtils.SPACE);
return parts[0] + StringUtils.SPACE + parts[1] + StringUtils.SPACE + parts[2] +
(this.getEnPassantTarget() != Square.NONE ? parts[3] : "-");
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/github/bhlangonijr/chesslib/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.github.bhlangonijr.chesslib;

import org.apache.commons.lang3.StringUtils;

/**
* The files in a board. A <i>file</i> is a column in the chessboard, and it is identified as a letter from {@code A} to
* {@code H}.
Expand Down Expand Up @@ -59,7 +61,7 @@ public enum File {
/**
* Special value that represents no file in particular.
*/
NONE("");
NONE(StringUtils.EMPTY);

public static final File[] allFiles = values();

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/github/bhlangonijr/chesslib/PieceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.github.bhlangonijr.chesslib;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

Expand All @@ -30,7 +32,7 @@ public enum PieceType {
/**
* The pawn piece type.
*/
PAWN(""),
PAWN(StringUtils.EMPTY),
/**
* The knight piece type.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/github/bhlangonijr/chesslib/Rank.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.github.bhlangonijr.chesslib;

import org.apache.commons.lang3.StringUtils;

/**
* The ranks in a board. A <i>rank</i> is a raw in the chessboard, and it is identified as a number from 1 to 8.
* <p>
Expand Down Expand Up @@ -58,7 +60,7 @@ public enum Rank {
/**
* Special value that represents no rank in particular.
*/
NONE("");
NONE(StringUtils.EMPTY);

public static final Rank[] allRanks = values();

Expand Down
31 changes: 16 additions & 15 deletions src/main/java/com/github/bhlangonijr/chesslib/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.github.bhlangonijr.chesslib.move.MoveList;
import com.github.bhlangonijr.chesslib.pgn.PgnException;
import com.github.bhlangonijr.chesslib.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

/**
* A chess game, as defined by the specifications of the Portable Game Notation (PGN) format.
Expand Down Expand Up @@ -81,7 +82,7 @@ private static String makeProp(String name, String value) {
private static String getMovesAt(String moves, int index) {
StringBuilder b = new StringBuilder();
int count = 0;
for (String m : moves.split(" ")) {
for (String m : moves.split(StringUtils.SPACE)) {
count++;
if (count >= index) {
break;
Expand Down Expand Up @@ -305,7 +306,7 @@ public void setNag(Map<Integer, String> nag) {
*/
public MoveList getHalfMoves() {
if (halfMoves == null) {
if (getFen() != null && !getFen().trim().equals("")) {
if (StringUtils.isNotBlank(getFen())) {
halfMoves = new MoveList(getFen());
} else {
halfMoves = new MoveList();
Expand Down Expand Up @@ -384,7 +385,7 @@ public String toPgn(boolean includeVariations, boolean includeComments) throws M
sb.append(makeProp("Event", getRound().getEvent().getName()));
sb.append(makeProp("Site", getRound().getEvent().getSite()));
sb.append(makeProp("Date", getRound().getEvent().getStartDate()));
sb.append(makeProp("Round", getRound().getNumber() + ""));
sb.append(makeProp("Round", String.valueOf(getRound().getNumber())));
sb.append(makeProp("White", getWhitePlayer().getName()));
sb.append(makeProp("Black", getBlackPlayer().getName()));
sb.append(makeProp("Result", getResult().getDescription()));
Expand All @@ -397,23 +398,23 @@ public String toPgn(boolean includeVariations, boolean includeComments) throws M
} else {
sb.append(makeProp("TimeControl", "-"));
}
if (getAnnotator() != null && !getAnnotator().equals("")) {
if (StringUtils.isNotEmpty(getAnnotator())) {
sb.append(makeProp("Annotator", getAnnotator()));
}
if (getFen() != null && !getFen().equals("")) {
if (StringUtils.isNotEmpty(getFen())) {
sb.append(makeProp("FEN", getFen()));
}
if (getEco() != null && !getEco().equals("")) {
if (StringUtils.isNotEmpty(getEco())) {
sb.append(makeProp("ECO", getEco()));
}
if (getOpening() != null && !getOpening().equals("")) {
if (StringUtils.isNotEmpty(getOpening())) {
sb.append(makeProp("Opening", getOpening()));
}
if (getWhitePlayer().getElo() > 0) {
sb.append(makeProp("WhiteElo", getWhitePlayer().getElo() + ""));
sb.append(makeProp("WhiteElo", String.valueOf(getWhitePlayer().getElo())));
}
if (getBlackPlayer().getElo() > 0) {
sb.append(makeProp("BlackElo", getBlackPlayer().getElo() + ""));
sb.append(makeProp("BlackElo", String.valueOf(getBlackPlayer().getElo())));
}
if (getProperty() != null) {
for (Entry<String, String> entry : getProperty().entrySet()) {
Expand Down Expand Up @@ -749,7 +750,7 @@ public void loadMoveText(StringBuilder moveText) throws Exception {

String text = moveText.toString();

if (getFen() != null && !getFen().trim().equals("")) {
if (StringUtils.isNotBlank(getFen())) {
setHalfMoves(new MoveList(getFen()));
} else {
setHalfMoves(new MoveList());
Expand All @@ -766,8 +767,8 @@ public void loadMoveText(StringBuilder moveText) throws Exception {
boolean onCommentBlock = false;
boolean onVariationBlock = false;
boolean onLineCommentBlock = false;
for (String token : text.split(" ")) {
if (token == null || token.trim().equals("")) {
for (String token : text.split(StringUtils.SPACE)) {
if (StringUtils.isBlank(token)) {
continue;
}
if (!(onLineCommentBlock || onCommentBlock) &&
Expand Down Expand Up @@ -868,15 +869,15 @@ public void loadMoveText(StringBuilder moveText) throws Exception {
if (onCommentBlock || onLineCommentBlock) {
if (comment != null) {
comment.append(token);
comment.append(" ");
comment.append(StringUtils.SPACE);
}
continue;
}

if (onVariationBlock) {
if (variation != null) {
variation.getLast().text.append(token);
variation.getLast().text.append(" ");
variation.getLast().text.append(StringUtils.SPACE);
variation.getLast().size++;
variantIndex++;
}
Expand All @@ -885,7 +886,7 @@ public void loadMoveText(StringBuilder moveText) throws Exception {
variantIndex++;
halfMove++;
moves.append(token);
moves.append(" ");
moves.append(StringUtils.SPACE);
}

StringUtil.replaceAll(moves, "\n", " ");
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/github/bhlangonijr/chesslib/move/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.bhlangonijr.chesslib.Piece;
import com.github.bhlangonijr.chesslib.Side;
import com.github.bhlangonijr.chesslib.Square;
import org.apache.commons.lang3.StringUtils;

/**
* The definition of a chess move, that is, a piece movement from its starting square (the origin square) to a
Expand Down Expand Up @@ -143,7 +144,7 @@ public int hashCode() {
*/
@Override
public String toString() {
String promo = "";
String promo = StringUtils.EMPTY;
if (!Piece.NONE.equals(promotion)) {
promo = promotion.getFenSymbol();
}
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/com/github/bhlangonijr/chesslib/move/MoveList.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.github.bhlangonijr.chesslib.Side;
import com.github.bhlangonijr.chesslib.Square;
import com.github.bhlangonijr.chesslib.util.StringUtil;
import org.apache.commons.lang3.StringUtils;

/**
* A convenient data structure to store an ordered sequence of moves and access to their human-readable representation
Expand Down Expand Up @@ -389,7 +390,7 @@ private String toStringWithoutMoveNumbers(String[] moveArray) throws MoveConvers
StringBuilder sb = new StringBuilder();
for (String move : moveArray) {
sb.append(move);
sb.append(" ");
sb.append(StringUtils.SPACE);
}
return sb.toString();
}
Expand All @@ -400,7 +401,7 @@ private String toStringWithMoveNumbers(String[] moveArray) throws MoveConversion
if (halfMove % 2 == 0) {
sb.append((halfMove / 2) + 1).append(". ");
}
sb.append(moveArray[halfMove]).append(" ");
sb.append(moveArray[halfMove]).append(StringUtils.SPACE);
}
return sb.toString();
}
Expand Down Expand Up @@ -485,7 +486,7 @@ public synchronized void loadFromText(String text) throws MoveConversionExceptio
try {
Side side = b.getSideToMove();
text = StringUtil.normalize(text);
String[] m = text.split(" ");
String[] m = text.split(StringUtils.SPACE);
int i = 0;
for (String strMove : m) {
Move move = new Move(strMove, side);
Expand Down Expand Up @@ -561,7 +562,7 @@ public void loadFromSan(String text) throws MoveConversionException {
}
try {
text = StringUtil.normalize(text);
String[] m = text.split(" ");
String[] m = text.split(StringUtils.SPACE);
for (String strMove : m) {
if (strMove.startsWith("$")) {
continue;
Expand All @@ -572,7 +573,7 @@ public void loadFromSan(String text) throws MoveConversionException {
if (strMove.contains(".")) {
strMove = StringUtil.afterSequence(strMove, ".");
}
if (strMove.trim().equals("")) {
if (StringUtils.isBlank(strMove)) {
continue;
}
addSanMove(strMove);
Expand Down Expand Up @@ -609,7 +610,7 @@ protected Move decodeSan(Board board, String san, Side side) throws MoveConversi
//FIX missing equal sign for pawn promotions
if (Character.isLetter(lastChar) && Character.toUpperCase(lastChar) != 'O') {
san = san.substring(0, san.length() - 1);
strPromotion = lastChar + "";
strPromotion = String.valueOf(lastChar);
}

if (san.equals("O-O") || san.equals("O-O-O")) { // is castle
Expand All @@ -634,9 +635,8 @@ protected Move decodeSan(Board board, String san, Side side) throws MoveConversi
throw new MoveConversionException("Couldn't parse destination square[" + san + "]: " +
san.toUpperCase());
}
Piece promotion = strPromotion.equals("") ? Piece.NONE :
Piece.fromFenSymbol(side.equals(Side.WHITE) ?
strPromotion.toUpperCase() : strPromotion.toLowerCase());
Piece promotion = StringUtils.isEmpty(strPromotion) ? Piece.NONE :
Piece.fromFenSymbol(side.equals(Side.WHITE) ? strPromotion.toUpperCase() : strPromotion.toLowerCase());

if (san.length() == 2) { //is pawn move
long mask = Bitboard.getBbtable(to) - 1L;
Expand All @@ -661,13 +661,13 @@ protected Move decodeSan(Board board, String san, Side side) throws MoveConversi
PieceType fromPiece = PieceType.PAWN;

if (Character.isUpperCase(strFrom.charAt(0))) {
fromPiece = PieceType.fromSanSymbol(strFrom.charAt(0) + "");
fromPiece = PieceType.fromSanSymbol(String.valueOf(strFrom.charAt(0)));
}

if (strFrom.length() == 3) {
from = Square.valueOf(strFrom.substring(1, 3).toUpperCase());
} else {
String location = "";
String location = StringUtils.EMPTY;
if (strFrom.length() == 2) {
if (Character.isUpperCase(strFrom.charAt(0))) {
location = strFrom.substring(1, 2);
Expand Down Expand Up @@ -805,7 +805,7 @@ public String toString() {
StringBuilder b = new StringBuilder();
for (Move move : this) {
b.append(move.toString());
b.append(" ");
b.append(StringUtils.SPACE);
}
return b.toString().trim();
}
Expand Down Expand Up @@ -846,12 +846,12 @@ public boolean equals(Object obj) {
return false;
}

private String normalizeSan(String san) {
return san.replace("+", "")
.replace("#", "")
.replace("!", "")
.replace("?", "")
.replace("ep", "")
.replace("\n", " ");
private String normalizeSan(String san) { //TODO regex?
return san.replace("+", StringUtils.EMPTY)
.replace("#", StringUtils.EMPTY)
.replace("!", StringUtils.EMPTY)
.replace("?", StringUtils.EMPTY)
.replace("ep", StringUtils.EMPTY)
.replace("\n", StringUtils.SPACE);
}
}
Loading

0 comments on commit 4df8d6f

Please sign in to comment.