1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.validator.routines.checkdigit;
19
20 import org.apache.commons.validator.GenericValidator;
21 import org.apache.commons.validator.routines.CodeValidator;
22
23 /**
24 * Modulus 11 <strong>EC number</strong> Check Digit calculation/validation.
25 *
26 * <p>
27 * The European Community number (EC number) is a unique seven-digit identifier that is assigned to chemical substances. For example, the EC number of arsenic
28 * is 231-148-6:
29 * </p>
30 *
31 * <p>
32 * Check digit calculation is based on <em>modulus 11</em> with digits being weighted based on their position (from left to right).
33 * </p>
34 *
35 * <p>
36 * For further information see <a href="https://en.wikipedia.org/wiki/European_Community_number">Wikipedia - EC number</a>.
37 * </p>
38 *
39 * @since 1.9.0
40 */
41 public final class ECNumberCheckDigit extends ModulusCheckDigit {
42
43 private static final long serialVersionUID = 7265356024784308367L;
44
45 /** Singleton Check Digit instance. */
46 private static final ECNumberCheckDigit INSTANCE = new ECNumberCheckDigit();
47
48 /**
49 * EC number consists of 3 groups of numbers separated dashes (-). Example: dexamethasone is 200-003-9
50 */
51 private static final String GROUP = "(\\d{3})";
52
53 private static final String DASH = "(?:\\-)";
54
55 static final String EC_REGEX = "^(?:" + GROUP + DASH + GROUP + DASH + "(\\d))$";
56
57 private static final int EC_LEN = 7;
58
59 static final CodeValidator REGEX_VALIDATOR = new CodeValidator(EC_REGEX, EC_LEN, null);
60
61 /**
62 * Gets the singleton instance of this validator.
63 *
64 * @return A singleton instance of the EC Number validator.
65 */
66 public static CheckDigit getInstance() {
67 return INSTANCE;
68 }
69
70 /**
71 * Constructs a modulus 11 Check Digit routine.
72 */
73 private ECNumberCheckDigit() {
74 super(MODULUS_11);
75 }
76
77 /**
78 * {@inheritDoc}
79 */
80 @Override
81 public String calculate(final String code) throws CheckDigitException {
82 if (GenericValidator.isBlankOrNull(code)) {
83 throw new CheckDigitException("Code is missing");
84 }
85 final int modulusResult = INSTANCE.calculateModulus(code, false);
86 return toCheckDigit(modulusResult);
87 }
88
89 /**
90 * {@inheritDoc}
91 */
92 @Override
93 public boolean isValid(final String code) {
94 if (GenericValidator.isBlankOrNull(code)) {
95 return false;
96 }
97 final Object cde = REGEX_VALIDATOR.validate(code);
98 if (!(cde instanceof String)) {
99 return false;
100 }
101 final String validated = (String) cde;
102 try {
103 return INSTANCE.calculateModulus(validated, true) == Character.getNumericValue(validated.charAt(validated.length() - 1));
104 } catch (final CheckDigitException e) {
105 return false;
106 }
107 }
108
109 /**
110 * Calculates the <em>weighted</em> value of a character in the code at a specified position.
111 *
112 * <p>
113 * For EC number digits are weighted by their position from left to right.
114 * </p>
115 *
116 * @param charValue The numeric value of the character.
117 * @param leftPos The position of the character in the code, counting from left to right.
118 * @param rightPos The position of the character in the code, counting from right to left.
119 * @return The weighted value of the character.
120 */
121 @Override
122 protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
123 return leftPos >= EC_LEN ? 0 : charValue * leftPos;
124 }
125 }