Apache Commons logo Commons Validator

CPD Results

The following document contains the results of PMD's CPD 7.14.0.

Duplications

File Line
org/apache/commons/validator/routines/CalendarValidator.java 218
org/apache/commons/validator/routines/TimeValidator.java 167
return compare(value, compare, Calendar.YEAR);
    }

    /**
     * <p>Convert the parsed {@code Date} to a {@link Calendar}.</p>
     *
     * @param value The parsed {@code Date} object created.
     * @param formatter The Format used to parse the value with.
     * @return The parsed value converted to a {@link Calendar}.
     */
    @Override
    protected Object processParsedValue(final Object value, final Format formatter) {
        return ((DateFormat) formatter).getCalendar();
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the default
     *    {@link Locale} and {@code TimeZone}.
     *
     * @param value The value validation is being performed on.
     * @return The parsed {@link Calendar} if valid or {@code null}
     *  if invalid.
     */
    public Calendar validate(final String value) {
        return (Calendar) parse(value, (String) null, (Locale) null, (TimeZone) null);
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the specified
     *    {@link Locale} and default {@code TimeZone}.
     *
     * @param value The value validation is being performed on.
     * @param locale The locale to use for the date format, system default if null.
     * @return The parsed {@link Calendar} if valid or {@code null} if invalid.
     */
    public Calendar validate(final String value, final Locale locale) {
        return (Calendar) parse(value, (String) null, locale, (TimeZone) null);
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the specified
     *    {@link Locale} and {@code TimeZone}.
     *
     * @param value The value validation is being performed on.
     * @param locale The locale to use for the date format, system default if null.
     * @param timeZone The Time Zone used to parse the date, system default if null.
     * @return The parsed {@link Calendar} if valid or {@code null} if invalid.
     */
    public Calendar validate(final String value, final Locale locale, final TimeZone timeZone) {
        return (Calendar) parse(value, (String) null, locale, timeZone);
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the specified
     *    <em>pattern</em> and default {@code TimeZone}.
     *
     * @param value The value validation is being performed on.
     * @param pattern The pattern used to validate the value against.
     * @return The parsed {@link Calendar} if valid or {@code null} if invalid.
     */
    public Calendar validate(final String value, final String pattern) {
        return (Calendar) parse(value, pattern, (Locale) null, (TimeZone) null);
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the specified pattern
     *    and {@link Locale} and the default {@code TimeZone}.
     *
     * @param value The value validation is being performed on.
     * @param pattern The pattern used to validate the value against, or the
     *        default for the {@link Locale} if {@code null}.
     * @param locale The locale to use for the date format, system default if null.
     * @return The parsed {@link Calendar} if valid or {@code null} if invalid.
     */
    public Calendar validate(final String value, final String pattern, final Locale locale) {
        return (Calendar) parse(value, pattern, locale, (TimeZone) null);
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the specified
     *    pattern, and {@link Locale} and {@code TimeZone}.
     *
     * @param value The value validation is being performed on.
     * @param pattern The pattern used to validate the value against, or the
     *        default for the {@link Locale} if {@code null}.
     * @param locale The locale to use for the date format, system default if null.
     * @param timeZone The Time Zone used to parse the date, system default if null.
     * @return The parsed {@link Calendar} if valid or {@code null} if invalid.
     */
    public Calendar validate(final String value, final String pattern, final Locale locale, final TimeZone timeZone) {
        return (Calendar) parse(value, pattern, locale, timeZone);
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the specified
     *    <em>pattern</em> and {@code TimeZone}.
     *
     * @param value The value validation is being performed on.
     * @param pattern The pattern used to validate the value against.
     * @param timeZone The Time Zone used to parse the date, system default if null.
     * @return The parsed {@link Calendar} if valid or {@code null} if invalid.
     */
    public Calendar validate(final String value, final String pattern, final TimeZone timeZone) {
        return (Calendar) parse(value, pattern, (Locale) null, timeZone);
    }

    /**
     * <p>Validate/convert a {@link Calendar} using the specified
     *    {@code TimeZone} and default {@link Locale}.
     *
     * @param value The value validation is being performed on.
     * @param timeZone The Time Zone used to parse the date, system default if null.
     * @return The parsed {@link Calendar} if valid or {@code null}
     *  if invalid.
     */
    public Calendar validate(final String value, final TimeZone timeZone) {
        return (Calendar) parse(value, (String) null, (Locale) null, timeZone);
    }

}
File Line
org/apache/commons/validator/routines/checkdigit/CASNumberCheckDigit.java 86
org/apache/commons/validator/routines/checkdigit/ECNumberCheckDigit.java 74
}

    /**
     * {@inheritDoc}
     */
    @Override
    public String calculate(final String code) throws CheckDigitException {
        if (GenericValidator.isBlankOrNull(code)) {
            throw new CheckDigitException("Code is missing");
        }
        final int modulusResult = INSTANCE.calculateModulus(code, false);
        return toCheckDigit(modulusResult);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isValid(final String code) {
        if (GenericValidator.isBlankOrNull(code)) {
            return false;
        }
        final Object cde = REGEX_VALIDATOR.validate(code);
        if (!(cde instanceof String)) {
            return false;
        }
        try {
            final int modulusResult = INSTANCE.calculateModulus((String) cde, true);
            return modulusResult == Character.getNumericValue(code.charAt(code.length() - 1));
        } catch (final CheckDigitException ex) {
            return false;
        }
    }

    /**
     * Calculates the <em>weighted</em> value of a character in the code at a specified position.
     * <p>
     * CAS numbers are weighted in the following manner:
     * </p>
     * <pre>{@code
     *    right position: 1  2  3  4  5  6  7  8  9 10
     *            weight: 1  2  3  4  5  6  7  8  9  0
     * }</pre>
     *
     * @param charValue The numeric value of the character.
     * @param leftPos The position of the character in the code, counting from left to right
     * @param rightPos The position of the character in the code, counting from right to left
     * @return The weighted value of the character.
     */
    @Override
    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
File Line
org/apache/commons/validator/routines/checkdigit/ISBN10CheckDigit.java 54
org/apache/commons/validator/routines/checkdigit/ISSNCheckDigit.java 59
public ISBN10CheckDigit() {
        super(MODULUS_11);
    }

    /**
     * <p>Convert an integer value to a character at a specified position.</p>
     *
     * <p>Value '10' for position 1 (check digit) converted to 'X'.</p>
     *
     * @param charValue The integer value of the character.
     * @return The converted character.
     * @throws CheckDigitException if an error occurs.
     */
    @Override
    protected String toCheckDigit(final int charValue)
            throws CheckDigitException {
        if (charValue == 10) {  // CHECKSTYLE IGNORE MagicNumber
            return "X";
        }
        return super.toCheckDigit(charValue);
    }

    /**
     * <p>Convert a character at a specified position to an
     * integer value.</p>
     *
     * <p>Character 'X' check digit converted to 10.</p>
     *
     * @param character The character to convert.
     * @param leftPos The position of the character in the code, counting from left to right
     * @param rightPos The position of the character in the code, counting from right to left
     * @return The integer value of the character.
     * @throws CheckDigitException if an error occurs.
     */
    @Override
    protected int toInt(final char character, final int leftPos, final int rightPos)
            throws CheckDigitException {
        if (rightPos == 1 && character == 'X') {
            return 10;  // CHECKSTYLE IGNORE MagicNumber
        }
        return super.toInt(character, leftPos, rightPos);
    }

    /**
     * Calculates the <em>weighted</em> value of a character in the
     * code at a specified position.
     *
     * <p>For ISBN-10 (from right to left) digits are weighted
     * by their position.</p>
     *
     * @param charValue The numeric value of the character.
     * @param leftPos The position of the character in the code, counting from left to right
     * @param rightPos The position of the character in the code, counting from right to left
     * @return The weighted value of the character.
     */
    @Override
    protected int weightedValue(final int charValue, final int leftPos, final int rightPos) {
File Line
org/apache/commons/validator/GenericTypeValidator.java 69
org/apache/commons/validator/GenericTypeValidator.java 342
org/apache/commons/validator/GenericTypeValidator.java 394
org/apache/commons/validator/GenericTypeValidator.java 446
Byte result = null;

        if (value != null) {
            NumberFormat formatter = null;
            if (locale != null) {
                formatter = NumberFormat.getNumberInstance(locale);
            } else {
                formatter = NumberFormat.getNumberInstance(Locale.getDefault());
            }
            formatter.setParseIntegerOnly(true);
            final ParsePosition pos = new ParsePosition(0);
            final Number num = formatter.parse(value, pos);

            // If there was no error      and we used the whole string
            if (pos.getErrorIndex() == -1 && pos.getIndex() == value.length() &&
                    num.doubleValue() >= Byte.MIN_VALUE &&