001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator.routines.checkdigit;
018
019import org.apache.commons.validator.GenericValidator;
020import org.apache.commons.validator.routines.CodeValidator;
021
022/**
023 * Modulus 10 <strong>CAS Registry Number</strong> (or <strong>Chemical Abstracts Service</strong> (CAS RN)) Check Digit
024 * calculation/validation.
025 *
026 * <p>
027 * CAS Numbers are unique identification numbers used
028 * to identify chemical substance described in the open scientific literature.
029 * </p>
030 *
031 * <p>
032 * Check digit calculation is based on <em>modulus 10</em> with digits being weighted
033 * based on their position (from right to left).
034 * </p>
035 *
036 * <p>
037 * The check digit is found by taking the last digit times 1, the preceding digit times 2,
038 * the preceding digit times 3 etc., adding all these up and computing the sum modulo 10.
039 * For example, the CAS number of water is {@code 7732-18-5}:
040 * the checksum 5 is calculated as (8×1 + 1×2 + 2×3 + 3×4 + 7×5 + 7×6) = 105; 105 mod 10 = 5.
041 * </p>
042 *
043 * <p>
044 * For further information see
045 *  <a href="https://en.wikipedia.org/wiki/CAS_Registry_Number">Wikipedia - CAS Registry Number</a>.
046 * </p>
047 *
048 * @since 1.9.0
049 */
050public final class CASNumberCheckDigit extends ModulusCheckDigit {
051
052    private static final long serialVersionUID = -5387334603220786657L;
053
054    /** Singleton Check Digit instance */
055    private static final CASNumberCheckDigit INSTANCE = new CASNumberCheckDigit();
056
057    /**
058     * CAS number consists of 3 groups of numbers separated dashes (-).
059     * First group has 2 to 7 digits.
060     * Example: water is 7732-18-5
061     */
062    private static final String GROUP1 = "(\\d{2,7})";
063
064    private static final String DASH = "(?:\\-)";
065    static final String CAS_REGEX = "^(?:" + GROUP1 + DASH + "(\\d{2})" + DASH + "(\\d))$";
066    private static final int CAS_MIN_LEN = 4; // 9-99-9 LEN without SEP
067
068    /** maximum capacity of 1,000,000,000 == 9999999-99-9*/
069    private static final int CAS_MAX_LEN = 10;
070    static final CodeValidator REGEX_VALIDATOR = new CodeValidator(CAS_REGEX, CAS_MIN_LEN, CAS_MAX_LEN, null);
071
072    /** Weighting given to digits depending on their right position */
073    private static final int[] POSITION_WEIGHT = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
074
075    /**
076     * Gets the singleton instance of this validator.
077     *
078     * @return A singleton instance of the CAS Number validator.
079     */
080    public static CheckDigit getInstance() {
081        return INSTANCE;
082    }
083
084    /**
085     * Constructs a modulus 10 Check Digit routine for CAS Numbers.
086     */
087    private CASNumberCheckDigit() {
088    }
089
090    /**
091     * {@inheritDoc}
092     */
093    @Override
094    public String calculate(final String code) throws CheckDigitException {
095        if (GenericValidator.isBlankOrNull(code)) {
096            throw new CheckDigitException("Code is missing");
097        }
098        final int modulusResult = INSTANCE.calculateModulus(code, false);
099        return toCheckDigit(modulusResult);
100    }
101
102    /**
103     * {@inheritDoc}
104     */
105    @Override
106    public boolean isValid(final String code) {
107        if (GenericValidator.isBlankOrNull(code)) {
108            return false;
109        }
110        final Object cde = REGEX_VALIDATOR.validate(code);
111        if (!(cde instanceof String)) {
112            return false;
113        }
114        try {
115            final int modulusResult = INSTANCE.calculateModulus((String) cde, true);
116            return modulusResult == Character.getNumericValue(code.charAt(code.length() - 1));
117        } catch (final CheckDigitException ex) {
118            return false;
119        }
120    }
121
122    /**
123     * Calculates the <em>weighted</em> value of a character in the code at a specified position.
124     * <p>
125     * CAS numbers are weighted in the following manner:
126     * </p>
127     * <pre>{@code
128     *    right position: 1  2  3  4  5  6  7  8  9 10
129     *            weight: 1  2  3  4  5  6  7  8  9  0
130     * }</pre>
131     *
132     * @param charValue The numeric value of the character.
133     * @param leftPos The position of the character in the code, counting from left to right
134     * @param rightPos The position of the character in the code, counting from right to left
135     * @return The weighted value of the character.
136     */
137    @Override
138    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
139        final int weight = POSITION_WEIGHT[(rightPos - 1) % MODULUS_10];
140        return charValue * weight;
141    }
142
143}