You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2017/04/25 14:25:25 UTC

svn commit: r1792618 - in /commons/proper/validator/trunk/src: changes/ main/java/org/apache/commons/validator/routines/ test/java/org/apache/commons/validator/routines/ test/java/org/apache/commons/validator/routines/checkdigit/

Author: sebb
Date: Tue Apr 25 14:25:25 2017
New Revision: 1792618

URL: http://svn.apache.org/viewvc?rev=1792618&view=rev
Log:
VALIDATOR-423 Add ISINValidator

Added:
    commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISINValidator.java   (with props)
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java   (with props)
Modified:
    commons/proper/validator/trunk/src/changes/changes.xml
    commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java

Modified: commons/proper/validator/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/changes/changes.xml?rev=1792618&r1=1792617&r2=1792618&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/changes/changes.xml (original)
+++ commons/proper/validator/trunk/src/changes/changes.xml Tue Apr 25 14:25:25 2017
@@ -64,6 +64,44 @@ The <action> type attribute can be add,u
      -->
 
   <body>
+  <release version="1.7" date="TBA" description="
+This is primarily a maintenance release.
+
+All projects are encouraged to update to this release of
+Apache Commons Validator.
+
+  Commons Validator requires Java 1.6 or later.
+
+
+ Main enhancements
+ =================
+
+
+ * CreditCard validation specification by numeric range
+
+
+ IMPORTANT NOTES
+ ===============
+
+
+ BREAKING CHANGES:
+
+   * NONE.
+
+
+ DEPENDENCIES
+ ============
+
+The dependencies for Validator have not changed since the 1.4 release.
+
+For the current list of dependencies, please see
+http://commons.apache.org/validator/dependencies.html
+  ">
+    <action issue="VALIDATOR-423" type="add" dev="sebb">
+    Add ISINValidator
+    </action>
+  </release>
+
   <release version="1.6" date="2017-02-21" description="
 This is primarily a maintenance release.
 

Added: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISINValidator.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISINValidator.java?rev=1792618&view=auto
==============================================================================
--- commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISINValidator.java (added)
+++ commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISINValidator.java Tue Apr 25 14:25:25 2017
@@ -0,0 +1,124 @@
+/*
+ * 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;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.Locale;
+
+import org.apache.commons.validator.routines.checkdigit.ISINCheckDigit;
+
+/**
+ * <b>ISIN</b> (International Securities Identifying Number) validation.
+ *
+ * <p>
+ * ISIN Numbers are 12 character alphanumeric codes used to identify Securities.
+ * </p>
+ *
+ * <p>
+ * ISINs consist of two alphabetic characters,
+ * which are the ISO 3166-1 alpha-2 code for the issuing country,
+ * nine alpha-numeric characters (the National Securities Identifying Number, or NSIN, which identifies the security),
+ * and one numerical check digit.
+ * They are 12 characters in length.
+ * </p>
+ *
+ * <p>
+ * See <a href="http://en.wikipedia.org/wiki/ISIN">Wikipedia - ISIN</a>
+ * for more details.
+ * </p>
+ *
+ * @since 1.7
+ */
+public class ISINValidator implements Serializable {
+
+    private static final long serialVersionUID = -5964391439144260936L;
+
+    private static final String ISIN_REGEX = "([A-Z]{2}[A-Z0-9]{9}[0-9])";
+
+    private static final CodeValidator VALIDATOR = new CodeValidator(ISIN_REGEX, 12, ISINCheckDigit.ISIN_CHECK_DIGIT);
+
+    /** ISIN Code Validator (no countryCode check) */
+    private static final ISINValidator ISIN_VALIDATOR_FALSE = new ISINValidator(false);
+
+    /** ISIN Code Validator (with countryCode check) */
+    private static final ISINValidator ISIN_VALIDATOR_TRUE = new ISINValidator(true);
+
+    private static final String [] CCODES = Locale.getISOCountries();
+
+    private static final String [] SPECIALS = {
+            "EZ", // http://www.anna-web.org/standards/isin-iso-6166/
+            "XS", // https://www.isin.org/isin/
+        };
+
+    static {
+        Arrays.sort(CCODES); // we cannot assume the codes are sorted
+        Arrays.sort(SPECIALS); // Just in case ...
+    }
+
+    private final boolean checkCountryCode;
+
+    /**
+     * Return a singleton instance of the ISIN validator
+     * @param checkCountryCode whether to check the country-code prefix or not
+     * @return A singleton instance of the appropriate ISIN validator.
+     */
+    public static ISINValidator getInstance(boolean checkCountryCode) {
+        return checkCountryCode ? ISIN_VALIDATOR_TRUE : ISIN_VALIDATOR_FALSE;
+    }
+
+    private ISINValidator(boolean checkCountryCode) {
+        this.checkCountryCode = checkCountryCode;
+    }
+
+    /**
+     * Check the code is a valid ISIN code after any transformation
+     * by the validate routine.
+     * @param code The code to validate.
+     * @return <code>true</code> if a valid ISIN
+     * code, otherwise <code>false</code>.
+     */
+    public boolean isValid(String code) {
+        final boolean valid = VALIDATOR.isValid(code);
+        if (valid && checkCountryCode) {
+            return checkCode(code.substring(0,2));
+        }
+        return valid;
+    }
+
+    /**
+     * Check the code is valid ISIN code.
+     *
+     * @param code The code to validate.
+     * @return A valid ISIN code if valid, otherwise <code>null</code>.
+     */
+    public Object validate(String code) {
+        final Object validate = VALIDATOR.validate(code);
+        if (validate != null && checkCountryCode) {
+            return checkCode(code.substring(0,2)) ? validate : null;
+        }
+        return validate;
+    }
+
+    private boolean checkCode(String code) {
+        return Arrays.binarySearch(CCODES, code) >= 0
+               ||
+               Arrays.binarySearch(SPECIALS, code) >= 0
+        ;
+    }
+
+}

