Skip to content

Commit

Permalink
resolve errors reported by Checkstyle 10.18.2
Browse files Browse the repository at this point in the history
  • Loading branch information
homebeaver committed Jan 4, 2025
1 parent a2e0117 commit 9df5404
Show file tree
Hide file tree
Showing 26 changed files with 178 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class SireneValidator {
final Validator formatValidator;

private static final Validator DEFAULT_FORMAT =
new Validator( new String[]
new Validator(new String[]
{ "^(\\d{9})$" // SIREN
, "^(\\d{14})$" // SIRET
});
Expand Down Expand Up @@ -110,7 +110,7 @@ public boolean isValid(final String code) {
if (GenericValidator.isBlankOrNull(code)) {
return false;
}
String id = code.trim();
final String id = code.trim();
if (id.length() != SIREN_CODE_LEN && id.length() != SIRET_CODE_LEN) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected int calculateModulus(final String code, final boolean includesCheckDig
sum = sum % MODULUS_10;
product = 2 * (sum == 0 ? MODULUS_10 : sum) % MODULUS_11;
}
int pruefZiffer = MODULUS_11 - product;
final int pruefZiffer = MODULUS_11 - product;
return pruefZiffer == MODULUS_10 ? 0 : pruefZiffer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public String calculate(final String code) throws CheckDigitException {
if (mr > MAX) {
mr = mr % modulus;
}
int modulusResult = (int) (mr % modulus);
final int modulusResult = (int) (mr % modulus);
if (modulusResult == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
}
Expand All @@ -122,11 +122,11 @@ public String calculate(final String code) throws CheckDigitException {
*/
protected long calculateModulus(final String code, final boolean removeCheckDigit) throws CheckDigitException {
// reformattedCode with check digit removed
String reformattedCode = removeCheckDigit ? code.substring(0, code.length() - CHECKDIGIT_LEN) : code;
final String reformattedCode = removeCheckDigit ? code.substring(0, code.length() - CHECKDIGIT_LEN) : code;
long total = 0;
for (int i = 0; i < reformattedCode.length(); i++) {
char c = reformattedCode.charAt(i);
int charValue = Character.getNumericValue(c);
final char c = reformattedCode.charAt(i);
final int charValue = Character.getNumericValue(c);
if (charValue < 0 || charValue > MAX_ALPHANUMERIC_VALUE) {
throw new CheckDigitException("Invalid Character[" + i + "] = '" + c + "'");
}
Expand All @@ -149,14 +149,14 @@ public boolean isValid(final String code) {
if (code.length() < MIN_CODE_LEN) {
return false;
}
String check = code.substring(code.length() - CHECKDIGIT_LEN);
Integer icheck = GenericTypeValidator.formatInt(check);
final String check = code.substring(code.length() - CHECKDIGIT_LEN);
final Integer icheck = GenericTypeValidator.formatInt(check);
if (icheck == null) {
return false;
}
try {
long mr = calculateModulus(code, false);
int modulusResult = (int) (mr % modulus);
final long mr = calculateModulus(code, false);
final int modulusResult = (int) (mr % modulus);
return modulusResult == 1;
} catch (CheckDigitException ex) {
return false;
Expand All @@ -179,7 +179,7 @@ public static String toCheckDigit(final int cdValue) throws CheckDigitException
if (cdValue > 99) { // CHECKSTYLE IGNORE MagicNumber
throw new CheckDigitException("Invalid Check Digit Value =" + cdValue);
}
String checkDigit = Integer.toString(cdValue);
final String checkDigit = Integer.toString(cdValue);
return cdValue > 9 ? checkDigit : "0" + checkDigit; // CHECKSTYLE IGNORE MagicNumber
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ private TidDECheckDigit() {
protected int calculateModulus(final String code, final boolean includesCheckDigit) throws CheckDigitException {
int product = MODULUS_10;
int sum = 0;
int[] anzahl = new int[10]; // CHECKSTYLE IGNORE MagicNumber
int[] fstpos = new int[10]; // CHECKSTYLE IGNORE MagicNumber
final int[] anzahl = new int[10]; // CHECKSTYLE IGNORE MagicNumber
final int[] fstpos = new int[10]; // CHECKSTYLE IGNORE MagicNumber
for (int i = 0; i < code.length() - (includesCheckDigit ? 1 : 0); i++) {
final int leftPos = i + 1;
final int rightPos = -1; // rightPos param not used
Expand Down Expand Up @@ -104,14 +104,14 @@ protected int calculateModulus(final String code, final boolean includesCheckDig
LOG.warn(code + ": mehrere dreifach");
throw new CheckDigitException("Invalid code, mehrere dreifach");
} else if (dreifach == 1) {
int i = code.indexOf("" + dreifachz);
final int i = code.indexOf("" + dreifachz);
if (dreifachz == toInt(code.charAt(i + 1), i + 1, -1)
&& dreifachz == toInt(code.charAt(i + 2), i + 2, -1)) {
LOG.warn(code + ": dreifach direkt hintereinander Ziffer:" + dreifachz);
throw new CheckDigitException("Invalid code, dreifach direkt hintereinander");
}
}
int pruefZiffer = MODULUS_11 - product;
final int pruefZiffer = MODULUS_11 - product;
return pruefZiffer == MODULUS_10 ? 0 : pruefZiffer;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ private VATidATCheckDigit() {
*/
@Override
protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
if (leftPos >= LEN) return 0;
if (leftPos >= LEN) {
return 0;
}
if ((leftPos - 1) % 2 == 0) {
return charValue;
} else {
int i = charValue / 5; // CHECKSTYLE IGNORE MagicNumber
final int i = charValue / 5; // CHECKSTYLE IGNORE MagicNumber
return i + charValue * 2 % MODULUS_10;
}
}
Expand All @@ -93,7 +95,7 @@ public String calculate(final String code) throws CheckDigitException {
throw new CheckDigitException(CheckDigitException.MISSING_CODE);
}
// need this for testZeroSum():
Long l = GenericTypeValidator.formatLong(code);
final Long l = GenericTypeValidator.formatLong(code);
if (l != null && l == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ public String calculate(final String code) throws CheckDigitException {
if (GenericValidator.isBlankOrNull(code)) {
throw new CheckDigitException(CheckDigitException.MISSING_CODE);
}
if (code.length() < MIN_CODE_LEN) {
throw new CheckDigitException("Invalid Code length=" + code.length());
}

long mr = calculateModulus(code, false);
int modulusResult = (int) (mr % MODULUS_97);
if (modulusResult == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
try {
final long l = Long.parseLong(code); // throws NumberFormatException
if (l == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
}
final int r = (int) (l % MODULUS_97); // MOD 97 reminder
return toCheckDigit(MODULUS_97 - r);
} catch (final NumberFormatException ex) {
// Expected exception for high codes f.i. 99999999999999999999999
throw new CheckDigitException("Invalid Code " + code);
}
// The check digits are calculated as 97 - MOD 97
return toCheckDigit(MODULUS_97 - modulusResult);
}

/**
Expand All @@ -90,22 +90,13 @@ public boolean isValid(final String code) {
if (GenericValidator.isBlankOrNull(code)) {
return false;
}
if (code.length() < MIN_CODE_LEN) {
return false;
}
String check = code.substring(code.length() - CHECKDIGIT_LEN);
Integer icheck = GenericTypeValidator.formatInt(check);
if (icheck == null) {
if (code.length() <= CHECKDIGIT_LEN) {
return false;
}
final String check = code.substring(code.length() - CHECKDIGIT_LEN);
try {
long mr = calculateModulus(code, true);
int modulusResult = (int) (mr % MODULUS_97);
if (modulusResult == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
}
final int cdValue = MODULUS_97 - modulusResult;
return icheck.intValue() == cdValue;
final String cd = calculate(code.substring(0, code.length() - CHECKDIGIT_LEN)); // throws CheckDigitException
return cd.equals(check);
} catch (final CheckDigitException ex) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ private boolean checkCivilNumber(final String code) throws CheckDigitException {
mmborn = mm - BORN_AFTER_2000_MOD;
}
}
DateValidator dateValidator = new DateValidator();
String date = String.format("%02d", mmborn) + "/" + code.substring(4, 6) + "/" + yyborn; // CHECKSTYLE IGNORE MagicNumber
final DateValidator dateValidator = new DateValidator();
final String date = String.format("%02d", mmborn) + "/" + code.substring(4, 6) + "/" + yyborn; // CHECKSTYLE IGNORE MagicNumber
if (dateValidator.validate(date, "MM/dd/yyyy") == null) {
throw new CheckDigitException("Invalid date " + date + " - Invalid DDC " + code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ private int calculateRodneCislo(final String code, final boolean includesCheckDi
final String sex = mm > FEMALE_MOD ? "female" : "male";
LOG.debug(code + ": individual (" + sex + ") born=" + yyborn + "/" + mmborn + "/" + ddborn);
}
DateValidator dateValidator = new DateValidator();
String date = String.format("%02d", mmborn) + "/" + String.format("%02d", ddborn) + "/" + yyborn;
final DateValidator dateValidator = new DateValidator();
final String date = String.format("%02d", mmborn) + "/" + String.format("%02d", ddborn) + "/" + yyborn;
if (dateValidator.validate(date, "MM/dd/yyyy") == null) {
throw new CheckDigitException("Invalid date " + date + " - invalid Rodné číslo (RČ) " + code);
}
Expand Down Expand Up @@ -190,20 +190,29 @@ public boolean isValid(final String code) {
if (GenericValidator.isBlankOrNull(code)) {
return false;
}
if (code.length() == LEN10ICO) try {
int cd = calculateRodneCislo(code, true);
return cd == Character.getNumericValue(code.charAt(code.length() - 1));
} catch (final CheckDigitException ex) {
return false;

if (code.length() == LEN10ICO) {
try {
int cd = calculateRodneCislo(code, true);
return cd == Character.getNumericValue(code.charAt(code.length() - 1));
} catch (final CheckDigitException ex) {
return false;
}
}
if (code.length() == LEN9ICO) try {
final int modulusResult = calculateModulus6(code, true);
final int charValue = modulusResult == 0 ? MODULUS_11 : (MODULUS_11 - modulusResult);
return DIFFTABLE[charValue - 1] == Character.getNumericValue(code.charAt(code.length() - 1));
} catch (final CheckDigitException ex) {
if (code.length() == LEN9ICO) {
try {
final int modulusResult = calculateModulus6(code, true);
final int charValue = modulusResult == 0 ? MODULUS_11 : (MODULUS_11 - modulusResult);
return DIFFTABLE[charValue - 1] == Character.getNumericValue(code.charAt(code.length() - 1));
} catch (final CheckDigitException ex) {
return false;
}
}

if (code.length() > LEN) {
return false;
}
if (code.length() <= LEN) try {
try {
if (code.startsWith(INVALID_START_WITH)) {
throw new CheckDigitException(INVALID_START_MSG + code);
}
Expand All @@ -212,7 +221,6 @@ public boolean isValid(final String code) {
} catch (final CheckDigitException ex) {
return false;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ private VATidELCheckDigit() {
*/
@Override
protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
if (leftPos >= LEN) return 0;
if (leftPos >= LEN) {
return 0;
}
final int weight = (int) Math.pow(2, rightPos - 1);
return charValue * weight;
}
Expand All @@ -78,7 +80,11 @@ public String calculate(final String code) throws CheckDigitException {
if (GenericValidator.isBlankOrNull(code)) {
throw new CheckDigitException(CheckDigitException.MISSING_CODE);
}
if (GenericTypeValidator.formatLong(code) == 0) {
final Long l = GenericTypeValidator.formatLong(code);
if (l == null) {
throw new CheckDigitException("Invalid VAT number " + code);
}
if (l == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
}
final int cm = INSTANCE.calculateModulus(code, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,14 @@ private int calculateLuhn(final String code) throws CheckDigitException {
}

private char calculateNIFletter(final String code) throws CheckDigitException {
long value = GenericTypeValidator.formatLong(code);
if (GenericTypeValidator.formatLong(code) == 0) {
final Long l = GenericTypeValidator.formatLong(code);
if (l == null) {
throw new CheckDigitException("Invalid VAT number " + code);
}
if (l == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
}
return NIF_LETTER.charAt((int) (value % MODULUS_23));
return NIF_LETTER.charAt((int) (l % MODULUS_23));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ private VATidFICheckDigit() {
*/
@Override
protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
if (leftPos - 1 >= POSITION_WEIGHT.length) return 0;
if (leftPos - 1 >= POSITION_WEIGHT.length) {
return 0;
}
final int weight = POSITION_WEIGHT[(leftPos - 1)];
return charValue * weight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ private VATidFRCheckDigit() {
*/
@Override
protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
if (rightPos > LEN - CHECKDIGIT_LEN) return 0;
Double pow = Math.pow(10, rightPos + 1 - CHECKDIGIT_LEN); // CHECKSTYLE IGNORE MagicNumber
if (rightPos > LEN - CHECKDIGIT_LEN) {
return 0;
}
final Double pow = Math.pow(10, rightPos + 1 - CHECKDIGIT_LEN); // CHECKSTYLE IGNORE MagicNumber
return charValue * pow.intValue();
}

Expand All @@ -90,7 +92,7 @@ public String calculate(final String code) throws CheckDigitException {
if (GenericValidator.isBlankOrNull(code)) {
throw new CheckDigitException(CheckDigitException.MISSING_CODE);
}
Long checkZero = GenericTypeValidator.formatLong(code);
final Long checkZero = GenericTypeValidator.formatLong(code);
if (checkZero != null && checkZero == 0) {
throw new CheckDigitException(CheckDigitException.ZERO_SUM);
}
Expand All @@ -112,16 +114,16 @@ public String calculate(final String code) throws CheckDigitException {
* @throws CheckDigitException
*/
private int calcOldStyle(final String code) throws CheckDigitException {
Long cde12 = GenericTypeValidator.formatLong(code + "12");
final Long cde12 = GenericTypeValidator.formatLong(code + "12");
if (cde12 == null) {
throw new CheckDigitException("Invalid code, '" + code + "'");
}
return (int) (cde12 % MODULUS_97);
}

private boolean isValidOldStyle(final String code) throws CheckDigitException {
int cd = GenericTypeValidator.formatInt(code.substring(0, CHECKDIGIT_LEN));
Long cde = GenericTypeValidator.formatLong((code + "12").substring(CHECKDIGIT_LEN));
final int cd = GenericTypeValidator.formatInt(code.substring(0, CHECKDIGIT_LEN));
final Long cde = GenericTypeValidator.formatLong((code + "12").substring(CHECKDIGIT_LEN));
if (cde == null) {
throw new CheckDigitException("Invalid code " + code);
}
Expand All @@ -143,9 +145,9 @@ private boolean isValid(final String siren, final int s0, final int s1) {
if (s1 > -1 && s1 < 10 && s0 >= 10) { // CHECKSTYLE IGNORE MagicNumber
s = s0 * ALPHABET.length() + s1 - 100; // CHECKSTYLE IGNORE MagicNumber
}
int p = (s / MODULUS_11) + 1;
int r1 = s % MODULUS_11;
int r2 = (GenericTypeValidator.formatInt(siren) + p) % MODULUS_11;
final int p = (s / MODULUS_11) + 1;
final int r1 = s % MODULUS_11;
final int r2 = (GenericTypeValidator.formatInt(siren) + p) % MODULUS_11;
return r1 == r2;
}

Expand All @@ -172,10 +174,10 @@ public boolean isValid(final String code) {
// 2. c0 isDigit && c1 isUpperCase ==> new style
// 3. c0 isDigit && c1 isDigit ==> old style
// alle anderen sind nicht valide
int c0 = code.codePointAt(0);
int c1 = code.codePointAt(1);
int s0 = ALPHABET.indexOf(c0);
int s1 = ALPHABET.indexOf(c1);
final int c0 = code.codePointAt(0);
final int c1 = code.codePointAt(1);
final int s0 = ALPHABET.indexOf(c0);
final int s1 = ALPHABET.indexOf(c1);
if (Character.isUpperCase(c0) && Character.isDigit(c1)) {
return isValid(code.substring(CHECKDIGIT_LEN), s0, s1);
} else if (Character.isDigit(c0) && Character.isUpperCase(c1)) {
Expand Down
Loading

0 comments on commit 9df5404

Please sign in to comment.