Author: niallp
Date: Thu Feb 4 21:27:34 2010
New Revision: 906656
URL: http://svn.apache.org/viewvc?rev=906656&view=rev
Log:
VALIDATOR-250 Banking CheckDigit implementations: ABA, CUSIP, IBAN, ISIN and
Sedol
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigit.java
(with props)
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java
(with props)
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigit.java
(with props)
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigit.java
(with props)
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigit.java
(with props)
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigitTest.java
(with props)
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java
(with props)
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigitTest.java
(with props)
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java
(with props)
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigitTest.java
(with props)
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigit.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigit.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigit.java
(added)
+++
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigit.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,79 @@
+/*
+ * 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;
+
+import java.io.Serializable;
+
+/**
+ * Modulus 10 <b>ABA Number</b> (or <b>Routing Transit Number</b> (RTN)) Check
Digit
+ * calculation/validation.
+ * <p>
+ * ABA Numbers (or Routing Transit Numbers) are a nine digit numeric code used
+ * to identify American financial institutions for things such as checks or
deposits
+ * (ABA stands for the American Bankers Association).
+ * <p>
+ * Check digit calculation is based on <i>modulus 10</i> with digits being
weighted
+ * based on their position (from right to left) as follows:
+ * <ul>
+ * <li>Digits 1, 4 and & 7 are weighted 1
+ * <li>Digits 2, 5 and & 8 are weighted 7
+ * <li>Digits 3, 6 and & 9 are weighted 3
+ * </ul>
+ * <p>
+ * For further information see
+ * <a href="http://en.wikipedia.org/wiki/Routing_transit_number">Wikipedia -
+ * Routing transit number</a>.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class ABANumberCheckDigit extends ModulusCheckDigit implements
Serializable {
+
+ /** Singleton Routing Transit Number Check Digit instance */
+ public static final CheckDigit INSTANCE = new ABANumberCheckDigit();
+
+ /** weighting given to digits depending on their right position */
+ private static final int[] POSITION_WEIGHT = new int[] {3, 1, 7};
+
+ /**
+ * Construct a modulus 10 Check Digit routine for ABA Numbers.
+ */
+ public ABANumberCheckDigit() {
+ super(10);
+ }
+
+ /**
+ * Calculates the <i>weighted</i> value of a character in the
+ * code at a specified position.
+ * <p>
+ * ABA Routing numbers are weighted in the following manner:
+ * <pre><code>
+ * left position: 1 2 3 4 5 6 7 8 9
+ * weight: 3 7 1 3 7 1 3 7 1
+ * </code></pre>
+ *
+ * @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.
+ */
+ protected int weightedValue(int charValue, int leftPos, int rightPos) {
+ int weight = POSITION_WEIGHT[rightPos % 3];
+ return (charValue * weight);
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigit.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigit.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java
(added)
+++
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,94 @@
+/*
+ * 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;
+
+import java.io.Serializable;
+
+
+/**
+ * Modulus 10 <b>CUSIP</b> (North American Securities)
+ * Check Digit calculation/validation.
+ * <p>
+ * CUSIP Numbers are 9 character alphanumeric codes used
+ * to identify North American Securities.
+ * <p>
+ * Check digit calculation uses the <i>Modulus 10 Double Add Double</i>
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.
+ * <p>
+ *
+ * <p>
+ * See <a href="http://en.wikipedia.org/wiki/CUSIP">Wikipedia - CUSIP</a>
+ * for more details.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class CUSIPCheckDigit extends ModulusCheckDigit implements
Serializable {
+
+ /** Singleton CUSIP Check Digit instance */
+ public static final CheckDigit INSTANCE = 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);
+ }
+
+ /**
+ * 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 positionof the character in the code, counting from
right to left
+ * @return The integer value of the character
+ * @throws CheckDigitException if character is not alphanumeric
+ */
+ protected int toInt(char character, int leftPos, int rightPos)
+ throws CheckDigitException {
+ int charValue = Character.getNumericValue(character);
+ if (charValue < 0 || charValue > 35) {
+ throw new CheckDigitException("Invalid Character[" +
+ leftPos + "] = '" + charValue + "'");
+ }
+ return charValue;
+ }
+
+ /**
+ * <p>Calculates the <i>weighted</i> value of a charcter in the
+ * code at a specified position.</p>
+ *
+ * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
+ * with a factor of <b>one</b> and <b>even</b> digits with a factor
+ * of <b>two</b>. Weighted values > 9, have 9 subtracted</p>
+ *
+ * @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.
+ */
+ protected int weightedValue(int charValue, int leftPos, int rightPos) {
+ int weight = POSITION_WEIGHT[rightPos % 2];
+ int weightedValue = (charValue * weight);
+ return ModulusCheckDigit.sumDigits(weightedValue);
+ }
+}
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigit.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigit.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigit.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigit.java
(added)
+++
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigit.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,122 @@
+/*
+ * 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;
+
+import java.io.Serializable;
+
+/**
+ * <b>IBAN</b> (International Bank Account Number) Check Digit
calculation/validation.
+ * <p>
+ * This rountine is based on the ISO 7064 Mod 97,10 check digit caluclation
routine.
+ * <p>
+ * The two check digit characters in a IBAN number are the third and fourth
characters
+ * in the code. For <i>check digit</i> calculation/validation the first four
characters are moved
+ * to the end of the code.
+ * So <code>CCDDnnnnnnn</code> becomes <code>nnnnnnnCCDD</code> (where
+ * <code>CC</code> is the country code and <code>DD</code> is the check
digit). For
+ * check digit calcualtion the check digit value should be set to zero (i.e.
+ * <code>CC00nnnnnnn</code> in this example.
+ * <p>
+ * For further information see
+ * <a
href="http://en.wikipedia.org/wiki/International_Bank_Account_Number">Wikipedia
-
+ * IBAN number</a>.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class IBANCheckDigit implements CheckDigit, Serializable {
+
+ /** Singleton IBAN Number Check Digit instance */
+ public static final CheckDigit INSTANCE = new IBANCheckDigit();
+
+ private static final long MAX = 999999999;
+
+ private static final long MODULUS = 97;
+
+ /**
+ * Construct Check Digit routine for IBAN Numbers.
+ */
+ public IBANCheckDigit() {
+ }
+
+ /**
+ * Validate the check digit for an the IBAN code.
+ *
+ * @param code The code to validate
+ * @return <code>true</code> if the check digit is valid, otherwise
+ * <code>false</code>
+ */
+ public boolean isValid(String code) {
+ if (code == null || code.length() < 5) {
+ return false;
+ }
+ try {
+ int modulusResult = calculateModulus(code);
+ return (modulusResult == 1);
+ } catch (CheckDigitException ex) {
+ return false;
+ }
+ }
+
+ /**
+ * Calculate the <i>Check Digit</i> for an IBAN code.
+ * <p>
+ * <b>Note:</b> The check digit is the third and fourth
+ * characters and and should contain value "<code>00</code>".
+ *
+ * @param code The code to calculate the Check Digit for
+ * @return The calculated Check Digit
+ * @throws CheckDigitException if an error occurs calculating
+ * the check digit for the specified code
+ */
+ public String calculate(String code) throws CheckDigitException {
+ if (code == null || code.length() < 5) {
+ throw new CheckDigitException("Invalid Code length=" +
+ (code == null ? 0 : code.length()));
+ }
+ int modulusResult = calculateModulus(code);
+ int charValue = (98 - modulusResult);
+ String checkDigit = Integer.toString(charValue);
+ return (charValue > 9 ? checkDigit : "0" + checkDigit);
+ }
+
+ /**
+ * Calculate the modulus for a code.
+ *
+ * @param code The code to calculate the modulus for.
+ * @return The modulus value
+ * @throws CheckDigitException if an error occurs calculating the modulus
+ * for the specified code
+ */
+ private int calculateModulus(String code) throws CheckDigitException {
+ String reformattedCode = code.substring(4) + code.substring(0, 4);
+ long total = 0;
+ for (int i = 0; i < reformattedCode.length(); i++) {
+ int charValue =
Character.getNumericValue(reformattedCode.charAt(i));
+ if (charValue < 0 || charValue > 35) {
+ throw new CheckDigitException("Invalid Character[" +
+ i + "] = '" + charValue + "'");
+ }
+ total = (charValue > 9 ? total * 100 : total * 10) + charValue;
+ if (total > MAX) {
+ total = (total % MODULUS);
+ }
+ }
+ return (int)(total % MODULUS);
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigit.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigit.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigit.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigit.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigit.java
(added)
+++
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigit.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,95 @@
+/*
+ * 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;
+
+import java.io.Serializable;
+
+
+/**
+ * Modulus 10 <b>ISIN</b> (International Securities Identifying Number)
+ * Check Digit calculation/validation.
+ * <p>
+ * ISIN Numbers are 12 character alphanumeric codes used
+ * to identify Securities.
+ * <p>
+ * Check digit calculation uses the <i>Modulus 10 Double Add Double</i>
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.
+ * <p>
+ * See <a href="http://en.wikipedia.org/wiki/ISIN">Wikipedia - ISIN</a>
+ * for more details.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class ISINCheckDigit extends ModulusCheckDigit implements
Serializable {
+
+ /** Singleton ISIN Check Digit instance */
+ public static final CheckDigit INSTANCE = new ISINCheckDigit();
+
+ /** weighting given to digits depending on their right position */
+ private static final int[] POSITION_WEIGHT = new int[] {2, 1};
+
+ /**
+ * Construct an ISIN Indetifier Check Digit routine.
+ */
+ public ISINCheckDigit() {
+ super(10);
+ }
+
+ /**
+ * 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
+ */
+ protected int calculateModulus(String code, boolean includesCheckDigit)
throws CheckDigitException {
+ StringBuffer transformed = new StringBuffer(code.length() * 2);
+ for (int i = 0; i < code.length(); i++) {
+ int charValue = Character.getNumericValue(code.charAt(i));
+ if (charValue < 0 || charValue > 35) {
+ throw new CheckDigitException("Invalid Character[" +
+ (i + 1) + "] = '" + charValue + "'");
+ }
+ transformed.append(charValue);
+ }
+ return super.calculateModulus(transformed.toString(),
includesCheckDigit);
+ }
+
+ /**
+ * <p>Calculates the <i>weighted</i> value of a charcter in the
+ * code at a specified position.</p>
+ *
+ * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
+ * with a factor of <b>one</b> and <b>even</b> digits with a factor
+ * of <b>two</b>. Weighted values > 9, have 9 subtracted</p>
+ *
+ * @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.
+ */
+ protected int weightedValue(int charValue, int leftPos, int rightPos) {
+ int weight = POSITION_WEIGHT[rightPos % 2];
+ int weightedValue = (charValue * weight);
+ return ModulusCheckDigit.sumDigits(weightedValue);
+ }
+}
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigit.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigit.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigit.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigit.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigit.java
(added)
+++
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigit.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,104 @@
+/*
+ * 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;
+
+import java.io.Serializable;
+
+/**
+ * Modulus 10 <b>SEDOL</b> (UK Securities) Check Digit calculation/validation.
+ * <p>
+ * SEDOL Numbers are 7 character alphanumeric codes used
+ * to identify UK Securities (SEDOL stands for Stock Exchange Daily Official
List).
+ * <p>
+ * Check digit calculation is based on <i>modulus 10</i> with digits being
weighted
+ * based on their position, from left to right, as follows:
+ * <p>
+ * <pre><code>
+ * position: 1 2 3 4 5 6 7
+ * weighting: 1 3 1 7 3 9 1
+ * </code></pre>
+ * <p>
+ * See <a href="http://en.wikipedia.org/wiki/SEDOL">Wikipedia - SEDOL</a>
+ * for more details.
+ *
+ * @version $Revision$ $Date$
+ * @since Validator 1.4
+ */
+public final class SedolCheckDigit extends ModulusCheckDigit implements
Serializable {
+
+ /** Singleton ISBN-10 check digit instance */
+ public static final CheckDigit INSTANCE = new SedolCheckDigit();
+
+ /** weighting given to digits depending on their right position */
+ private static final int[] POSITION_WEIGHT = new int[] {1, 3, 1, 7, 3, 9,
1};
+
+ /**
+ * Construct a modulus 11 Check Digit routine for ISBN-10.
+ */
+ public SedolCheckDigit() {
+ super(10);
+ }
+
+ /**
+ * 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
+ */
+ protected int calculateModulus(String code, boolean includesCheckDigit)
throws CheckDigitException {
+ if (code.length() > 7) {
+ throw new CheckDigitException("Invalid Code Length = " +
code.length());
+ }
+ return super.calculateModulus(code, includesCheckDigit);
+ }
+
+ /**
+ * Calculates the <i>weighted</i> value of a charcter in the
+ * code at a specified position.
+ *
+ * @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.
+ */
+ protected int weightedValue(int charValue, int leftPos, int rightPos) {
+ return (charValue * POSITION_WEIGHT[leftPos - 1]);
+ }
+
+ /**
+ * 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 positionof the character in the code, counting from
right to left
+ * @return The integer value of the character
+ * @throws CheckDigitException if character is not alphanumeric
+ */
+ protected int toInt(char character, int leftPos, int rightPos)
+ throws CheckDigitException {
+ int charValue = Character.getNumericValue(character);
+ if (charValue < 0 || charValue > 35) {
+ throw new CheckDigitException("Invalid Character[" +
+ leftPos + "] = '" + charValue + "'");
+ }
+ return charValue;
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigit.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigit.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigitTest.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigitTest.java
(added)
+++
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigitTest.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,52 @@
+/*
+ * 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;
+
+
+/**
+ * ABA Number Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class ABANumberCheckDigitTest extends AbstractCheckDigitTest {
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public ABANumberCheckDigitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Set up routine & valid codes.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ routine = ABANumberCheckDigit.INSTANCE;
+ valid = new String[] {
+ "123456780",
+ "123123123",
+ "011000015",
+ "111000038",
+ "231381116",
+ "121181976"
+ };
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigitTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ABANumberCheckDigitTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java
(added)
+++
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,54 @@
+/*
+ * 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;
+
+
+/**
+ * CUSIP Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class CUSIPCheckDigitTest extends AbstractCheckDigitTest {
+
+ /**
+ * Construct a new test.
+ * @param name test name
+ */
+ public CUSIPCheckDigitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Set up routine & valid codes.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ routine = CUSIPCheckDigit.INSTANCE;
+ valid = new String[] {"037833100",
+ "931142103",
+ "837649128",
+ "392690QT3",
+ "594918104",
+ "86770G101",
+ "Y8295N109",
+ "G8572F100"
+ };
+ invalid = new String[] {"0378#3100"};
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/CUSIPCheckDigitTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigitTest.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigitTest.java
(added)
+++
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigitTest.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,139 @@
+/*
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * EAN-13 Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class IBANCheckDigitTest extends AbstractCheckDigitTest {
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public IBANCheckDigitTest(String name) {
+ super(name);
+ checkDigitLth = 2;
+ }
+
+ /**
+ * Set up routine & valid codes.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ routine = IBANCheckDigit.INSTANCE;
+ valid = new String[] {
+ "AD1200012030200359100100",
+ "AT611904300234573201",
+ "AT611904300234573201",
+ "BE68539007547034",
+ "BE62510007547061",
+ "CY17002001280000001200527600",
+ "CZ6508000000192000145399",
+ "DK5000400440116243",
+ "EE382200221020145685",
+ "FI2112345600000785",
+ "FR1420041010050500013M02606",
+ "DE89370400440532013000",
+ "GI75NWBK000000007099453",
+ "GR1601101250000000012300695",
+ "HU42117730161111101800000000",
+ "IS140159260076545510730339",
+ "IE29AIBK93115212345678",
+ "IT60X0542811101000000123456",
+ "LV80BANK0000435195001",
+ "LT121000011101001000",
+ "LU280019400644750000",
+ "NL91ABNA0417164300",
+ "NO9386011117947",
+ "PL27114020040000300201355387",
+ "PT50000201231234567890154",
+ "SK3112000000198742637541",
+ "SI56191000000123438",
+ "ES8023100001180000012345",
+ "SE3550000000054910000003",
+ "CH3900700115201849173",
+ "GB29NWBK60161331926819"
+ };
+ invalid = new String[] {"510007+47061BE63"};
+ zeroSum = null;
+ missingMessage = "Invalid Code length=0";
+
+ }
+
+ /**
+ * Test zero sum
+ */
+ public void testZeroSum() {
+ // ignore, don't run this test
+ }
+
+ /**
+ * Returns an array of codes with invalid check digits.
+ *
+ * @param codes Codes with valid check digits
+ * @return Codes with invalid check digits
+ */
+ protected String[] createInvalidCodes(String[] codes) {
+ List list = new ArrayList();
+
+ // create invalid check digit values
+ for (int i = 0; i < codes.length; i++) {
+ String code = removeCheckDigit(codes[i]);
+ String check = checkDigit(codes[i]);
+ for (int j = 0; j < 96; j++) {
+ String curr = j > 9 ? "" + j : "0" + j;
+ if (!curr.equals(check)) {
+ list.add(code.substring(0, 2) + curr + code.substring(4));
+ }
+ }
+ }
+
+ return (String[])list.toArray(new String[list.size()]);
+ }
+
+ /**
+ * Returns a code with the Check Digit (i.e. last character) removed.
+ *
+ * @param code The code
+ * @return The code without the check digit
+ */
+ protected String removeCheckDigit(String code) {
+ return code.substring(0, 2) + "00" + code.substring(4);
+ }
+
+ /**
+ * Returns the check digit (i.e. last character) for a code.
+ *
+ * @param code The code
+ * @return The check digit
+ */
+ protected String checkDigit(String code) {
+ if (code == null || code.length() <= checkDigitLth) {
+ return "";
+ }
+ return code.substring(2, 4);
+ }
+
+}
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigitTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/IBANCheckDigitTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java
(added)
+++
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,51 @@
+/*
+ * 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;
+
+
+/**
+ * ISIN Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class ISINCheckDigitTest extends AbstractCheckDigitTest {
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public ISINCheckDigitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Set up routine & valid codes.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ routine = ISINCheckDigit.INSTANCE;
+ valid = new String[] {"US0378331005",
+ "BMG8571G1096",
+ "AU0000XVGZA3",
+ "GB0002634946",
+ "FR0004026250",
+ "DK0009763344"
+ };
+ invalid = new String[] {"0378#3100"};
+ }
+}
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Added:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigitTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigitTest.java?rev=906656&view=auto
==============================================================================
---
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigitTest.java
(added)
+++
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigitTest.java
Thu Feb 4 21:27:34 2010
@@ -0,0 +1,52 @@
+/*
+ * 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;
+
+
+/**
+ * ISIN Check Digit Test.
+ *
+ * @version $Revision$
+ * @since Validator 1.4
+ */
+public class SedolCheckDigitTest extends AbstractCheckDigitTest {
+
+ /**
+ * Constructor
+ * @param name test name
+ */
+ public SedolCheckDigitTest(String name) {
+ super(name);
+ }
+
+ /**
+ * Set up routine & valid codes.
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ routine = SedolCheckDigit.INSTANCE;
+ valid = new String[] {
+ "0263494",
+ "0870612",
+ "B06LQ97",
+ "3437575",
+ "B07LF55",
+ };
+ invalid = new String[] {"123#567"};
+ zeroSum = "0000000";
+ }
+}
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigitTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/SedolCheckDigitTest.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL