Übung Java Lambda Validatorhttps://www.std.ch/kurs-kurse-jpf2?&atomTue, 19 Mar 2024 10:25:38 +0000stack.ch
https://stack.ch/
07e686c5-e5db-11ee-8c11-005056bb85fbSimtech AG - Ausbildung - Java - Kurs Java Advanced - Kurs Java Advanced - Ressourcen - Kurs Java Advanced - Übungen Lambda & Stream API - Übung Java Lambda Validator
https://www.std.ch/kurs-kurse-jpf2
07e688a0-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000Übung Java Lambda Validator
https://www.std.ch/kurs-kurse-jpf2
07e689d9-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000
https://www.std.ch/kurs-kurse-jpf2
07e68ace-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000Die folgenden Listings zeigen eine Validator Lösung mit enum Typ klassisch ohne Lambda Expressions: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();
}
}
}
https://www.std.ch/kurs-kurse-jpf2
07e690ba-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000Die Dateien der Vorlage finden Sie auch hier
https://www.std.ch/kurs-kurse-jpf2
07e695b6-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000In einem ersten Schritt möchten wir die Vorlage mit dem Supplier FunctionalInterface anpassen, so dass die Validierung über solches erfolgen kann. Das folgende Listing zeigt das angepasste Demo Programm: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();
}
}
},
https://www.std.ch/kurs-kurse-jpf2
07e69833-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000Eine mögliche Lösung mit Supplier FunctionalInterface finden Sie hier
https://www.std.ch/kurs-kurse-jpf2
07e69c73-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000Jetzt wollen wir die Validierungsregeln über ein eigenen Validator FunctionalInterface erweitern. Das folgende Listing zeigt die Enum Klasse: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.
https://www.std.ch/kurs-kurse-jpf2
07e6a1db-e5db-11ee-8c11-005056bb85fbTue, 19 Mar 2024 10:25:38 +0000Eine mögliche Lösung mit Callable FunctionalInterface finden Sie hierÜber uns
https://www.std.ch/about
Tue, 19 Mar 2024 10:25:38 +000007e6a8aa-e5db-11ee-8c11-005056bb85fbAktuell
https://www.std.ch/
Tue, 19 Mar 2024 10:25:38 +000007e6a955-e5db-11ee-8c11-005056bb85fbAGB
https://www.std.ch/agb
Tue, 19 Mar 2024 10:25:38 +000007e6a9f2-e5db-11ee-8c11-005056bb85fbBildungswege
https://www.std.ch/bildungswege
Tue, 19 Mar 2024 10:25:38 +000007e6aae8-e5db-11ee-8c11-005056bb85fbBlog
https://www.std.ch/blog
Tue, 19 Mar 2024 10:25:38 +000007e6ab9e-e5db-11ee-8c11-005056bb85fbRufen Sie mich an
https://www.std.ch/callus
Tue, 19 Mar 2024 10:25:38 +000007e6ac40-e5db-11ee-8c11-005056bb85fbCharts
https://www.std.ch/charts
Tue, 19 Mar 2024 10:25:38 +000007e6acf7-e5db-11ee-8c11-005056bb85fbConsulting
https://www.std.ch/consulting
Tue, 19 Mar 2024 10:25:38 +000007e6ad94-e5db-11ee-8c11-005056bb85fbKontakt
https://www.std.ch/contact
Tue, 19 Mar 2024 10:25:38 +000007e6ae2c-e5db-11ee-8c11-005056bb85fbAusbildung/Kurse
https://www.std.ch/education
Tue, 19 Mar 2024 10:25:38 +000007e6b519-e5db-11ee-8c11-005056bb85fbSoftware Engineering
https://www.std.ch/engineering
Tue, 19 Mar 2024 10:25:38 +000007e6b5c8-e5db-11ee-8c11-005056bb85fbFreelancer
https://www.std.ch/freelancer
Tue, 19 Mar 2024 10:25:38 +000007e6b666-e5db-11ee-8c11-005056bb85fbImpressum
https://www.std.ch/impressum
Tue, 19 Mar 2024 10:25:38 +000007e6b703-e5db-11ee-8c11-005056bb85fbKursleiter
https://www.std.ch/kursleiter
Tue, 19 Mar 2024 10:25:38 +000007e6b79e-e5db-11ee-8c11-005056bb85fbNetzwerk
https://www.std.ch/network
Tue, 19 Mar 2024 10:25:38 +000007e6b854-e5db-11ee-8c11-005056bb85fbReferenzen
https://www.std.ch/references
Tue, 19 Mar 2024 10:25:38 +000007e6b8eb-e5db-11ee-8c11-005056bb85fbSitemap
https://www.std.ch/sitemap
Tue, 19 Mar 2024 10:25:38 +000007e6b983-e5db-11ee-8c11-005056bb85fbTools
https://www.std.ch/tools
Tue, 19 Mar 2024 10:25:38 +000007e6ba1d-e5db-11ee-8c11-005056bb85fbVision
https://www.std.ch/vision
Tue, 19 Mar 2024 10:25:38 +000007e6bab1-e5db-11ee-8c11-005056bb85fb