View Javadoc

1   /***********************************************************************************************************************
2    * Copyright (c) 2004, International Barcode Consortium
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this list of
9    * conditions and the following disclaimer.
10   * Redistributions in binary form must reproduce the above copyright notice, this list of
11   * conditions and the following disclaimer in the documentation and/or other materials
12   * provided with the distribution.
13   * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse
14   * or promote products derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   ***********************************************************************************************************************/
26  
27  package net.sourceforge.barbecue;
28  
29  /**
30   * Modulo 10 Check Sum class.
31   *
32   * @author <a href="mailto:james@metalskin.com">James Jenner</a>
33   */
34  public class Modulo10 {
35      
36      /* no public access */
37      private Modulo10() {
38      }
39      
40      /**
41       * Calculates a modulo 10 check digit based on the passed numeric data.
42       * The calculated check sum is determined using the weighted values, if
43       * there is a need for no weight to be applied against the odd and/or 
44       * even characters then use the value 1.
45       * If a non-numeric value is within the data then NumberFormatException 
46       * will be thrown.
47       * @param data The data to encode into a Modulo 10 check digit
48       * @param weightEven Every even digit will be multiplied by this value
49       * @param weightOdd Every odd digit will be multiplied by this value
50       * @param beginsEven true if the first numeric data is to be treated as even,
51       * false if the first digit is to be treated as odd
52       * @return the numeric value of the check digit calculated
53       * @see #getMod10CheckDigit(String, int, int)
54       * @see #getMod10CheckDigit(String, int)
55       */
56      public static int getMod10CheckDigit(String data, int weightEven, int weightOdd, boolean beginsEven) 
57        throws java.lang.NumberFormatException {
58          int sum = 0;
59          int len = data.length();
60          int value;
61          int compareValue = beginsEven ? 0 : 1;
62          int result = 0;
63          
64          for(int i = 0; i < len; i++) {
65              value = Integer.parseInt(String.valueOf(data.charAt(i)));
66              sum += (i % 2) == compareValue ? weightEven * value : weightOdd * value;
67          }
68  
69          result = 10 - (sum % 10);
70  
71          if(result == 10) {
72              result = 0;
73          }
74  
75          return result;
76      }
77  
78      /**
79       * Calculates a modulo 10 check digit based on the passed numeric data.
80       * The calculated check sum is determined using the weighted values, if
81       * there is a need for no weight to be applied against the odd and/or 
82       * even characters then use the value 1.
83       * If a non-numeric value is within the data then NumberFormatException 
84       * will be thrown.
85       * The first value of data is treated as even.
86       * @param data The data to encode into a Modulo 10 check digit
87       * @param weightEven Every even digit will be multiplied by this value
88       * @param weightOdd Every odd digit will be multiplied by this value
89       * false if the first digit is to be treated as odd
90       * @return the numeric value of the check digit calculated
91       * @see #getMod10CheckDigit(String, int)
92       */
93      public static int getMod10CheckDigit(String data, int weightEven, int weightOdd) 
94        throws java.lang.NumberFormatException {
95          return getMod10CheckDigit(data, weightEven, weightOdd, true);
96      }
97      
98      /**
99       * Calculates a modulo 10 check digit based on the passed numeric data.
100      * The only weight available is odd, this is a common usage.
101      * If a non-numeric value is within the data then NumberFormatException 
102      * will be thrown.
103      * The first value of data is treated as even.
104      * @param data The data to encode into a Modulo 10 check digit
105      * @param weightOdd Every odd digit will be multiplied by this value
106      * @return the numeric value of the check digit calculated
107      * @see #getMod10CheckDigit(String, int, int)
108      */
109     public static int getMod10CheckDigit(String data, int weightOdd) throws java.lang.NumberFormatException {
110         return getMod10CheckDigit(data, 1, weightOdd, true);
111     }
112 }