Skip to content

Commit a142c06

Browse files
Java 11 migraiton: monad
1 parent f1b27ef commit a142c06

File tree

5 files changed

+42
-36
lines changed

5 files changed

+42
-36
lines changed

monad/src/main/java/com/iluwatar/monad/App.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,8 @@
4141
* instance of a plain object with {@link Validator#of(Object)} and validates it {@link
4242
* Validator#validate(Function, Predicate, String)} against given predicates.
4343
*
44-
* <p>As a validation result {@link Validator#get()} it either returns valid object {@link
45-
* Validator#t} or throws a list of exceptions {@link Validator#exceptions} collected during
46-
* validation.
44+
* <p>As a validation result {@link Validator#get()} either returns valid object
45+
* or throws {@link IllegalStateException} with list of exceptions collected during validation.
4746
*/
4847
public class App {
4948

@@ -55,10 +54,10 @@ public class App {
5554
* @param args command line args
5655
*/
5756
public static void main(String[] args) {
58-
User user = new User("user", 24, Sex.FEMALE, "foobar.com");
57+
var user = new User("user", 24, Sex.FEMALE, "foobar.com");
5958
LOGGER.info(Validator.of(user).validate(User::getName, Objects::nonNull, "name is null")
6059
.validate(User::getName, name -> !name.isEmpty(), "name is empty")
61-
.validate(User::getEmail, email -> !email.contains("@"), "email doesn't containt '@'")
60+
.validate(User::getEmail, email -> !email.contains("@"), "email doesn't contains '@'")
6261
.validate(User::getAge, age -> age > 20 && age < 30, "age isn't between...").get()
6362
.toString());
6463
}

monad/src/main/java/com/iluwatar/monad/User.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
*/
2929
public class User {
3030

31-
private String name;
32-
private int age;
33-
private Sex sex;
34-
private String email;
31+
private final String name;
32+
private final int age;
33+
private final Sex sex;
34+
private final String email;
3535

3636
/**
3737
* Constructor.

monad/src/main/java/com/iluwatar/monad/Validator.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,21 @@ public Validator<T> validate(Predicate<T> validation, String message) {
8585
}
8686

8787
/**
88-
* Extension for the {@link Validator#validate(Function, Predicate, String)} method, dedicated for
89-
* objects, that need to be projected before requested validation.
88+
* Extension for the {@link Validator#validate(Predicate, String)} method, dedicated for objects,
89+
* that need to be projected before requested validation.
9090
*
9191
* @param projection function that gets an objects, and returns projection representing element to
9292
* be validated.
93-
* @param validation see {@link Validator#validate(Function, Predicate, String)}
94-
* @param message see {@link Validator#validate(Function, Predicate, String)}
95-
* @param <U> see {@link Validator#validate(Function, Predicate, String)}
93+
* @param validation see {@link Validator#validate(Predicate, String)}
94+
* @param message see {@link Validator#validate(Predicate, String)}
95+
* @param <U> see {@link Validator#validate(Predicate, String)}
9696
* @return this
9797
*/
98-
public <U> Validator<T> validate(Function<T, U> projection, Predicate<U> validation,
99-
String message) {
98+
public <U> Validator<T> validate(
99+
Function<T, U> projection,
100+
Predicate<U> validation,
101+
String message
102+
) {
100103
return validate(projection.andThen(validation::test)::apply, message);
101104
}
102105

@@ -110,7 +113,7 @@ public T get() throws IllegalStateException {
110113
if (exceptions.isEmpty()) {
111114
return obj;
112115
}
113-
IllegalStateException e = new IllegalStateException();
116+
var e = new IllegalStateException();
114117
exceptions.forEach(e::addSuppressed);
115118
throw e;
116119
}

monad/src/test/java/com/iluwatar/monad/AppTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public class AppTest {
3232

3333
@Test
3434
public void testMain() {
35-
String[] args = {};
36-
App.main(args);
35+
App.main(new String[]{});
3736
}
3837

3938
}

monad/src/test/java/com/iluwatar/monad/MonadTest.java

+22-17
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,46 @@
2323

2424
package com.iluwatar.monad;
2525

26-
import org.junit.jupiter.api.Test;
27-
28-
import java.util.Objects;
29-
3026
import static org.junit.jupiter.api.Assertions.assertSame;
3127
import static org.junit.jupiter.api.Assertions.assertThrows;
3228

29+
import java.util.Objects;
30+
import org.junit.jupiter.api.Test;
31+
3332
/**
3433
* Test for Monad Pattern
3534
*/
3635
public class MonadTest {
3736

3837
@Test
3938
public void testForInvalidName() {
40-
User tom = new User(null, 21, Sex.MALE, "tom@foo.bar");
41-
assertThrows(IllegalStateException.class, () -> {
42-
Validator.of(tom).validate(User::getName, Objects::nonNull, "name cannot be null").get();
43-
});
39+
var tom = new User(null, 21, Sex.MALE, "tom@foo.bar");
40+
assertThrows(
41+
IllegalStateException.class,
42+
() -> Validator.of(tom)
43+
.validate(User::getName, Objects::nonNull, "name cannot be null")
44+
.get()
45+
);
4446
}
4547

4648
@Test
4749
public void testForInvalidAge() {
48-
User john = new User("John", 17, Sex.MALE, "john@qwe.bar");
49-
assertThrows(IllegalStateException.class, () -> {
50-
Validator.of(john).validate(User::getName, Objects::nonNull, "name cannot be null")
51-
.validate(User::getAge, age -> age > 21, "user is underaged")
52-
.get();
53-
});
50+
var john = new User("John", 17, Sex.MALE, "john@qwe.bar");
51+
assertThrows(
52+
IllegalStateException.class,
53+
() -> Validator.of(john)
54+
.validate(User::getName, Objects::nonNull, "name cannot be null")
55+
.validate(User::getAge, age -> age > 21, "user is underage")
56+
.get()
57+
);
5458
}
5559

5660
@Test
5761
public void testForValid() {
58-
User sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
59-
User validated = Validator.of(sarah).validate(User::getName, Objects::nonNull, "name cannot be null")
60-
.validate(User::getAge, age -> age > 21, "user is underaged")
62+
var sarah = new User("Sarah", 42, Sex.FEMALE, "sarah@det.org");
63+
var validated = Validator.of(sarah)
64+
.validate(User::getName, Objects::nonNull, "name cannot be null")
65+
.validate(User::getAge, age -> age > 21, "user is underage")
6166
.validate(User::getSex, sex -> sex == Sex.FEMALE, "user is not female")
6267
.validate(User::getEmail, email -> email.contains("@"), "email does not contain @ sign")
6368
.get();

0 commit comments

Comments
 (0)