Skip to content

Commit

Permalink
HV-814 Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik authored and gunnarmorling committed Feb 10, 2014
1 parent d85dd69 commit b7ad918
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 81 deletions.
Expand Up @@ -21,8 +21,6 @@

import org.hibernate.validator.constraints.LuhnCheck;
import org.hibernate.validator.internal.util.ModUtil;
import org.hibernate.validator.internal.util.logging.Log;
import org.hibernate.validator.internal.util.logging.LoggerFactory;

/**
* Luhn algorithm checksum validator
Expand All @@ -36,9 +34,6 @@
*/
public class LuhnCheckValidator extends ModCheckBase
implements ConstraintValidator<LuhnCheck, CharSequence> {

private static final Log log = LoggerFactory.make();

@Override
public void initialize(LuhnCheck constraintAnnotation) {
super.initialize(
Expand Down
Expand Up @@ -58,6 +58,37 @@ public abstract class ModCheckBase {

private boolean ignoreNonDigitCharacters;

public boolean isValid(final CharSequence value, final ConstraintValidatorContext context) {
if ( value == null ) {
return true;
}

String valueAsString = stripNonDigitsIfRequired( value.toString() );

String digitsAsString;
char checkDigit;
try {
digitsAsString = extractVerificationString( valueAsString );
checkDigit = extractCheckDigit( valueAsString );
}
catch (IndexOutOfBoundsException e) {
return false;
}

List<Integer> digits;

try {
digits = extractDigits( digitsAsString );
}
catch (NumberFormatException e) {
return false;
}

return this.isCheckDigitValid( digits, checkDigit );
}

public abstract boolean isCheckDigitValid(List<Integer> digits, char checkDigit);

protected void initialize(int startIndex, int endIndex, int checkDigitIndex, boolean ignoreNonDigitCharacters) {
this.startIndex = startIndex;
this.endIndex = endIndex;
Expand All @@ -67,24 +98,6 @@ protected void initialize(int startIndex, int endIndex, int checkDigitIndex, boo
this.validateOptions();
}

/**
* Parses the {@link String} value as a {@link List} of {@link Integer} objects
*
* @param value the input string to be parsed
*
* @return List of {@code Integer} objects.
*
* @throws NumberFormatException in case any of the characters is not a digit
*/
private List<Integer> extractDigits(final String value) throws NumberFormatException {
List<Integer> digits = new ArrayList<Integer>( value.length() );
char[] chars = value.toCharArray();
for ( char c : chars ) {
digits.add( extractDigit( c ) );
}
return digits;
}

/**
* Returns the numeric {@code int} value of a {@code char}
*
Expand All @@ -103,38 +116,25 @@ protected int extractDigit(char value) throws NumberFormatException {
}
}

public abstract boolean isCheckDigitValid(List<Integer> digits, char checkDigit);

public boolean isValid(final CharSequence value, final ConstraintValidatorContext context) {
if ( value == null ) {
return true;
}

String valueAsString = this.stripNonDigitsIfRequired( value.toString() );

String digitsAsString;
char checkDigit;
try {
digitsAsString = this.extractVerificationString( valueAsString );
checkDigit = this.extractCheckDigit( valueAsString );
}
catch ( IndexOutOfBoundsException e ) {
return false;
}

List<Integer> digits;

try {
digits = extractDigits( digitsAsString );
}
catch ( NumberFormatException e ) {
return false;
/**
* Parses the {@link String} value as a {@link List} of {@link Integer} objects
*
* @param value the input string to be parsed
*
* @return List of {@code Integer} objects.
*
* @throws NumberFormatException in case any of the characters is not a digit
*/
private List<Integer> extractDigits(final String value) throws NumberFormatException {
List<Integer> digits = new ArrayList<Integer>( value.length() );
char[] chars = value.toCharArray();
for ( char c : chars ) {
digits.add( extractDigit( c ) );
}

return this.isCheckDigitValid( digits, checkDigit );
return digits;
}

public boolean validateOptions() {
private boolean validateOptions() {
if ( this.startIndex < 0 ) {
throw log.getStartIndexCannotBeNegativeException( this.startIndex );
}
Expand Down
Expand Up @@ -16,13 +16,12 @@
*/
package org.hibernate.validator.test.internal.constraintvalidators;

import org.testng.annotations.Test;

import org.hibernate.validator.constraints.Mod10Check;
import org.hibernate.validator.internal.constraintvalidators.Mod10CheckValidator;
import org.hibernate.validator.internal.util.annotationfactory.AnnotationDescriptor;
import org.hibernate.validator.internal.util.annotationfactory.AnnotationFactory;
import org.hibernate.validator.testutil.TestForIssue;
import org.testng.annotations.Test;

import static org.testng.Assert.assertFalse;
import static org.testng.AssertJUnit.assertTrue;
Expand Down Expand Up @@ -184,8 +183,4 @@ private Mod10Check createMod10CheckAnnotation(int start, int end, int checkDigit
private Mod10Check createMod10CheckAnnotation(int start, int end, int checkDigitIndex, boolean ignoreNonDigits) {
return this.createMod10CheckAnnotation( start, end, checkDigitIndex, ignoreNonDigits, 3, 1 );
}

private Mod10Check createMod10CheckAnnotation(boolean ignoreNonDigits, int multiplier, int weight) {
return this.createMod10CheckAnnotation( 0, Integer.MAX_VALUE, -1, ignoreNonDigits, multiplier, weight );
}
}

0 comments on commit b7ad918

Please sign in to comment.