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 package org.apache.commons.validator.routines.checkdigit;
18
19 import org.apache.commons.validator.GenericValidator;
20 import org.apache.commons.validator.routines.CodeValidator;
21
22 /**
23 * Modulus 11 <strong>EC number</strong> Check Digit calculation/validation.
24 *
25 * <p>
26 * The European Community number (EC number) is a unique seven-digit identifier
27 * that is assigned to chemical substances.
28 * For example, the EC number of arsenic is 231-148-6:
29 * </p>
30 *
31 * <p>
32 * Check digit calculation is based on <em>modulus 11</em> with digits being weighted
33 * based on their position (from left to right).
34 * </p>
35 *
36 * <p>
37 * For further information see
38 * <a href="https://en.wikipedia.org/wiki/European_Community_number">Wikipedia - EC number</a>.
39 * </p>
40 *
41 * @since 1.9.0
42 */
43 public final class ECNumberCheckDigit extends ModulusCheckDigit {
44
45 private static final long serialVersionUID = 7265356024784308367L;
46
47 /** Singleton Check Digit instance */
48 private static final ECNumberCheckDigit INSTANCE = new ECNumberCheckDigit();
49
50 /**
51 * EC number consists of 3 groups of numbers separated dashes (-).
52 * Example: dexamethasone is 200-003-9
53 */
54 private static final String GROUP = "(\\d{3})";
55
56 private static final String DASH = "(?:\\-)";
57 static final String EC_REGEX = "^(?:" + GROUP + DASH + GROUP + DASH + "(\\d))$";
58 private static final int EC_LEN = 7;
59
60 static final CodeValidator REGEX_VALIDATOR = new CodeValidator(EC_REGEX, EC_LEN, null);
61 /**
62 * Gets the singleton instance of this validator.
63 * @return A singleton instance of the EC Number validator.
64 */
65 public static CheckDigit getInstance() {
66 return INSTANCE;
67 }
68
69 /**
70 * Constructs a modulus 11 Check Digit routine.
71 */
72 private ECNumberCheckDigit() {
73 super(MODULUS_11);
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 public String calculate(final String code) throws CheckDigitException {
81 if (GenericValidator.isBlankOrNull(code)) {
82 throw new CheckDigitException("Code is missing");
83 }
84 final int modulusResult = INSTANCE.calculateModulus(code, false);
85 return toCheckDigit(modulusResult);
86 }
87
88 /**
89 * {@inheritDoc}
90 */
91 @Override
92 public boolean isValid(final String code) {
93 if (GenericValidator.isBlankOrNull(code)) {
94 return false;
95 }
96 final Object cde = REGEX_VALIDATOR.validate(code);
97 if (!(cde instanceof String)) {
98 return false;
99 }
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 }