Based on suggestions suggestions and some thoughts I made some refactorings of my classes to hold EAN13 codes, I created a contract interface which now is extended by BarCode
classes, since there will be other types of BarCodes
, like DUN-14, UPC-A...
Based on suggestions and some thoughts I made some refactorings of my classes to hold EAN13 codes, I created a contract interface which now is extended by BarCode
classes, since there will be other types of BarCodes
, like DUN-14, UPC-A...
Based on suggestions and some thoughts I made some refactorings of my classes to hold EAN13 codes, I created a contract interface which now is extended by BarCode
classes, since there will be other types of BarCodes
, like DUN-14, UPC-A...
Hold and validate eanEAN code - Refactoredfollow-up
Based on suggestions and some thoughts I made some refactorings of my classes to hold EAN13 codes.
, I createcreated a contract interface which now is extended by BarCodeBarCode
classes, since there will be other types of BarCodesBarCodes
, like DUN-14, UPC-A...
I changechanged the pre-generate exception creating a custom InvalidBarCodeExceptionInvalidBarCodeException
exception.
In the first moment, this was created as a RuntimeExceptionRuntimeException
. But I think this is the case where checked is much better suited, because I'm forced to deal with invalid codes.
The validation is movewas moved to a PredicatePredicate
in a separated class.:
In the productProduct
class now is simply a set method (Itit will be chancedchanged to BarCodeBarCode
interface).:
Product p = new Product();
p.setDescription(name);
p.setUrl(url);
try {
BarCode ean = new Ean13Factory().create(code);
//TODO: refactoring.
p.setEan((Ean13) ean);
} catch (InvalidBarCodeException e) {
logInvalidCode(e, code);
}
someone has more notesDoes anyone have other suggestions?
Hold and validate ean code - Refactored
Based on suggestions and some thoughts I made some refactorings of my classes to hold EAN13 codes.
I create a contract interface which now is extended by BarCode classes, since there will be other types of BarCodes, like DUN-14, UPC-A...
I change the pre-generate exception creating a custom InvalidBarCodeException exception.
In first moment, this was created as a RuntimeException. But I think this is the case where checked is much better suited, because I'm forced to deal with invalid codes.
The validation is move to a Predicate in a separated class.
In the product class now is simply a set method (It will be chanced to BarCode interface).
Product p = new Product();
p.setDescription(name);
p.setUrl(url);
try {
BarCode ean = new Ean13Factory().create(code);
//TODO: refactoring.
p.setEan((Ean13) ean);
} catch (InvalidBarCodeException e) {
logInvalidCode(e, code);
}
someone has more notes?
Hold and validate EAN code - follow-up
Based on suggestions and some thoughts I made some refactorings of my classes to hold EAN13 codes, I created a contract interface which now is extended by BarCode
classes, since there will be other types of BarCodes
, like DUN-14, UPC-A...
I changed the pre-generate exception creating a custom InvalidBarCodeException
exception.
In the first moment, this was created as a RuntimeException
. But I think this is the case where checked is much better suited, because I'm forced to deal with invalid codes.
The validation was moved to a Predicate
in a separated class:
In the Product
class now is simply a set method (it will be changed to BarCode
interface):
Product p = new Product();
p.setDescription(name);
p.setUrl(url);
try {
BarCode ean = new Ean13Factory().create(code);
//TODO: refactoring.
p.setEan((Ean13) ean);
} catch (InvalidBarCodeException e) {
logInvalidCode(e, code);
}
Does anyone have other suggestions?
Hold and validate ean code - Refactored
Based on suggestions and some thoughts I made some refactorings of my classes to hold EAN13 codes.
I create a contract interface which now is extended by BarCode classes, since there will be other types of BarCodes, like DUN-14, UPC-A...
public interface BarCode {}
I decide to move all the validations to factory class, so the EAN13 class looked like this:
@Embeddable
public class Ean13 implements BarCode {
@Column(name = "ean_code", nullable = true, length = 13)
private String code;
public Ean13() {
}
public Ean13(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
I change the pre-generate exception creating a custom InvalidBarCodeException exception.
In first moment, this was created as a RuntimeException. But I think this is the case where checked is much better suited, because I'm forced to deal with invalid codes.
public class InvalidBarCodeException extends Exception {
private String code;
private static final String INVALID_EAN = "INVALID EAN CODE";
public InvalidBarCodeException(String code) {
super(INVALID_EAN + " "+code);
this.code = code;
}
public String getCode() {
return code;
}
}
The validation is move to a Predicate in a separated class.
public class BarCodePredicate {
public static Predicate<String> isValidEan13() {
return p -> isValid(p);
}
private static boolean isValid(String code) {
if (code == null || code.length() != 13) {
return false;
}
if (!CharMatcher.DIGIT.matchesAllOf(code)) {
return false;
}
String codeWithoutVd = code.substring(0, 12);
int pretendVd = Integer.valueOf(code.substring(12, 13));
String[] evenOdd = SplitterByIndex.split(codeWithoutVd, idx -> idx % 2 == 0);
int evenSum = sumStringDigits(evenOdd[0]);
int oddSum = sumStringDigits(evenOdd[1]);
int oddFator = oddSum * 3;
int sumResult = oddFator + evenSum;
int dv = getEanVd(sumResult);
if (pretendVd != dv) {
return false;
}
return true;
}
private static int sumStringDigits(String s) {
return s.chars().map(n ->
Character.getNumericValue(n)
).sum();
}
private static int getEanVd(int s) {
return 10 - (s % 10);
}
}
So I use it in a factory class:
public interface BarCodeFactory {
BarCode create(String code) throws InvalidBarCodeException;
default boolean isValid(String code, Predicate<String> predicate) {
return predicate.test(code);
}
}
public class Ean13Factory implements BarCodeFactory {
@Override
public BarCode create(String code) throws InvalidBarCodeException {
if (!isValid(code, BarCodePredicate.isValidEan13())){
throw new InvalidBarCodeException(code);
}
return new Ean13(code);
}
}
In the product class now is simply a set method (It will be chanced to BarCode interface).
public class Product{
public void setEan(Ean13 ean) {
this.ean = ean;
}
}
And invalid codes are treated outside:
Product p = new Product();
p.setDescription(name);
p.setUrl(url);
try {
BarCode ean = new Ean13Factory().create(code);
//TODO: refactoring.
p.setEan((Ean13) ean);
} catch (InvalidBarCodeException e) {
logInvalidCode(e, code);
}
someone has more notes?