Skip to content

Package: PersonFinderImpl2b$Status

PersonFinderImpl2b$Status

nameinstructionbranchcomplexitylinemethod
PersonFinderImpl2b.Status(List, Pattern, Pattern)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.thesefoolishthings.examples.extendedfinderexample;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.List;
30: import java.util.regex.Pattern;
31: import java.util.stream.Collectors;
32: import it.tidalwave.util.spi.HierarchicFinderSupport;
33: import it.tidalwave.thesefoolishthings.examples.person.Person;
34:
35: /***************************************************************************************************************************************************************
36: *
37: * A variant of {@link PersonRegistryImpl2a} that uses an internal status class.
38: *
39: * @author Fabrizio Giudici
40: *
41: **************************************************************************************************************************************************************/
42: public class PersonFinderImpl2b extends HierarchicFinderSupport<Person, PersonFinder> implements PersonFinder
43: {
44: private static final long serialVersionUID = 51438631451345L;
45:
46: // START SNIPPET: public-constructor-and-fields
47: record Status (@Nonnull List<Person> persons, @Nonnull Pattern firstNamePattern, @Nonnull Pattern lastNamePattern)
48: // implements Serializable
49: {
50: }
51:
52: @Nonnull
53: private final Status status;
54:
55: // This is for public use
56: public PersonFinderImpl2b (@Nonnull final List<Person> persons)
57: {
58: this(new Status(persons, Pattern.compile(".*"), Pattern.compile(".*")));
59: }
60: // END SNIPPET: public-constructor-and-fields
61:
62: // This is for internal purposes
63: // This could be generated by Lombok's @RequiredArgsConstructor
64: // START SNIPPET: private-constructor
65: private PersonFinderImpl2b (@Nonnull final Status status)
66: {
67: this.status = status;
68: }
69: // END SNIPPET: private-constructor
70:
71: // START SNIPPET: clone-constructor
72: public PersonFinderImpl2b (@Nonnull final PersonFinderImpl2b other, @Nonnull final Object override)
73: {
74: super(other, override);
75: final var source = getSource(Status.class, other.status, override);
76: this.status = new Status(source.persons, source.firstNamePattern, source.lastNamePattern);
77: }
78: // END SNIPPET: clone-constructor
79:
80: // START SNIPPET: new-methods
81: @Override @Nonnull
82: public PersonFinder withFirstName (@Nonnull final String regex)
83: {
84: return clonedWith(new Status(status.persons, Pattern.compile(regex), status.lastNamePattern));
85: }
86:
87: @Override @Nonnull
88: public PersonFinder withLastName (@Nonnull final String regex)
89: {
90: return clonedWith(new Status(status.persons, status.firstNamePattern, Pattern.compile(regex)));
91: }
92: // END SNIPPET: new-methods
93:
94: // START SNIPPET: computeResults
95: @Override @Nonnull
96: protected List<Person> computeResults()
97: {
98: return status.persons.stream()
99: .filter(p -> status.firstNamePattern.matcher(p.getFirstName()).matches()
100: && status.lastNamePattern.matcher(p.getLastName()).matches())
101: .collect(Collectors.toList());
102: }
103: // END SNIPPET: computeResults
104: }