bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1228514 - in /incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit: ./ ModulusValidator.java
Date Sat, 07 Jan 2012 00:31:31 GMT
Author: simonetripodi
Date: Sat Jan  7 00:31:30 2012
New Revision: 1228514

URL: http://svn.apache.org/viewvc?rev=1228514&view=rev
Log:
first checkin of ModulusValidator, imported from commons-validation (see org.apache.commons.validator.routines.checkdigit.ModulusCheckDigit)

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

Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ModulusValidator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ModulusValidator.java?rev=1228514&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ModulusValidator.java
(added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ModulusValidator.java
Sat Jan  7 00:31:30 2012
@@ -0,0 +1,124 @@
+/*
+ * 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.Character.getNumericValue;
+import static java.lang.Character.isDigit;
+
+import java.lang.annotation.Annotation;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+/**
+ * Abstract <b>Modulus</b> Check digit calculation/validation.
+ * <p>
+ * Provides a <i>base</i> class for building <i>modulus</i> Check
+ * Digit routines.
+ * <p>
+ * This implementation only handles <i>numeric</i> codes, such as
+ * <b>EAN-13</b>. For <i>alphanumeric</i> codes such as <b>EAN-128</b>
you
+ * will need to implement/override the <code>toInt()</code> and
+ * <code>toChar()</code> methods.
+ *
+ * @param <A>
+ */
+abstract class ModulusValidator<A extends Annotation>
+    implements ConstraintValidator<A, String> {
+
+    private final int modulus;
+
+    public ModulusValidator(int modulus) {
+        this.modulus = modulus;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public final void initialize(A annotation) {
+        // not needed ATM
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isValid(String code, ConstraintValidatorContext context) {
+        if (code.length() == 0) {
+            return false;
+        }
+        int total = 0;
+        for (int i = 0; i < code.length(); i++) {
+            int lth = code.length();
+            int leftPos  = i + 1;
+            int rightPos = lth - i;
+            try {
+                int charValue = toInt(code.charAt(i), leftPos, rightPos);
+                total += weightedValue(charValue, leftPos, rightPos);
+            } catch (Throwable e) {
+                return false;
+            }
+        }
+        if (total == 0) {
+            return false;
+        }
+        return (total % modulus) == 0;
+    }
+
+    /**
+     * Calculates the <i>weighted</i> value of a character in the
+     * code at a specified position.
+     * <p>
+     * Some modulus routines weight the value of a character
+     * depending on its position in the code (e.g. ISBN-10), while
+     * others use different weighting factors for odd/even positions
+     * (e.g. EAN or Luhn). Implement the appropriate mechanism
+     * required by overriding this method.
+     *
+     * @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
+     */
+    protected abstract int weightedValue(int charValue, int leftPos, int rightPos)
+        throws Exception;
+
+    /**
+     * Convert a character at a specified position to an integer value.
+     * <p>
+     * <b>Note:</b> this implementation only handlers numeric values
+     * For non-numeric characters, override this method to provide
+     * character-->integer conversion.
+     *
+     * @param character The character to convert
+     * @param leftPos The position of the character in the code, counting from left to right
+     * @param rightPos The positionof the character in the code, counting from right to left
+     * @return The integer value of the character
+     */
+    protected static int toInt(char character, int leftPos, int rightPos) {
+        if (isDigit(character)) {
+            return getNumericValue(character);
+        }
+        throw new IllegalArgumentException("Invalid Character["
+                                           + leftPos
+                                           + "] = '"
+                                           + character
+                                           + "'");
+    }
+
+}

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

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

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



Mime
View raw message