You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by le...@apache.org on 2009/09/08 02:40:57 UTC
svn commit: r812313 -
/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java
Author: lektran
Date: Tue Sep 8 00:40:57 2009
New Revision: 812313
URL: http://svn.apache.org/viewvc?rev=812313&view=rev
Log:
Add EAN validation
Modified:
ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java
Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java?rev=812313&r1=812312&r2=812313&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java (original)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java Tue Sep 8 00:40:57 2009
@@ -1298,19 +1298,32 @@
}
public static char calcUpcChecksum(String upc) {
- if (upc != null && upc.length() == 12) {
- upc = upc.substring(0, 11);
+ return calcChecksum(upc, 12);
+ }
+
+ public static boolean isValidEan(String ean) {
+ if (ean == null || ean.length() != 13) {
+ throw new IllegalArgumentException("Invalid UPC length; must be 12 characters");
+ }
+ char csum = ean.charAt(13);
+ char calcSum = calcChecksum(ean, 13);
+ return csum == calcSum;
+ }
+
+ public static char calcChecksum(String value, int length) {
+ if (value != null && value.length() == length + 1) {
+ value = value.substring(0, length);
}
- if (upc == null || upc.length() != 11) {
- throw new IllegalArgumentException("Illegal size of UPC; must be 11 characters");
+ if (value == null || value.length() != length) {
+ throw new IllegalArgumentException("Illegal size of value; must be either" + length + " or " + (length + 1) + " characters");
}
int oddsum = 0;
int evensum = 0;
- for (int i = upc.length() - 1; i >= 0; i--) {
- if ((upc.length() - i) % 2 == 0) {
- evensum += Character.digit(upc.charAt(i), 10);
+ for (int i = value.length() - 1; i >= 0; i--) {
+ if ((value.length() - i) % 2 == 0) {
+ evensum += Character.digit(value.charAt(i), 10);
} else {
- oddsum += Character.digit(upc.charAt(i), 10);
+ oddsum += Character.digit(value.charAt(i), 10);
}
}
int check = 10 - ((evensum + 3 * oddsum) % 10);
Re: svn commit: r812313 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java
Posted by Scott Gray <sc...@hotwaxmedia.com>.
Thanks!
Regards
Scott
On 8/09/2009, at 12:44 PM, Adam Heath wrote:
> lektran@apache.org wrote:
>> Author: lektran
>> Date: Tue Sep 8 00:40:57 2009
>> New Revision: 812313
>>
>> Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/
>> UtilValidate.java
>> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java?rev=812313&r1=812312&r2=812313&view=diff
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =
>> =====================================================================
>> --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/
>> UtilValidate.java (original)
>> +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/
>> UtilValidate.java Tue Sep 8 00:40:57 2009
>> @@ -1298,19 +1298,32 @@
>> }
>>
>> public static char calcUpcChecksum(String upc) {
>> - if (upc != null && upc.length() == 12) {
>> - upc = upc.substring(0, 11);
>> + return calcChecksum(upc, 12);
>> + }
>> +
>> + public static boolean isValidEan(String ean) {
>> + if (ean == null || ean.length() != 13) {
>> + throw new IllegalArgumentException("Invalid UPC
>> length; must be 12 characters");
>
> s/UPC/EAN/
>
> .
Re: svn commit: r812313 - /ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java
Posted by Adam Heath <do...@brainfood.com>.
lektran@apache.org wrote:
> Author: lektran
> Date: Tue Sep 8 00:40:57 2009
> New Revision: 812313
>
> Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java
> URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java?rev=812313&r1=812312&r2=812313&view=diff
> ==============================================================================
> --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java (original)
> +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/UtilValidate.java Tue Sep 8 00:40:57 2009
> @@ -1298,19 +1298,32 @@
> }
>
> public static char calcUpcChecksum(String upc) {
> - if (upc != null && upc.length() == 12) {
> - upc = upc.substring(0, 11);
> + return calcChecksum(upc, 12);
> + }
> +
> + public static boolean isValidEan(String ean) {
> + if (ean == null || ean.length() != 13) {
> + throw new IllegalArgumentException("Invalid UPC length; must be 12 characters");
s/UPC/EAN/
.