incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1228625 - in /incubator/bval/trunk/bval-extras/src: main/java/org/apache/bval/extras/constraints/checkdigit/ test/java/org/apache/bval/extras/constraints/checkdigit/
Date Sat, 07 Jan 2012 14:46:01 GMT
Author: simonetripodi
Date: Sat Jan  7 14:46:01 2012
New Revision: 1228625

URL: http://svn.apache.org/viewvc?rev=1228625&view=rev
Log:
added Luhn validation, imported and readapted from commons-validations

Added:
    incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java
  (with props)
    incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java
  (with props)
    incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java
  (with props)

Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java?rev=1228625&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java
(added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java
Sat Jan  7 14:46:01 2012
@@ -0,0 +1,54 @@
+/*
+ * 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 org.apache.bval.extras.constraints.checkdigit;
+
+import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+/**
+ * <p>
+ * --
+ * TODO - This class is NOT part of the bean_validation spec and might disappear
+ * as soon as a final version of the specification contains a similar functionality.
+ * --
+ * </p>
+ * Description: annotation to validate a java.io.File is a directory<br/>
+ */
+@Documented
+@Constraint( validatedBy = LuhnValidator.class )
+@Target( { FIELD, ANNOTATION_TYPE, PARAMETER } )
+@Retention( RUNTIME )
+public @interface Luhn {
+
+    Class<?>[] groups() default {};
+
+    String message() default "{org.apache.bval.extras.constraints.checkdigit.Luhn.message}";
+
+    Class<? extends Payload>[] payload() default {};
+
+}

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/Luhn.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java?rev=1228625&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java
(added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java
Sat Jan  7 14:46:01 2012
@@ -0,0 +1,65 @@
+/*
+ * 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 org.apache.bval.extras.constraints.checkdigit;
+
+/**
+ * Modulus 10 <b>Luhn</b> Check Digit calculation/validation.
+ * <p>
+ * Luhn check digits are used, for example, by:
+ * <ul>
+ *    <li><a href="http://en.wikipedia.org/wiki/Credit_card">Credit Card Numbers</a></li>
+ *    <li><a href="http://en.wikipedia.org/wiki/IMEI">IMEI Numbers</a>
- International
+ *        Mobile Equipment Identity Numbers</li>
+ * </ul>
+ * Check digit calculation is based on <i>modulus 10</i> with digits in
+ * an <i>odd</i> position (from right to left) being weighted 1 and <i>even</i>
+ * position digits being weighted 2 (weighted values greater than 9 have 9 subtracted).
+ * <p>
+ * See <a href="http://en.wikipedia.org/wiki/Luhn_algorithm">Wikipedia</a>
+ * for more details.
+ */
+public final class LuhnValidator
+    extends ModulusValidator<Luhn> {
+
+    /** weighting given to digits depending on their right position */
+    private static final int[] POSITION_WEIGHT = new int[] {2, 1};
+
+    public LuhnValidator() {
+        super(10);
+    }
+
+    /**
+     * <p>Calculates the <i>weighted</i> value of a charcter in the
+     * code at a specified position.</p>
+     *
+     * <p>For Luhn (from right to left) <b>odd</b> digits are weighted
+     * with a factor of <b>one</b> and <b>even</b> digits with a
factor
+     * of <b>two</b>. Weighted values > 9, have 9 subtracted</p>
+     *
+     * {@inheritDoc}
+     */
+    @Override
+    protected int weightedValue( int charValue, int leftPos, int rightPos )
+            throws Exception {
+        int weight = POSITION_WEIGHT[rightPos % 2];
+        int weightedValue = (charValue * weight);
+        return (weightedValue > 9 ? (weightedValue - 9) : weightedValue);
+    }
+
+}

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/LuhnValidator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java?rev=1228625&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java
(added)
+++ incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java
Sat Jan  7 14:46:01 2012
@@ -0,0 +1,57 @@
+/*
+ * 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 org.apache.bval.extras.constraints.checkdigit;
+
+import java.lang.annotation.Annotation;
+
+import javax.validation.ConstraintValidator;
+
+/**
+ * Luhn Check Digit Test.
+ */
+public class LuhnCheckDigitTest extends AbstractCheckDigitTest {
+
+    private static final String VALID_VISA       = "4417123456789113";
+
+    private static final String VALID_SHORT_VISA = "4222222222222";
+
+    private static final String VALID_AMEX       = "378282246310005";
+
+    private static final String VALID_MASTERCARD = "5105105105105100";
+
+    private static final String VALID_DISCOVER   = "6011000990139424";
+
+    private static final String VALID_DINERS     = "30569309025904";
+
+    @Override
+    protected ConstraintValidator<? extends Annotation, String> getConstraint() {
+        return new LuhnValidator();
+    }
+
+    @Override
+    protected String[] getValid() {
+        return new String[] {
+            VALID_VISA,
+            VALID_SHORT_VISA,
+            VALID_AMEX,
+            VALID_MASTERCARD,
+            VALID_DISCOVER,
+            VALID_DINERS
+        };
+    }
+
+}

Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/LuhnCheckDigitTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message