All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commons.validator.routines.CodeValidator Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package commons.validator.routines;

import java.io.Serializable;

import commons.validator.routines.checkdigit.CheckDigit;

/**
 * Generic Code Validation providing format, minimum/maximum
 * length and {@link CheckDigit} validations.
 * 

* Performs the following validations on a code: *

    *
  • Check the format of the code using a regular expression. (if specified)
  • *
  • Check the minimum and maximum length (if specified) of the parsed code * (i.e. parsed by the regular expression).
  • *
  • Performs {@link CheckDigit} validation on the parsed code (if specified).
  • *
*

* Configure the validator with the appropriate regular expression, minimum/maximum length * and {@link CheckDigit} validator and then call one of the two validation * methods provided:

*
    *
  • boolean isValid(code)
  • *
  • String validate(code)
  • *
*

* Codes often include format characters - such as hyphens - to make them * more easily human readable. These can be removed prior to length and check digit * validation by specifying them as a non-capturing group in the regular * expression (i.e. use the (?: ) notation). * * @version $Revision$ * @since Validator 1.4 */ public final class CodeValidator implements Serializable { private static final long serialVersionUID = 446960910870938233L; private final RegexValidator regexValidator; private final int minLength; private final int maxLength; private final CheckDigit checkdigit; /** * Construct a code validator with a specified regular * expression and {@link CheckDigit}. * * @param regex The format regular expression * @param checkdigit The check digit validation routine */ public CodeValidator(String regex, CheckDigit checkdigit) { this(regex, -1, -1, checkdigit); } /** * Construct a code validator with a specified regular * expression, length and {@link CheckDigit}. * * @param regex The format regular expression. * @param length The length of the code * (sets the mimimum/maximum to the same) * @param checkdigit The check digit validation routine */ public CodeValidator(String regex, int length, CheckDigit checkdigit) { this(regex, length, length, checkdigit); } /** * Construct a code validator with a specified regular * expression, minimum/maximum length and {@link CheckDigit} validation. * * @param regex The regular expression validator * @param minLength The minimum length of the code * @param maxLength The maximum length of the code * @param checkdigit The check digit validation routine */ public CodeValidator(String regex, int minLength, int maxLength, CheckDigit checkdigit) { if (regex != null && regex.length() > 0) { this.regexValidator = new RegexValidator(regex); } else { this.regexValidator = null; } this.minLength = minLength; this.maxLength = maxLength; this.checkdigit = checkdigit; } /** * Construct a code validator with a specified regular expression, * validator and {@link CheckDigit} validation. * * @param regexValidator The format regular expression validator * @param checkdigit The check digit validation routine. */ public CodeValidator(RegexValidator regexValidator, CheckDigit checkdigit) { this(regexValidator, -1, -1, checkdigit); } /** * Construct a code validator with a specified regular expression, * validator, length and {@link CheckDigit} validation. * * @param regexValidator The format regular expression validator * @param length The length of the code * (sets the mimimum/maximum to the same value) * @param checkdigit The check digit validation routine */ public CodeValidator(RegexValidator regexValidator, int length, CheckDigit checkdigit) { this(regexValidator, length, length, checkdigit); } /** * Construct a code validator with a specified regular expression * validator, minimum/maximum length and {@link CheckDigit} validation. * * @param regexValidator The format regular expression validator * @param minLength The minimum length of the code * @param maxLength The maximum length of the code * @param checkdigit The check digit validation routine */ public CodeValidator(RegexValidator regexValidator, int minLength, int maxLength, CheckDigit checkdigit) { this.regexValidator = regexValidator; this.minLength = minLength; this.maxLength = maxLength; this.checkdigit = checkdigit; } /** * Return the check digit validation routine. *

* N.B. Optional, if not set no Check Digit * validation will be performed on the code. * * @return The check digit validation routine */ public CheckDigit getCheckDigit() { return checkdigit; } /** * Return the minimum length of the code. *

* N.B. Optional, if less than zero the * minimum length will not be checked. * * @return The minimum length of the code or * -1 if the code has no minimum length */ public int getMinLength() { return minLength; } /** * Return the maximum length of the code. *

* N.B. Optional, if less than zero the * maximum length will not be checked. * * @return The maximum length of the code or * -1 if the code has no maximum length */ public int getMaxLength() { return maxLength; } /** * Return the regular expression validator. *

* N.B. Optional, if not set no regular * expression validation will be performed on the code. * * @return The regular expression validator */ public RegexValidator getRegexValidator() { return regexValidator; } /** * Validate the code returning either true * or false. * * @param input The code to validate * @return true if valid, otherwise * false */ public boolean isValid(String input) { return (validate(input) != null); } /** * Validate the code returning either the valid code or * null if invalid. * * @param input The code to validate * @return The code if valid, otherwise null * if invalid */ public Object validate(String input) { if (input == null) { return null; } String code = input.trim(); if (code.length() == 0) { return null; } // validate/reformat using regular expression if (regexValidator != null) { code = regexValidator.validate(code); if (code == null) { return null; } } // check the length if ((minLength >= 0 && code.length() < minLength) || (maxLength >= 0 && code.length() > maxLength)) { return null; } // validate the check digit if (checkdigit != null && !checkdigit.isValid(code)) { return null; } return code; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy