package com.github.norwae.whatiread.data;

import java.util.regex.Pattern;

public final class ISBN13 {
	private static Pattern ISBN_PATTERN = Pattern.compile("97[89]\\d{10}");
	public static ISBN13 parse(String string) {
		if (string != null) {			
			if (ISBN_PATTERN.matcher(string).matches()){
				return new ISBN13(string);
			}
		}
		
		return null;
	}

	private String value;
	
	private ISBN13(String val) {
		value = val;
	}
	
	@Override
	public String toString() {
		return value;
	}
	
	@Override
	public int hashCode() {
		return value.hashCode();
	}
	
	@Override
	public boolean equals(Object o) {
		if (o == this) {
			return true;
		}
		
		if (o instanceof ISBN13) {
			return ((ISBN13) o).value.equals(value);
		}
		
		return false;
	}
}

@@ -121,24 +121,26 @@
 Pattern ISBN
+13
 _PATTERN = P
@@ -178,74 +178,324 @@
 ;%0A%09p
-ublic static ISBN13 parse(String string) %7B%0A%09%09if (string != null) %7B
+rivate static Pattern ISBN10_PATTERN = Pattern.compile(%22%5C%5Cd%7B9%7D%5B0-9Xx%5D%22);%0A%0A%09public static ISBN13 parse(String string) %7B%0A%09%09if (string != null) %7B%0A%09%09%09if (ISBN10_PATTERN.matcher(string).matches()) %7B%0A%09%09%09%09String code = string.substring(0, 9);%0A%09%09%09%09string = %22978%22 + code;%0A%09%09%09%09string = string + isbn13CheckDigit(string);%0A%09%09%09%7D%0A
 %09%09%09%0A
@@ -505,16 +505,18 @@
 if (ISBN
+13
 _PATTERN
@@ -542,18 +542,90 @@
 tches())
+ 
 %7B%0A
+%09%09%09%09if (string.endsWith(isbn13CheckDigit(string.substring(0, 12)))) %7B%0A%09
 %09%09%09%09retu
@@ -643,24 +643,30 @@
 13(string);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D%0A%09%09%0A
@@ -658,26 +658,24 @@
 %09%7D%0A%09%09%09%7D%0A%09%09%7D%0A
-%09%09
 %0A%09%09return nu
@@ -682,16 +682,319 @@
 ll;%0A%09%7D%0A%0A
+%09private static String isbn13CheckDigit(String string) %7B%0A%09%09char%5B%5D chars = string.toCharArray();%0A%09%09int check = 0;%0A%09%09for (int i = 0; i %3C 12; i++) %7B%0A%09%09%09int factor = (i %25 2) == 0 ? 1 : 3;%0A%09%09%09check += factor * (chars%5Bi%5D - '0');%0A%09%09%7D%0A%0A%09%09int checkValue = (10 - (check %25 10)) %25 10;%0A%09%09return %22%22 + checkValue;%0A%09%7D%0A%0A
 %09private
@@ -1008,17 +1008,16 @@
  value;%0A
-%09
 %0A%09privat
@@ -1053,25 +1053,24 @@
 e = val;%0A%09%7D%0A
-%09
 %0A%09@Override%0A
@@ -1112,25 +1112,24 @@
 n value;%0A%09%7D%0A
-%09
 %0A%09@Override%0A
@@ -1183,17 +1183,16 @@
 e();%0A%09%7D%0A
-%09
 %0A%09@Overr
@@ -1269,18 +1269,16 @@
 ue;%0A%09%09%7D%0A
-%09%09
 %0A%09%09if (o
@@ -1347,18 +1347,16 @@
 e);%0A%09%09%7D%0A
-%09%09
 %0A%09%09retur

Check ISBN13s, accept ISBN10s as well