View Javadoc
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    *      http://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 <b>EC number</b> 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 <i>modulus 11</i> 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       * Gets the singleton instance of this validator.
52       * @return A singleton instance of the EC Number validator.
53       */
54      public static CheckDigit getInstance() {
55          return INSTANCE;
56      }
57  
58      /**
59       * EC number consists of 3 groups of numbers separated dashes (-).
60       * Example: dexamethasone is 200-003-9
61       */
62      private static final String GROUP = "(\\d{3})";
63      private static final String DASH = "(?:\\-)";
64      static final String EC_REGEX = "^(?:" + GROUP + DASH + GROUP + DASH + "(\\d))$";
65  
66      private static final int EC_LEN = 7;
67      static final CodeValidator REGEX_VALIDATOR = new CodeValidator(EC_REGEX, EC_LEN, null);
68  
69      /**
70       * Constructs a modulus 11 Check Digit routine.
71       */
72      private ECNumberCheckDigit() {
73          super(MODULUS_11);
74      }
75  
76      /**
77       * Calculates the <i>weighted</i> value of a character in the
78       * code at a specified position.
79       *
80       * <p>For EC number digits are weighted by their position from left to right.</p>
81       *
82       * @param charValue The numeric value of the character.
83       * @param leftPos The position of the character in the code, counting from left to right
84       * @param rightPos The positionof the character in the code, counting from right to left
85       * @return The weighted value of the character.
86       */
87      @Override
88      protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
89          return leftPos >= EC_LEN ? 0 : charValue * leftPos;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public String calculate(final String code) throws CheckDigitException {
97          if (GenericValidator.isBlankOrNull(code)) {
98              throw new CheckDigitException("Code is missing");
99          }
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 }