org.apache.commons.validator.routines.checkdigit.CUSIPCheckDigit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-validator Show documentation
Show all versions of commons-validator Show documentation
Apache Commons Validator provides the building blocks for both client side validation and server side data validation.
It may be used standalone or with a framework like Struts.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.validator.routines.checkdigit;
/**
* Modulus 10 CUSIP (North American Securities) Check Digit calculation/validation.
*
*
* CUSIP Numbers are 9 character alphanumeric codes used
* to identify North American Securities.
*
*
*
* Check digit calculation uses the Modulus 10 Double Add Double technique
* with every second digit being weighted by 2. Alphabetic characters are
* converted to numbers by their position in the alphabet starting with A being 10.
* Weighted numbers greater than ten are treated as two separate numbers.
*
*
*
* See Wikipedia - CUSIP
* for more details.
*
*
* @version $Revision: 1739356 $
* @since Validator 1.4
*/
public final class CUSIPCheckDigit extends ModulusCheckDigit {
private static final long serialVersionUID = 666941918490152456L;
/** Singleton CUSIP Check Digit instance */
public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
/** weighting given to digits depending on their right position */
private static final int[] POSITION_WEIGHT = new int[] {2, 1};
/**
* Construct an CUSIP Indetifier Check Digit routine.
*/
public CUSIPCheckDigit() {
super(10); // CHECKSTYLE IGNORE MagicNumber
}
/**
* Convert a character at a specified position to an integer value.
*
* @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 character is not alphanumeric
*/
@Override
protected int toInt(char character, int leftPos, int rightPos)
throws CheckDigitException {
int charValue = Character.getNumericValue(character);
// the final character is only allowed to reach 9
final int charMax = rightPos == 1 ? 9 : 35; // CHECKSTYLE IGNORE MagicNumber
if (charValue < 0 || charValue > charMax) {
throw new CheckDigitException("Invalid Character[" +
leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
}
return charValue;
}
/**
* Calculates the weighted value of a charcter in the
* code at a specified position.
*
* For CUSIP (from right to left) odd digits are weighted
* with a factor of one and even digits with a factor
* of two. Weighted values > 9, have 9 subtracted
*
* @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);
}
}