    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 11 <strong>EC number</strong> Check Digit calculation/validation.
    024 *
    025 * <p>
    026 * The European Community number (EC number) is a unique seven-digit identifier
    027 * that is assigned to chemical substances.
    028 * For example, the EC number of arsenic is 231-148-6:
    029 * </p>
    030 *
    031 * <p>
    032 * Check digit calculation is based on <em>modulus 11</em> with digits being weighted
    033 * based on their position (from left to right).
    034 * </p>
    035 *
    036 * <p>
    037 * For further information see
    038 *  <a href="https://en.wikipedia.org/wiki/European_Community_number">Wikipedia - EC number</a>.
    039 * </p>
    040 *
    041 * @since 1.9.0
    042 */
    043public final class ECNumberCheckDigit extends ModulusCheckDigit {
    044
    045    private static final long serialVersionUID = 7265356024784308367L;
    046
    047    /** Singleton Check Digit instance */
    048    private static final ECNumberCheckDigit INSTANCE = new ECNumberCheckDigit();
    049
    050    /**
    051     * EC number consists of 3 groups of numbers separated dashes (-).
    052     * Example: dexamethasone is 200-003-9
    053     */
    054    private static final String GROUP = "(\\d{3})";
    055
    056    private static final String DASH = "(?:\\-)";
    057    static final String EC_REGEX = "^(?:" + GROUP + DASH + GROUP + DASH + "(\\d))$";
    058    private static final int EC_LEN = 7;
    059
    060    static final CodeValidator REGEX_VALIDATOR = new CodeValidator(EC_REGEX, EC_LEN, null);
    061    /**
    062     * Gets the singleton instance of this validator.
    063     * @return A singleton instance of the EC Number validator.
    064     */
    065    public static CheckDigit getInstance() {
    066        return INSTANCE;
    067    }
    068
    069    /**
    070     * Constructs a modulus 11 Check Digit routine.
    071     */
    072    private ECNumberCheckDigit() {
    073        super(MODULUS_11);
    074    }
    075
    076    /**
    077     * {@inheritDoc}
    078     */
    079    @Override
    080    public String calculate(final String code) throws CheckDigitException {
    081        if (GenericValidator.isBlankOrNull(code)) {
    082            throw new CheckDigitException("Code is missing");
    083        }
    084        final int modulusResult = INSTANCE.calculateModulus(code, false);
    085        return toCheckDigit(modulusResult);
    086    }
    087
    088    /**
    089     * {@inheritDoc}
    090     */
    091    @Override
    092    public boolean isValid(final String code) {
    093        if (GenericValidator.isBlankOrNull(code)) {
    094            return false;
    095        }
    096        final Object cde = REGEX_VALIDATOR.validate(code);
    097        if (!(cde instanceof String)) {
    098            return false;
    099        }
    100        try {
    101            final int modulusResult = INSTANCE.calculateModulus((String) cde, true);
    102            return modulusResult == Character.getNumericValue(code.charAt(code.length() - 1));
    103        } catch (final CheckDigitException ex) {
    104            return false;
    105        }
    106    }
    107
    108    /**
    109     * Calculates the <em>weighted</em> value of a character in the
    110     * code at a specified position.
    111     *
    112     * <p>For EC number digits are weighted by their position from left to right.</p>
    113     *
    114     * @param charValue The numeric value of the character.
    115     * @param leftPos The position of the character in the code, counting from left to right
    116     * @param rightPos The position of the character in the code, counting from right to left
    117     * @return The weighted value of the character.
    118     */
    119    @Override
    120    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
    121        return leftPos >= EC_LEN ? 0 : charValue * leftPos;
    122    }
    123
    124}