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 *      http://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 <b>EC number</b> 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 <i>modulus 11</i> 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     * Gets the singleton instance of this validator.
052     * @return A singleton instance of the EC Number validator.
053     */
054    public static CheckDigit getInstance() {
055        return INSTANCE;
056    }
057
058    /**
059     * EC number consists of 3 groups of numbers separated dashes (-).
060     * Example: dexamethasone is 200-003-9
061     */
062    private static final String GROUP = "(\\d{3})";
063    private static final String DASH = "(?:\\-)";
064    static final String EC_REGEX = "^(?:" + GROUP + DASH + GROUP + DASH + "(\\d))$";
065
066    private static final int EC_LEN = 7;
067    static final CodeValidator REGEX_VALIDATOR = new CodeValidator(EC_REGEX, EC_LEN, null);
068
069    /**
070     * Constructs a modulus 11 Check Digit routine.
071     */
072    private ECNumberCheckDigit() {
073        super(MODULUS_11);
074    }
075
076    /**
077     * Calculates the <i>weighted</i> value of a character in the
078     * code at a specified position.
079     *
080     * <p>For EC number digits are weighted by their position from left to right.</p>
081     *
082     * @param charValue The numeric value of the character.
083     * @param leftPos The position of the character in the code, counting from left to right
084     * @param rightPos The positionof the character in the code, counting from right to left
085     * @return The weighted value of the character.
086     */
087    @Override
088    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
089        return leftPos >= EC_LEN ? 0 : charValue * leftPos;
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    @Override
096    public String calculate(final String code) throws CheckDigitException {
097        if (GenericValidator.isBlankOrNull(code)) {
098            throw new CheckDigitException("Code is missing");
099        }
100        final int modulusResult = INSTANCE.calculateModulus(code, false);
101        return toCheckDigit(modulusResult);
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public boolean isValid(final String code) {
109        if (GenericValidator.isBlankOrNull(code)) {
110            return false;
111        }
112        final Object cde = REGEX_VALIDATOR.validate(code);
113        if (!(cde instanceof String)) {
114            return false;
115        }
116        try {
117            final int modulusResult = INSTANCE.calculateModulus((String) cde, true);
118            return modulusResult == Character.getNumericValue(code.charAt(code.length() - 1));
119        } catch (final CheckDigitException ex) {
120            return false;
121        }
122    }
123
124}