mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-17 00:25:54 +00:00
* Issue #2377: The repository code has been refactored to use the record class * Issue #2377: Refactored according to the rules defined for the repo code * Issue #2377: Refactored according to the rules defined for the repo code * Issue #2377: Refactored according to the rules defined for the repo code
This commit is contained in:
@@ -54,10 +54,10 @@ public class App {
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
|
||||
LOGGER.info(Validator.of(user).validate(User::getName, Objects::nonNull, "name is null")
|
||||
.validate(User::getName, name -> !name.isEmpty(), "name is empty")
|
||||
.validate(User::getEmail, email -> !email.contains("@"), "email doesn't contains '@'")
|
||||
.validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get()
|
||||
LOGGER.info(Validator.of(user).validate(User::name, Objects::nonNull, "name is null")
|
||||
.validate(User::name, name -> !name.isEmpty(), "name is empty")
|
||||
.validate(User::email, email -> !email.contains("@"), "email doesn't contains '@'")
|
||||
.validate(User::age, age -> age > 20 && age < 30, "age isn't between...").get()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,43 +25,13 @@
|
||||
package com.iluwatar.monad;
|
||||
|
||||
/**
|
||||
* User Definition.
|
||||
* Record class.
|
||||
*
|
||||
* @param name - name
|
||||
* @param age - age
|
||||
* @param sex - sex
|
||||
* @param email - email address
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
private final Sex sex;
|
||||
private final String email;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param name - name
|
||||
* @param age - age
|
||||
* @param sex - sex
|
||||
* @param email - email address
|
||||
*/
|
||||
public User(String name, int age, Sex sex, String email) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.sex = sex;
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public Sex getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
public record User(String name, int age, Sex sex, String email) {
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class MonadTest {
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> Validator.of(tom)
|
||||
.validate(User::getName, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.get()
|
||||
);
|
||||
}
|
||||
@@ -52,8 +52,8 @@ class MonadTest {
|
||||
assertThrows(
|
||||
IllegalStateException.class,
|
||||
() -> Validator.of(john)
|
||||
.validate(User::getName, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::getAge, age -> age > 21, "user is underage")
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::age, age -> age > 21, "user is underage")
|
||||
.get()
|
||||
);
|
||||
}
|
||||
@@ -62,10 +62,10 @@ class MonadTest {
|
||||
void testForValid() {
|
||||
var sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
|
||||
var validated = Validator.of(sarah)
|
||||
.validate(User::getName, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::getAge, age -> age > 21, "user is underage")
|
||||
.validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
|
||||
.validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
|
||||
.validate(User::name, Objects::nonNull, "name cannot be null")
|
||||
.validate(User::age, age -> age > 21, "user is underage")
|
||||
.validate(User::sex, sex -> sex == Sex.FEMALE, "user is not female")
|
||||
.validate(User::email, email -> email.contains("@"), "email does not contain @ sign")
|
||||
.get();
|
||||
assertSame(validated, sarah);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user