package ch.std.jup8.lambda.validator.classic;
public enum StringValidatorEnum {
STRING_8 {
@Override
public void validate(String s) throws ValidatorException {
if (s instanceof String) {
System.out.println();
}
if (s.length() > 8) {
throw new ValidatorException(this.name());
}
}
},
STRING_16 {
@Override
public void validate(String s) throws ValidatorException {
if (s.length() > 16) {
throw new ValidatorException(this.name());
}
}
},
STRING_32 {
@Override
public void validate(String s) throws ValidatorException {
if (s.length() > 32) {
throw new ValidatorException(this.name());
}
}
};
private StringValidatorEnum() {
}
public abstract void validate(String s) throws ValidatorException;
}
package ch.std.jup8.lambda.validator.classic;
public class ValidatorException extends Exception {
private static final long serialVersionUID = 3493994521286475516L;
public ValidatorException() {
super();
}
public ValidatorException(String message) {
super(message);
}
}
package ch.std.jup8.lambda.validator.classic;
public class ValidatorDemo {
public static void main(String[] args) {
String s = "1234567890";
try {
StringValidatorEnum.STRING_32.validate(s);
StringValidatorEnum.STRING_16.validate(s);
StringValidatorEnum.STRING_8.validate(s);
} catch (ValidatorException e) {
e.printStackTrace();
}
}
}
package ch.std.jup8.lambda.validator.lambda.supplier;
public class ValidatorDemo {
public static void main(String[] args) {
String s = "1234567890";
try {
StringValidator.STRING_32.validate(() -> s);
StringValidator.STRING_16.validate(() -> s);
StringValidator.STRING_8.validate(() -> s);
} catch (ValidatorException e) {
e.printStackTrace();
}
}
},
package ch.std.jup8.lambda.validator.lambda;
import java.util.function.Supplier;
public enum StringValidatorEnum {
STRING_8(s -> {
if (s.get().toString().length() > 8) {
throw new ValidatorException("min length is 8");
}
}),
STRING_16(s -> {
if (s.get().toString().length() > 16) {
throw new ValidatorException("min length is 16");
}
}),
STRING_32(s -> {
if (s.get().toString().length() > 32) {
throw new ValidatorException("min length is 32");
}
});
private Validator<String> v;
private StringValidatorEnum(Validator<String> v) {
this.v = v;
}
public void validate(Supplier<String> s) throws ValidatorException {
v.validate(s);
}
}
Programmieren Sie das Validator FunctionalInterface, so dass der enum Typ kompiliert. Das Demo Programm sollte identisch bleiben.Das folgende Listing zeigt den Einsatz eines allgemeinen AnyValidators:
// check null
try {
AnyValidator.validate(supplier -> {
if (supplier.get() == null) {
throw new ValidatorException();
}
}, null);
} catch (ValidatorException e) {
e.printStackTrace();
}
// check is number
try {
AnyValidator.validate(supplier -> {
if (supplier.get() == null) {
throw new ValidatorException();
}
Integer.parseInt(supplier.get());
}, "123");
} catch (ValidatorException e) {
e.printStackTrace();
}
Programmieren Sie den AnyValidator (generic Interface), so dass das Listing kompiliert als Teil des Demo Programms.