/** * Calculate a modulus <i>Check Digit</i> for a code which does not yet have one. * * @param code The code for which to calculate the Check Digit; * the check digit should not be included * @return The calculated Check Digit * @throws CheckDigitException if an error occurs calculating the check digit */ @Override public String calculate(String code) throws CheckDigitException { if (code == null || code.length() == 0) { throw new CheckDigitException("Code is missing"); } int modulusResult = calculateModulus(code, false); int charValue = (modulus - modulusResult) % modulus; return toCheckDigit(charValue); }
/** * Calculate the modulus for a code. * * @param code The code to calculate the modulus for. * @param includesCheckDigit Whether the code includes the Check Digit or not. * @return The modulus value * @throws CheckDigitException if an error occurs calculating the modulus * for the specified code */ protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException { int total = 0; for (int i = 0; i < code.length(); i++) { int lth = code.length() + (includesCheckDigit ? 0 : 1); int leftPos = i + 1; int rightPos = lth - i; int charValue = toInt(code.charAt(i), leftPos, rightPos); total += weightedValue(charValue, leftPos, rightPos); } if (total == 0) { throw new CheckDigitException("Invalid code, sum is zero"); } return total % modulus; }
/** * Validate a modulus check digit for a code. * <p> * Note: assumes last digit is the check digit * * @param code The code to validate * @return <code>true</code> if the check digit is valid, otherwise * <code>false</code> */ @Override public boolean isValid(String code) { if (code == null || code.length() == 0) { return false; } if (!Character.isDigit(code.charAt(code.length() - 1))) { return false; } return super.isValid(code); }
/** * Validate a modulus check digit for a code. * * @param code The code to validate * @return <code>true</code> if the check digit is valid, otherwise * <code>false</code> */ @Override public boolean isValid(String code) { if (code == null || code.length() == 0) { return false; } try { int modulusResult = calculateModulus(code, true); return (modulusResult == 0); } catch (CheckDigitException ex) { return false; } }
@Override protected String toCheckDigit(int charValue) throws CheckDigitException { if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber return "X"; } return super.toCheckDigit(charValue); }
@Override protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException { if (rightPos == 1 && character == 'X') { return 10; // CHECKSTYLE IGNORE MagicNumber } return super.toInt(character, leftPos, rightPos); } }
/** * Calculates the <i>weighted</i> value of a character in the code at a * specified position. * * @param charValue The numeric value of the character. * @param leftPos The position of the character in the code, counting from * left to right * @param rightPos The position of the character in the code, counting from * right to left * @return The weighted value of the character. */ @Override protected int weightedValue(int charValue, int leftPos, int rightPos) { int pos = useRightPos ? rightPos : leftPos; int weight = postitionWeight[(pos - 1) % postitionWeight.length]; int weightedValue = charValue * weight; if (sumWeightedDigits) { weightedValue = ModulusCheckDigit.sumDigits(weightedValue); } return weightedValue; }
/** * Calculate the modulus for an SEDOL code. * * @param code The code to calculate the modulus for. * @param includesCheckDigit Whether the code includes the Check Digit or not. * @return The modulus value * @throws CheckDigitException if an error occurs calculating the modulus * for the specified code */ @Override protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException { if (code.length() > POSITION_WEIGHT.length) { throw new CheckDigitException("Invalid Code Length = " + code.length()); } return super.calculateModulus(code, includesCheckDigit); }
/** * <p>Convert an integer value to a character at a specified position.</p> * * <p>Value '10' for position 1 (check digit) converted to 'X'.</p> * * @param charValue The integer value of the character. * @return The converted character. * @throws CheckDigitException if an error occurs. */ @Override protected String toCheckDigit(int charValue) throws CheckDigitException { if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber return "X"; } return super.toCheckDigit(charValue); }
/** * <p>Convert a character at a specified position to an * integer value.</p> * * <p>Character 'X' check digit converted to 10.</p> * * @param character The character to convert. * @param leftPos The position of the character in the code, counting from left to right * @param rightPos The position of the character in the code, counting from right to left * @return The integer value of the character. * @throws CheckDigitException if an error occurs. */ @Override protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException { if (rightPos == 1 && character == 'X') { return 10; // CHECKSTYLE IGNORE MagicNumber } return super.toInt(character, leftPos, rightPos); }
/** * <p>Calculates the <i>weighted</i> value of a charcter in the * code at a specified position.</p> * * <p>For Luhn (from right to left) <b>odd</b> digits are weighted * with a factor of <b>one</b> and <b>even</b> digits with a factor * of <b>two</b>. Weighted values > 9, have 9 subtracted</p> * * @param charValue The numeric value of the character. * @param leftPos The position of the character in the code, counting from left to right * @param rightPos The positionof the character in the code, counting from right to left * @return The weighted value of the character. */ @Override protected int weightedValue(int charValue, int leftPos, int rightPos) { int weight = POSITION_WEIGHT[rightPos % 2]; int weightedValue = (charValue * weight); return ModulusCheckDigit.sumDigits(weightedValue); } }
/** * Calculate a modulus <i>Check Digit</i> for a code which does not yet have one. * * @param code The code for which to calculate the Check Digit; * the check digit should not be included * @return The calculated Check Digit * @throws CheckDigitException if an error occurs calculating the check digit */ @Override public String calculate(String code) throws CheckDigitException { if (code == null || code.length() == 0) { throw new CheckDigitException("Code is missing"); } int modulusResult = calculateModulus(code, false); int charValue = (modulus - modulusResult) % modulus; return toCheckDigit(charValue); }
/** * Calculate the modulus for a code. * * @param code The code to calculate the modulus for. * @param includesCheckDigit Whether the code includes the Check Digit or not. * @return The modulus value * @throws CheckDigitException if an error occurs calculating the modulus * for the specified code */ protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException { int total = 0; for (int i = 0; i < code.length(); i++) { int lth = code.length() + (includesCheckDigit ? 0 : 1); int leftPos = i + 1; int rightPos = lth - i; int charValue = toInt(code.charAt(i), leftPos, rightPos); total += weightedValue(charValue, leftPos, rightPos); } if (total == 0) { throw new CheckDigitException("Invalid code, sum is zero"); } return total % modulus; }
/** * Calculate the modulus for an ISIN code. * * @param code The code to calculate the modulus for. * @param includesCheckDigit Whether the code includes the Check Digit or not. * @return The modulus value * @throws CheckDigitException if an error occurs calculating the modulus * for the specified code */ @Override protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException { StringBuilder transformed = new StringBuilder(code.length() * 2); if (includesCheckDigit) { char checkDigit = code.charAt(code.length()-1); // fetch the last character if (!Character.isDigit(checkDigit)){ throw new CheckDigitException("Invalid checkdigit["+ checkDigit+ "] in " + code); } } for (int i = 0; i < code.length(); i++) { int charValue = Character.getNumericValue(code.charAt(i)); if (charValue < 0 || charValue > MAX_ALPHANUMERIC_VALUE) { throw new CheckDigitException("Invalid Character[" + (i + 1) + "] = '" + charValue + "'"); } // this converts alphanumerics to two digits // so there is no need to overload toInt() transformed.append(charValue); } return super.calculateModulus(transformed.toString(), includesCheckDigit); }
@Override protected String toCheckDigit(int charValue) throws CheckDigitException { if (charValue == 10) { // CHECKSTYLE IGNORE MagicNumber return "X"; } return super.toCheckDigit(charValue); }
@Override protected int toInt(char character, int leftPos, int rightPos) throws CheckDigitException { if (rightPos == 1 && character == 'X') { return 10; // CHECKSTYLE IGNORE MagicNumber } return super.toInt(character, leftPos, rightPos); } }
/** * <p>Calculates the <i>weighted</i> value of a charcter in the * code at a specified position.</p> * * <p>For CUSIP (from right to left) <b>odd</b> digits are weighted * with a factor of <b>one</b> and <b>even</b> digits with a factor * of <b>two</b>. Weighted values > 9, have 9 subtracted</p> * * @param charValue The numeric value of the character. * @param leftPos The position of the character in the code, counting from left to right * @param rightPos The positionof the character in the code, counting from right to left * @return The weighted value of the character. */ @Override protected int weightedValue(int charValue, int leftPos, int rightPos) { int weight = POSITION_WEIGHT[rightPos % 2]; int weightedValue = (charValue * weight); return ModulusCheckDigit.sumDigits(weightedValue); } }
/** * Validate a modulus check digit for a code. * <p> * Note: assumes last digit is the check digit * * @param code The code to validate * @return <code>true</code> if the check digit is valid, otherwise * <code>false</code> */ @Override public boolean isValid(String code) { if (code == null || code.length() == 0) { return false; } if (!Character.isDigit(code.charAt(code.length() - 1))) { return false; } return super.isValid(code); }
/** * Calculate a modulus <i>Check Digit</i> for a code which does not yet have one. * * @param code The code for which to calculate the Check Digit; * the check digit should not be included * @return The calculated Check Digit * @throws CheckDigitException if an error occurs calculating the check digit */ @Override public String calculate(String code) throws CheckDigitException { if (code == null || code.length() == 0) { throw new CheckDigitException("Code is missing"); } int modulusResult = calculateModulus(code, false); int charValue = (modulus - modulusResult) % modulus; return toCheckDigit(charValue); }
/** * Calculate the modulus for a code. * * @param code The code to calculate the modulus for. * @param includesCheckDigit Whether the code includes the Check Digit or not. * @return The modulus value * @throws CheckDigitException if an error occurs calculating the modulus * for the specified code */ protected int calculateModulus(String code, boolean includesCheckDigit) throws CheckDigitException { int total = 0; for (int i = 0; i < code.length(); i++) { int lth = code.length() + (includesCheckDigit ? 0 : 1); int leftPos = i + 1; int rightPos = lth - i; int charValue = toInt(code.charAt(i), leftPos, rightPos); total += weightedValue(charValue, leftPos, rightPos); } if (total == 0) { throw new CheckDigitException("Invalid code, sum is zero"); } return total % modulus; }