Skip to content

Method: PersonFinderImpl2a(List, Pattern, Pattern)

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 it.tidalwave.util.spi.HierarchicFinderSupport;
33: import it.tidalwave.thesefoolishthings.examples.person.Person;
34:
35: /***************************************************************************************************************************************************************
36: *
37: * @author Fabrizio Giudici
38: *
39: **************************************************************************************************************************************************************/
40: public class PersonFinderImpl2a extends HierarchicFinderSupport<Person, PersonFinder> implements PersonFinder
41: {
42: private static final long serialVersionUID = 51438631451345L;
43:
44: // START SNIPPET: public-constructor-and-fields
45: @Nonnull
46: private final List<Person> persons;
47:
48: @Nonnull
49: private final Pattern firstNamePattern;
50:
51: @Nonnull
52: private final Pattern lastNamePattern;
53:
54: // This is for public use
55: public PersonFinderImpl2a (@Nonnull final List<Person> persons)
56: {
57: this(persons, Pattern.compile(".*"), Pattern.compile(".*"));
58: }
59: // END SNIPPET: public-constructor-and-fields
60:
61: // This is for internal purposes
62: // START SNIPPET: private-constructor
63: // This could be generated by Lombok @RequiredArgsConstructor
64: private PersonFinderImpl2a (@Nonnull final List<Person> persons,
65: @Nonnull final Pattern firstNamePattern,
66: @Nonnull final Pattern lastNamePattern)
67: {
68: this.persons = persons;
69: this.firstNamePattern = firstNamePattern;
70: this.lastNamePattern = lastNamePattern;
71: }
72: // END SNIPPET: private-constructor
73:
74: // START SNIPPET: clone-constructor
75: public PersonFinderImpl2a (@Nonnull final PersonFinderImpl2a other, @Nonnull final Object override)
76: {
77: super(other, override);
78: final var source = getSource(PersonFinderImpl2a.class, other, override);
79: this.persons = source.persons;
80: this.firstNamePattern = source.firstNamePattern;
81: this.lastNamePattern = source.lastNamePattern;
82: }
83: // END SNIPPET: clone-constructor
84:
85: // START SNIPPET: new-methods
86: @Override @Nonnull
87: public PersonFinder withFirstName (@Nonnull final String regex)
88: {
89: return clonedWith(new PersonFinderImpl2a(persons, Pattern.compile(regex), lastNamePattern));
90: }
91:
92: @Override @Nonnull
93: public PersonFinder withLastName (@Nonnull final String regex)
94: {
95: return clonedWith(new PersonFinderImpl2a(persons, firstNamePattern, Pattern.compile(regex)));
96: }
97: // END SNIPPET: new-methods
98:
99: // START SNIPPET: computeResults
100: @Override @Nonnull
101: protected List<Person> computeResults()
102: {
103: return persons.stream()
104: .filter(p -> firstNamePattern.matcher(p.getFirstName()).matches()
105: && lastNamePattern.matcher(p.getLastName()).matches())
106: .collect(Collectors.toList());
107: }
108: // END SNIPPET: computeResults
109: }