Coverage Report - org.apache.commons.validator.routines.checkdigit.CUSIPCheckDigit
 
Classes in this File Line Coverage Branch Coverage Complexity
CUSIPCheckDigit
100%
11/11
75%
3/4
2
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.validator.routines.checkdigit;
 18  
 
 19  
 /**
 20  
  * Modulus 10 <b>CUSIP</b> (North American Securities)
 21  
  * Check Digit calculation/validation.
 22  
  * <p>
 23  
  * CUSIP Numbers are 9 character alphanumeric codes used
 24  
  * to identify North American Securities.
 25  
  * <p>
 26  
  * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
 27  
  * with every second digit being weighted by 2. Alphabetic characters are
 28  
  * converted to numbers by their position in the alphabet starting with A being 10.
 29  
  * Weighted numbers greater than ten are treated as two separate numbers.
 30  
  * <p>
 31  
  *
 32  
  * <p>
 33  
  * See <a href="http://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a>
 34  
  * for more details.
 35  
  *
 36  
  * @version $Revision: 1227719 $ $Date: 2012-01-05 12:45:51 -0500 (Thu, 05 Jan 2012) $
 37  
  * @since Validator 1.4
 38  
  */
 39  
 public final class CUSIPCheckDigit extends ModulusCheckDigit {
 40  
 
 41  
     private static final long serialVersionUID = 666941918490152456L;
 42  
 
 43  
     /** Singleton CUSIP Check Digit instance */
 44  1
     public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
 45  
 
 46  
     /** weighting given to digits depending on their right position */
 47  1
     private static final int[] POSITION_WEIGHT = new int[] {2, 1};
 48  
 
 49  
     /**
 50  
      * Construct an CUSIP Indetifier Check Digit routine.
 51  
      */
 52  
     public CUSIPCheckDigit() {
 53  1
         super(10);
 54  1
     }
 55  
 
 56  
     /**
 57  
      * Convert a character at a specified position to an integer value.
 58  
      *
 59  
      * @param character The character to convert
 60  
      * @param leftPos The position of the character in the code, counting from left to right
 61  
      * @param rightPos The positionof the character in the code, counting from right to left
 62  
      * @return The integer value of the character
 63  
      * @throws CheckDigitException if character is not alphanumeric
 64  
      */
 65  
     protected int toInt(char character, int leftPos, int rightPos)
 66  
             throws CheckDigitException {
 67  814
         int charValue = Character.getNumericValue(character);
 68  814
         if (charValue < 0 || charValue > 35) {
 69  2
             throw new CheckDigitException("Invalid Character[" +
 70  
                     leftPos + "] = '" + charValue + "'");
 71  
         }
 72  812
         return charValue;
 73  
     }
 74  
 
 75  
     /**
 76  
      * <p>Calculates the <i>weighted</i> value of a charcter in the
 77  
      * code at a specified position.</p>
 78  
      *
 79  
      * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
 80  
      * with a factor of <b>one</b> and <b>even</b> digits with a factor
 81  
      * of <b>two</b>. Weighted values > 9, have 9 subtracted</p>
 82  
      *
 83  
      * @param charValue The numeric value of the character.
 84  
      * @param leftPos The position of the character in the code, counting from left to right
 85  
      * @param rightPos The positionof the character in the code, counting from right to left
 86  
      * @return The weighted value of the character.
 87  
      */
 88  
     protected int weightedValue(int charValue, int leftPos, int rightPos) {
 89  812
         int weight = POSITION_WEIGHT[rightPos % 2];
 90  812
         int weightedValue = (charValue * weight);
 91  812
         return ModulusCheckDigit.sumDigits(weightedValue);
 92  
     }
 93  
 }