package br.cefetrj.sca.dominio; public final class Isbn { final String codigo; public Isbn(String id) { if (id == null || id.isEmpty()) { throw new IllegalArgumentException("Código inválido para ISBN"); } if (!this.validateIsbn13(id)) { throw new IllegalArgumentException("Código inválido para ISBN"); } this.codigo = id; } public String getCodigo() { return codigo; } private boolean validateIsbn13(String isbn) { if (isbn == null) { return false; } // remove any hyphens isbn = isbn.replaceAll("-", ""); // must be a 13 digit ISBN if (isbn.length() != 13) { return false; } try { int tot = 0; for (int i = 0; i < 12; i++) { int digit = Integer.parseInt(isbn.substring(i, i + 1)); tot += (i % 2 == 0) ? digit * 1 : digit * 3; } // checksum must be 0-9. If calculated as 10 then = 0 int checksum = 10 - (tot % 10); if (checksum == 10) { checksum = 0; } return checksum == Integer.parseInt(isbn.substring(12)); } catch (NumberFormatException nfe) { // to catch invalid ISBNs that have non-numeric characters in them return false; } } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((codigo == null) ? 0 : codigo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Isbn other = (Isbn) obj; if (codigo == null) { if (other.codigo != null) return false; } else if (!codigo.equals(other.codigo)) return false; return true; } }