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 /**
63 * Gets the singleton instance of this validator.
64 *
65 * @return A singleton instance of the EC Number validator.
66 */
67 public static CheckDigit getInstance() {
68 return INSTANCE;
69 }
70
71 /**
72 * Constructs a modulus 11 Check Digit routine.
73 */
74 private ECNumberCheckDigit() {
75 super(MODULUS_11);
76 }
77
78 /**
79 * {@inheritDoc}
80 */
81 @Override
82 public String calculate(final String code) throws CheckDigitException {
83 if (GenericValidator.isBlankOrNull(code)) {
84 throw new CheckDigitException("Code is missing");
85 }
86 final int modulusResult = INSTANCE.calculateModulus(code, false);
87 return toCheckDigit(modulusResult);
88 }
89
90 /**
91 * {@inheritDoc}
92 */
93 @Override
94 public boolean isValid(final String code) {
95 if (GenericValidator.isBlankOrNull(code)) {
96 return false;
97 }
98 final Object cde = REGEX_VALIDATOR.validate(code);
99 if (!(cde instanceof String)) {
100 return false;
101 }
102 try {
103 final int modulusResult = INSTANCE.calculateModulus((String) cde, true);
104 return modulusResult == Character.getNumericValue(code.charAt(code.length() - 1));
105 } catch (final CheckDigitException ex) {
106 return false;
107 }
108 }
109
110 /**
111 * Calculates the <em>weighted</em> value of a character in the
112 * code at a specified position.
113 *
114 * <p>For EC number digits are weighted by their position from left to right.</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
126 }