Coverage Report - org.apache.commons.validator.routines.checkdigit.CUSIPCheckDigit
 
Classes in this File Line Coverage Branch Coverage Complexity
CUSIPCheckDigit
100%
12/12
100%
6/6
2.333
 
 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) Check Digit calculation/validation.
 21  
  *
 22  
  * <p>
 23  
  * CUSIP Numbers are 9 character alphanumeric codes used
 24  
  * to identify North American Securities.
 25  
  * </p>
 26  
  *
 27  
  * <p>
 28  
  * Check digit calculation uses the <i>Modulus 10 Double Add Double</i> technique
 29  
  * with every second digit being weighted by 2. Alphabetic characters are
 30  
  * converted to numbers by their position in the alphabet starting with A being 10.
 31  
  * Weighted numbers greater than ten are treated as two separate numbers.
 32  
  * </p>
 33  
  *
 34  
  * <p>
 35  
  * See <a href="http://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a>
 36  
  * for more details.
 37  
  * </p>
 38  
  *
 39  
  * @version $Revision: 1739356 $
 40  
  * @since Validator 1.4
 41  
  */
 42  
 public final class CUSIPCheckDigit extends ModulusCheckDigit {
 43  
 
 44  
     private static final long serialVersionUID = 666941918490152456L;
 45  
 
 46  
     /** Singleton CUSIP Check Digit instance */
 47  1
     public static final CheckDigit CUSIP_CHECK_DIGIT = new CUSIPCheckDigit();
 48  
 
 49  
     /** weighting given to digits depending on their right position */
 50  1
     private static final int[] POSITION_WEIGHT = new int[] {2, 1};
 51  
 
 52  
     /**
 53  
      * Construct an CUSIP Indetifier Check Digit routine.
 54  
      */
 55  
     public CUSIPCheckDigit() {
 56  1
         super(10); // CHECKSTYLE IGNORE MagicNumber
 57  1
     }
 58  
 
 59  
     /**
 60  
      * Convert a character at a specified position to an integer value.
 61  
      *
 62  
      * @param character The character to convert
 63  
      * @param leftPos The position of the character in the code, counting from left to right
 64  
      * @param rightPos The position of the character in the code, counting from right to left
 65  
      * @return The integer value of the character
 66  
      * @throws CheckDigitException if character is not alphanumeric
 67  
      */
 68  
     @Override
 69  
     protected int toInt(char character, int leftPos, int rightPos)
 70  
             throws CheckDigitException {
 71  5531
         int charValue = Character.getNumericValue(character);
 72  
         // the final character is only allowed to reach 9
 73  5531
         final int charMax = rightPos == 1 ? 9 : 35;  // CHECKSTYLE IGNORE MagicNumber
 74  5531
         if (charValue < 0 || charValue > charMax) {
 75  525
             throw new CheckDigitException("Invalid Character[" +
 76  
                     leftPos + "," + rightPos + "] = '" + charValue + "' out of range 0 to " + charMax);
 77  
         }
 78  5006
         return charValue;
 79  
     }
 80  
 
 81  
     /**
 82  
      * <p>Calculates the <i>weighted</i> value of a charcter in the
 83  
      * code at a specified position.</p>
 84  
      *
 85  
      * <p>For CUSIP (from right to left) <b>odd</b> digits are weighted
 86  
      * with a factor of <b>one</b> and <b>even</b> digits with a factor
 87  
      * of <b>two</b>. Weighted values &gt; 9, have 9 subtracted</p>
 88  
      *
 89  
      * @param charValue The numeric value of the character.
 90  
      * @param leftPos The position of the character in the code, counting from left to right
 91  
      * @param rightPos The positionof the character in the code, counting from right to left
 92  
      * @return The weighted value of the character.
 93  
      */
 94  
     @Override
 95  
     protected int weightedValue(int charValue, int leftPos, int rightPos) {
 96  5006
         int weight = POSITION_WEIGHT[rightPos % 2];
 97  5006
         int weightedValue = (charValue * weight);
 98  5006
         return ModulusCheckDigit.sumDigits(weightedValue);
 99  
     }
 100  
 }