incubator-bval-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1228608 - 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 12:59:51 GMT
Author: simonetripodi
Date: Sat Jan  7 12:59:51 2012
New Revision: 1228608

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

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

Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10.java?rev=1228608&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10.java
(added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10.java
Sat Jan  7 12:59:51 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 = ISBN10Validator.class )
+@Target( { FIELD, ANNOTATION_TYPE, PARAMETER } )
+@Retention( RUNTIME )
+public @interface ISBN10 {
+
+    Class<?>[] groups() default {};
+
+    String message() default "{org.apache.bval.extras.constraints.checkdigit.ISBN10.message}";
+
+    Class<? extends Payload>[] payload() default {};
+
+}

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

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

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

Added: incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10Validator.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10Validator.java?rev=1228608&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10Validator.java
(added)
+++ incubator/bval/trunk/bval-extras/src/main/java/org/apache/bval/extras/constraints/checkdigit/ISBN10Validator.java
Sat Jan  7 12:59:51 2012
@@ -0,0 +1,81 @@
+/*
+ * 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 11 <b>ISBN-10</b> Check Digit calculation/validation.
+ * <p>
+ * ISBN-10 Numbers are a numeric code except for the last (check) digit
+ * which can have a value of "X".
+ * <p>
+ * Check digit calculation is based on <i>modulus 11</i> with digits being weighted
+ * based by their position, from right to left  with the first digit being weighted
+ * 1, the second 2 and so on. If the check digit is calculated as "10" it is converted
+ * to "X".
+ * <p>
+ * <b>N.B.</b> From 1st January 2007 the book industry will start to use a new
13 digit
+ * ISBN number (rather than this 10 digit ISBN number) which uses the EAN-13 / UPC
+ * (see {@link EAN13CheckDigit}) standard.
+ * <p>
+ * For further information see:
+ * <ul>
+ *   <li><a href="http://en.wikipedia.org/wiki/ISBN">Wikipedia - International
+ *       Standard Book Number (ISBN)</a>.</li>
+ *   <li><a href="http://www.isbn.org/standards/home/isbn/transition.asp">ISBN-13
+ *       Transition details</a>.</li>
+ * </ul>
+ */
+public final class ISBN10Validator
+    extends ModulusValidator<EAN13> {
+
+    public ISBN10Validator() {
+        super(11);
+    }
+
+    /**
+     * Calculates the <i>weighted</i> value of a charcter in the
+     * code at a specified position.
+     *
+     * <p>For ISBN-10 (from right to left) digits are weighted
+     * by their position.</p>
+     *
+     * {@inheritDoc}
+     */
+    @Override
+    protected int weightedValue( int charValue, int leftPos, int rightPos )
+            throws Exception {
+        return (charValue * rightPos);
+    }
+
+    /**
+     * <p>Convert a character at a specified position to an
+     * integer value.</p>
+     *
+     * <p>Character 'X' check digit converted to 10.</p>
+     *
+     * {@inheritDoc}
+     */
+    protected int toInt(char character, int leftPos, int rightPos) {
+        if (rightPos == 1 && character == 'X') {
+            return 10;
+        }
+        return super.toInt(character, leftPos, rightPos);
+    }
+
+}

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

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

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

Added: incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/ISBN10CheckDigitTest.java
URL: http://svn.apache.org/viewvc/incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/ISBN10CheckDigitTest.java?rev=1228608&view=auto
==============================================================================
--- incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/ISBN10CheckDigitTest.java
(added)
+++ incubator/bval/trunk/bval-extras/src/test/java/org/apache/bval/extras/constraints/checkdigit/ISBN10CheckDigitTest.java
Sat Jan  7 12:59:51 2012
@@ -0,0 +1,43 @@
+/*
+ * 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;
+
+/**
+ * ISBN-10 Check Digit Test.
+ */
+public class ISBN10CheckDigitTest extends AbstractCheckDigitTest {
+
+    @Override
+    protected ConstraintValidator<? extends Annotation, String> getConstraint() {
+        return new ISBN10Validator();
+    }
+
+    @Override
+    protected String[] getValid() {
+        return new String[] {
+            "1930110995",
+            "020163385X",
+            "1932394354",
+            "1590596277"
+        };
+    }
+
+}

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

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

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



Mime
View raw message