Skip to contentMethod: PersonFinderImpl2b(List)
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 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 javax.annotation.Nonnull;
29: import java.util.List;
30: import java.util.regex.Pattern;
31: import java.util.stream.Collectors;
32: import java.io.Serializable;
33: import it.tidalwave.util.spi.HierarchicFinderSupport;
34: import it.tidalwave.thesefoolishthings.examples.person.Person;
35:
36: /***************************************************************************************************************************************************************
37: *
38: * A variant of {@link PersonRegistryImpl2a} that uses an internal status class.
39: *
40: * @author Fabrizio Giudici
41: *
42: **************************************************************************************************************************************************************/
43: public class PersonFinderImpl2b extends HierarchicFinderSupport<Person, PersonFinder> implements PersonFinder
44: {
45: private static final long serialVersionUID = 51438631451345L;
46:
47: // START SNIPPET: public-constructor-and-fields
48: record Status (@Nonnull List<Person> persons, @Nonnull Pattern firstNamePattern, @Nonnull Pattern lastNamePattern)
49: // implements Serializable
50: {
51: }
52:
53: @Nonnull
54: private final Status status;
55:
56: // This is for public use
57: public PersonFinderImpl2b (@Nonnull final List<Person> persons)
58: {
59: this(new Status(persons, Pattern.compile(".*"), Pattern.compile(".*")));
60: }
61: // END SNIPPET: public-constructor-and-fields
62:
63: // This is for internal purposes
64: // This could be generated by Lombok's @RequiredArgsConstructor
65: // START SNIPPET: private-constructor
66: private PersonFinderImpl2b (@Nonnull final Status status)
67: {
68: this.status = status;
69: }
70: // END SNIPPET: private-constructor
71:
72: // START SNIPPET: clone-constructor
73: public PersonFinderImpl2b (@Nonnull final PersonFinderImpl2b other, @Nonnull final Object override)
74: {
75: super(other, override);
76: final var source = getSource(Status.class, other.status, override);
77: this.status = new Status(source.persons, source.firstNamePattern, source.lastNamePattern);
78: }
79: // END SNIPPET: clone-constructor
80:
81: // START SNIPPET: new-methods
82: @Override @Nonnull
83: public PersonFinder withFirstName (@Nonnull final String regex)
84: {
85: return clonedWith(new Status(status.persons, Pattern.compile(regex), status.lastNamePattern));
86: }
87:
88: @Override @Nonnull
89: public PersonFinder withLastName (@Nonnull final String regex)
90: {
91: return clonedWith(new Status(status.persons, status.firstNamePattern, Pattern.compile(regex)));
92: }
93: // END SNIPPET: new-methods
94:
95: // START SNIPPET: computeResults
96: @Override @Nonnull
97: protected List<Person> computeResults()
98: {
99: return status.persons.stream()
100: .filter(p -> status.firstNamePattern.matcher(p.getFirstName()).matches()
101: && status.lastNamePattern.matcher(p.getLastName()).matches())
102: .collect(Collectors.toList());
103: }
104: // END SNIPPET: computeResults
105: }