This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-validator.git
commit bc6e998ef803f63b3b81bd5220cc7b541e511a0e Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sun Aug 25 10:45:43 2024 -0400 Sort members --- .../routines/checkdigit/CASNumberCheckDigit.java | 58 +++++++++++----------- .../routines/checkdigit/ECNumberCheckDigit.java | 50 +++++++++---------- .../checkdigit/CASNumberCheckDigitTest.java | 18 +++---- .../checkdigit/ECNumberCheckDigitTest.java | 18 +++---- 4 files changed, 72 insertions(+), 72 deletions(-) diff --git a/src/main/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigit.java b/src/main/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigit.java index a3d79b1b..5daa8069 100644 --- a/src/main/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigit.java +++ b/src/main/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigit.java @@ -54,56 +54,35 @@ public final class CASNumberCheckDigit extends ModulusCheckDigit { /** Singleton Check Digit instance */ private static final CASNumberCheckDigit INSTANCE = new CASNumberCheckDigit(); - /** - * Gets the singleton instance of this validator. - * @return A singleton instance of the CAS Number validator. - */ - public static CheckDigit getInstance() { - return INSTANCE; - } - /** * CAS number consists of 3 groups of numbers separated dashes (-). * First group has 2 to 7 digits. * Example: water is 7732-18-5 */ private static final String GROUP1 = "(\\d{2,7})"; + private static final String DASH = "(?:\\-)"; static final String CAS_REGEX = "^(?:" + GROUP1 + DASH + "(\\d{2})" + DASH + "(\\d))$"; - private static final int CAS_MIN_LEN = 4; // 9-99-9 LEN without SEP + /** maximum capacity of 1,000,000,000 == 9999999-99-9*/ private static final int CAS_MAX_LEN = 10; static final CodeValidator REGEX_VALIDATOR = new CodeValidator(CAS_REGEX, CAS_MIN_LEN, CAS_MAX_LEN, null); - /** Weighting given to digits depending on their right position */ private static final int[] POSITION_WEIGHT = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; /** - * Constructs a modulus 10 Check Digit routine for CAS Numbers. + * Gets the singleton instance of this validator. + * @return A singleton instance of the CAS Number validator. */ - private CASNumberCheckDigit() { + public static CheckDigit getInstance() { + return INSTANCE; } /** - * Calculates the <em>weighted</em> value of a character in the code at a specified position. - * <p> - * CAS numbers are weighted in the following manner: - * </p> - * <pre>{@code - * right position: 1 2 3 4 5 6 7 8 9 10 - * weight: 1 2 3 4 5 6 7 8 9 0 - * }</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. + * Constructs a modulus 10 Check Digit routine for CAS Numbers. */ - @Override - protected int weightedValue(final int charValue, final int leftPos, final int rightPos) { - final int weight = POSITION_WEIGHT[(rightPos - 1) % MODULUS_10]; - return charValue * weight; + private CASNumberCheckDigit() { } /** @@ -138,4 +117,25 @@ public final class CASNumberCheckDigit extends ModulusCheckDigit { } } + /** + * Calculates the <em>weighted</em> value of a character in the code at a specified position. + * <p> + * CAS numbers are weighted in the following manner: + * </p> + * <pre>{@code + * right position: 1 2 3 4 5 6 7 8 9 10 + * weight: 1 2 3 4 5 6 7 8 9 0 + * }</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. + */ + @Override + protected int weightedValue(final int charValue, final int leftPos, final int rightPos) { + final int weight = POSITION_WEIGHT[(rightPos - 1) % MODULUS_10]; + return charValue * weight; + } + } diff --git a/src/main/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigit.java b/src/main/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigit.java index 6ac2ccb4..a8212e40 100644 --- a/src/main/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigit.java +++ b/src/main/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigit.java @@ -47,46 +47,30 @@ public final class ECNumberCheckDigit extends ModulusCheckDigit { /** Singleton Check Digit instance */ private static final ECNumberCheckDigit INSTANCE = new ECNumberCheckDigit(); - /** - * Gets the singleton instance of this validator. - * @return A singleton instance of the EC Number validator. - */ - public static CheckDigit getInstance() { - return INSTANCE; - } - /** * EC number consists of 3 groups of numbers separated dashes (-). * Example: dexamethasone is 200-003-9 */ private static final String GROUP = "(\\d{3})"; + private static final String DASH = "(?:\\-)"; static final String EC_REGEX = "^(?:" + GROUP + DASH + GROUP + DASH + "(\\d))$"; - private static final int EC_LEN = 7; - static final CodeValidator REGEX_VALIDATOR = new CodeValidator(EC_REGEX, EC_LEN, null); + static final CodeValidator REGEX_VALIDATOR = new CodeValidator(EC_REGEX, EC_LEN, null); /** - * Constructs a modulus 11 Check Digit routine. + * Gets the singleton instance of this validator. + * @return A singleton instance of the EC Number validator. */ - private ECNumberCheckDigit() { - super(MODULUS_11); + public static CheckDigit getInstance() { + return INSTANCE; } /** - * Calculates the <em>weighted</em> value of a character in the - * code at a specified position. - * - * <p>For EC number digits are weighted by their position from left to right.</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. + * Constructs a modulus 11 Check Digit routine. */ - @Override - protected int weightedValue(final int charValue, final int leftPos, final int rightPos) { - return leftPos >= EC_LEN ? 0 : charValue * leftPos; + private ECNumberCheckDigit() { + super(MODULUS_11); } /** @@ -121,4 +105,20 @@ public final class ECNumberCheckDigit extends ModulusCheckDigit { } } + /** + * Calculates the <em>weighted</em> value of a character in the + * code at a specified position. + * + * <p>For EC number digits are weighted by their position from left to right.</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. + */ + @Override + protected int weightedValue(final int charValue, final int leftPos, final int rightPos) { + return leftPos >= EC_LEN ? 0 : charValue * leftPos; + } + } diff --git a/src/test/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigitTest.java b/src/test/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigitTest.java index 5eba9bda..c7391d99 100644 --- a/src/test/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigitTest.java +++ b/src/test/java/org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigitTest.java @@ -34,15 +34,6 @@ public class CASNumberCheckDigitTest extends AbstractCheckDigitTest { private static final String ASBESTOS = "1332-21-4"; private static final String MAX = "9999999-99-5"; // theoretical - /** - * Sets up routine & valid codes. - */ - @BeforeEach - protected void setUp() { - routine = CASNumberCheckDigit.getInstance(); - valid = new String[] {MIN, WATER, ETHANOL, ASPIRIN, COFFEIN, FORMALDEHYDE, DEXAMETHASONE, ARSENIC, ASBESTOS, MAX}; - } - /** * {@inheritDoc} */ @@ -55,4 +46,13 @@ public class CASNumberCheckDigitTest extends AbstractCheckDigitTest { return cde.substring(0, cde.length() - checkDigitLth); } + /** + * Sets up routine & valid codes. + */ + @BeforeEach + protected void setUp() { + routine = CASNumberCheckDigit.getInstance(); + valid = new String[] {MIN, WATER, ETHANOL, ASPIRIN, COFFEIN, FORMALDEHYDE, DEXAMETHASONE, ARSENIC, ASBESTOS, MAX}; + } + } diff --git a/src/test/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigitTest.java b/src/test/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigitTest.java index f8a4cbce..cc9afb97 100644 --- a/src/test/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigitTest.java +++ b/src/test/java/org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigitTest.java @@ -30,15 +30,6 @@ public class ECNumberCheckDigitTest extends AbstractCheckDigitTest { private static final String ASBESTOS = "603-721-4"; private static final String MAX = "999-999-2"; // theoretical - /** - * Sets up routine & valid codes. - */ - @BeforeEach - protected void setUp() { - routine = ECNumberCheckDigit.getInstance(); - valid = new String[] {MIN, FORMALDEHYDE, DEXAMETHASONE, ARSENIC, ASBESTOS, MAX}; - } - /** * {@inheritDoc} */ @@ -51,4 +42,13 @@ public class ECNumberCheckDigitTest extends AbstractCheckDigitTest { return cde.substring(0, cde.length() - checkDigitLth); } + /** + * Sets up routine & valid codes. + */ + @BeforeEach + protected void setUp() { + routine = ECNumberCheckDigit.getInstance(); + valid = new String[] {MIN, FORMALDEHYDE, DEXAMETHASONE, ARSENIC, ASBESTOS, MAX}; + } + }