Propchange: commons/proper/validator/trunk/src/main/java/org/apache/commons/validator/routines/ISINValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java?rev=1792618&view=auto
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java (added)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java Tue Apr 25 14:25:25 2017
@@ -0,0 +1,100 @@
+/*
+ * 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;
+
+import junit.framework.TestCase;
+
+/**
+ * ISINValidator Test Case.
+ *
+ * @since 1.7
+ */
+public class ISINValidatorTest extends TestCase {
+
+    private static final ISINValidator VALIDATOR_TRUE = ISINValidator.getInstance(true);
+    
+    private static final ISINValidator VALIDATOR_FALSE = ISINValidator.getInstance(false);
+
+    private final String[] validFormat = new String[] {
+            "US0378331005",
+            "BMG8571G1096",
+            "AU0000XVGZA3",
+            "GB0002634946",
+            "FR0004026250",
+            "DK0009763344",
+            "GB00B03MLX29",
+            "US7562071065",
+            "US56845T3059",
+            "LU0327357389",
+            "US032511BN64",
+            "INE112A01023",
+            "EZ0000000003", // Invented; for use in ISINValidator
+            "XS0000000009",
+            };
+
+    private final String[] invalidFormat = new String[] {
+            null,
+            "",                        // empty
+            "   ",                     // empty
+            "US037833100O", // proper check digit is '5', see above
+            "BMG8571G109D", // proper check digit is '6', see above
+            "AU0000XVGZAD", // proper check digit is '3', see above
+            "GB000263494I", // proper check digit is '6', see above
+            "FR000402625C", // proper check digit is '0', see above
+            "DK000976334H", // proper check digit is '4', see above            
+            "3133EHHF3", // see VALIDATOR-422 Valid check-digit, but not valid ISIN
+            "AU0000xvgzA3", // disallow lower case NSIN
+            "gb0002634946", // disallow lower case ISO code
+            };
+
+    // Invalid codes if country checking is enabled
+    private final String[] invalidFormatTrue = new String[] {
+            "AA0000000006", // Invalid country code            
+            };
+
+    public ISINValidatorTest(String name) {
+        super(name);
+    }
+
+    public void testIsValidTrue() {
+        for(String f : validFormat) {
+            assertTrue(f, VALIDATOR_TRUE.isValid(f));            
+        }
+    }
+
+    public void testInvalidTrue() {
+        for(String f : invalidFormat) {
+            assertFalse(f, VALIDATOR_TRUE.isValid(f));            
+        }
+        for(String f : invalidFormatTrue) {
+            assertFalse(f, VALIDATOR_TRUE.isValid(f));            
+        }
+    }
+
+    public void testIsValidFalse() {
+        for(String f : validFormat) {
+            assertTrue(f, VALIDATOR_FALSE.isValid(f));            
+        }
+    }
+
+    public void testInvalidFalse() {
+        for(String f : invalidFormat) {
+            assertFalse(f, VALIDATOR_FALSE.isValid(f));            
+        }
+    }
+
+}

Propchange: commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/ISINValidatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 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=1792618&r1=1792617&r2=1792618&view=diff
==============================================================================
--- commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java (original)
+++ commons/proper/validator/trunk/src/test/java/org/apache/commons/validator/routines/checkdigit/ISINCheckDigitTest.java Tue Apr 25 14:25:25 2017
@@ -49,6 +49,9 @@ public class ISINCheckDigitTest extends
                               "DK0009763344",
                               "dk0009763344", // TODO lowercase is currently accepted, but is this valid?
                               "AU0000xvgza3", // lowercase NSIN
+                              "EZ0000000003", // Invented; for use in ISINValidatorTest
+                              "XS0000000009", // ditto
+                              "AA0000000006", // ditto
                               };
         invalid = new String[] {"0378#3100"};
     }