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    *      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.routines.CodeValidator;
20  import org.apache.commons.validator.routines.ISBNValidator;
21  
22  /**
23   * <strong>Check Digit</strong> calculation and validation.
24   * <p>
25   * The logic for validating check digits has previously been
26   * embedded within the logic for specific code validation, which
27   * includes other validations such as verifying the format
28   * or length of a code. {@link CheckDigit} provides for separating out
29   * the check digit calculation logic enabling it to be more easily
30   * tested and reused.
31   * </p>
32   * <p>
33   * Although Commons Validator is primarily concerned with validation,
34   * {@link CheckDigit} also defines behavior for calculating/generating check
35   * digits, since it makes sense that users will want to (re-)use the
36   * same logic for both. The {@link ISBNValidator}
37   * makes specific use of this feature by providing the facility to validate ISBN-10 codes
38   * and then convert them to the new ISBN-13 standard.
39   * </p>
40   * <p>
41   * CheckDigit is used by the new generic {@link CodeValidator} implementation.
42   * </p>
43   *
44   * <h2>Implementations</h2>
45   * See the
46   * <a href="package-summary.html">Package Summary</a> for a full
47   * list of implementations provided within Commons Validator.
48   *
49   * @see org.apache.commons.validator.routines.CodeValidator
50   * @since 1.4
51   */
52  public interface CheckDigit {
53  
54      /**
55       * Calculates the <em>Check Digit</em> for a code.
56       *
57       * @param code The code to calculate the Check Digit for. The string must not include the check digit.
58       * @return The calculated Check Digit.
59       * @throws CheckDigitException if an error occurs.
60       */
61      String calculate(String code) throws CheckDigitException;
62  
63      /**
64       * Validates the check digit for the code.
65       *
66       * @param code The code to validate, the string must include the check digit.
67       * @return {@code true} if the check digit is valid, otherwise {@code false}.
68       */
69      boolean isValid(String code);
70  }