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