001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.validator.routines.checkdigit;
018
019import org.apache.commons.validator.routines.CodeValidator;
020import org.apache.commons.validator.routines.ISBNValidator;
021
022/**
023 * <strong>Check Digit</strong> calculation and validation.
024 * <p>
025 * The logic for validating check digits has previously been
026 * embedded within the logic for specific code validation, which
027 * includes other validations such as verifying the format
028 * or length of a code. {@link CheckDigit} provides for separating out
029 * the check digit calculation logic enabling it to be more easily
030 * tested and reused.
031 * </p>
032 * <p>
033 * Although Commons Validator is primarily concerned with validation,
034 * {@link CheckDigit} also defines behavior for calculating/generating check
035 * digits, since it makes sense that users will want to (re-)use the
036 * same logic for both. The {@link ISBNValidator}
037 * makes specific use of this feature by providing the facility to validate ISBN-10 codes
038 * and then convert them to the new ISBN-13 standard.
039 * </p>
040 * <p>
041 * CheckDigit is used by the new generic {@link CodeValidator} implementation.
042 * </p>
043 *
044 * <h2>Implementations</h2>
045 * See the
046 * <a href="package-summary.html">Package Summary</a> for a full
047 * list of implementations provided within Commons Validator.
048 *
049 * @see org.apache.commons.validator.routines.CodeValidator
050 * @since 1.4
051 */
052public interface CheckDigit {
053
054    /**
055     * Calculates the <em>Check Digit</em> for a code.
056     *
057     * @param code The code to calculate the Check Digit for. The string must not include the check digit.
058     * @return The calculated Check Digit.
059     * @throws CheckDigitException if an error occurs.
060     */
061    String calculate(String code) throws CheckDigitException;
062
063    /**
064     * Validates the check digit for the code.
065     *
066     * @param code The code to validate, the string must include the check digit.
067     * @return {@code true} if the check digit is valid, otherwise {@code false}.
068     */
069    boolean isValid(String code);
070}