/** * 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; } }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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; } }
/** * 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; } }
/** * 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; } }
/** * 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); }
/** * 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 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 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 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 = CharacterGetNumericValue.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); }