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 *      http://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;
020
021/**
022 * <b>Check Digit</b> calculation and validation.
023 * <p>
024 * The logic for validating check digits has previously been
025 * embedded within the logic for specific code validation, which
026 * includes other validations such as verifying the format
027 * or length of a code. {@link CheckDigit} provides for separating out
028 * the check digit calculation logic enabling it to be more easily
029 * tested and reused.
030 * </p>
031 * <p>
032 * Although Commons Validator is primarily concerned with validation,
033 * {@link CheckDigit} also defines behavior for calculating/generating check
034 * digits, since it makes sense that users will want to (re-)use the
035 * same logic for both. The {@link org.apache.commons.validator.routines.ISBNValidator}
036 * makes specific use of this feature by providing the facility to validate ISBN-10 codes
037 * and then convert them to the new ISBN-13 standard.
038 * </p>
039 * <p>
040 * CheckDigit is used by the new generic {@link CodeValidator} implementation.
041 * </p>
042 *
043 * <h2>Implementations</h2>
044 * See the
045 * <a href="package-summary.html">Package Summary</a> for a full
046 * list of implementations provided within Commons Validator.
047 *
048 * @see org.apache.commons.validator.routines.CodeValidator
049 * @since 1.4
050 */
051public interface CheckDigit {
052
053    /**
054     * Calculates the <i>Check Digit</i> for a code.
055     *
056     * @param code The code to calculate the Check Digit for.
057     * 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
068     * {@code false}.
069     */
070    boolean isValid(String code);
071
072}