1 package epam.legalage;
2
3 import java.time.Clock;
4 import java.time.LocalDate;
5 import java.time.Period;
6 import java.util.function.Predicate;
7
8
9
10
11 public final class LegalAgePredicate implements Predicate<LocalDate> {
12
13 private static final int LEGAL_AGE = 18;
14 private final Clock clock;
15
16
17
18
19 public LegalAgePredicate() {
20 this(Clock.systemDefaultZone());
21 }
22
23
24
25
26
27
28 public LegalAgePredicate(Clock clock) {
29 this.clock = clock;
30 }
31
32
33
34
35
36
37
38 @Override
39 public boolean test(LocalDate dateOfBirth) {
40 var currentDate = LocalDate.now(clock);
41 var age = Period.between(dateOfBirth, currentDate).getYears();
42 return age >= LEGAL_AGE;
43 }
44 }