Skip to contentMethod: computeResults()
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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
12: * the License. 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
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.thesefoolishthings.examples.extendedfinderexample;
28:
29: import javax.annotation.Nonnull;
30: import java.util.List;
31: import java.util.regex.Pattern;
32: import java.util.stream.Collectors;
33: import it.tidalwave.util.spi.HierarchicFinderSupport;
34: import it.tidalwave.thesefoolishthings.examples.person.Person;
35:
36: /***********************************************************************************************************************
37: *
38: * @author Fabrizio Giudici
39: *
40: **********************************************************************************************************************/
41: public class PersonFinderImpl2a extends HierarchicFinderSupport<Person, PersonFinder> implements PersonFinder
42: {
43: // START SNIPPET: public-constructor-and-fields
44: @Nonnull
45: private final List<Person> persons;
46:
47: @Nonnull
48: private final Pattern firstNamePattern;
49:
50: @Nonnull
51: private final Pattern lastNamePattern;
52:
53: // This is for public use
54: public PersonFinderImpl2a (@Nonnull final List<Person> persons)
55: {
56: this(persons, Pattern.compile(".*"), Pattern.compile(".*"));
57: }
58: // END SNIPPET: public-constructor-and-fields
59:
60: // This is for internal purposes
61: // START SNIPPET: private-constructor
62: // This could be generated by Lombok @RequiredArgsConstructor
63: private PersonFinderImpl2a (@Nonnull final List<Person> persons,
64: @Nonnull final Pattern firstNamePattern,
65: @Nonnull final Pattern lastNamePattern)
66: {
67: this.persons = persons;
68: this.firstNamePattern = firstNamePattern;
69: this.lastNamePattern = lastNamePattern;
70: }
71: // END SNIPPET: private-constructor
72:
73: // START SNIPPET: clone-constructor
74: public PersonFinderImpl2a (@Nonnull final PersonFinderImpl2a other, @Nonnull final Object override)
75: {
76: super(other, override);
77: final PersonFinderImpl2a source = getSource(PersonFinderImpl2a.class, other, override);
78: this.persons = source.persons;
79: this.firstNamePattern = source.firstNamePattern;
80: this.lastNamePattern = source.lastNamePattern;
81: }
82: // END SNIPPET: clone-constructor
83:
84: // START SNIPPET: new-methods
85: @Override @Nonnull
86: public PersonFinder withFirstName (@Nonnull final String regex)
87: {
88: return clonedWith(new PersonFinderImpl2a(persons, Pattern.compile(regex), lastNamePattern));
89: }
90:
91: @Override @Nonnull
92: public PersonFinder withLastName (@Nonnull final String regex)
93: {
94: return clonedWith(new PersonFinderImpl2a(persons, firstNamePattern, Pattern.compile(regex)));
95: }
96: // END SNIPPET: new-methods
97:
98: // START SNIPPET: computeResults
99: @Override @Nonnull
100: protected List<? extends Person> computeResults()
101: {
102: return persons.stream()
103: .filter(p -> firstNamePattern.matcher(p.getFirstName()).matches()
104: && lastNamePattern.matcher(p.getLastName()).matches())
105: .collect(Collectors.toList());
106: }
107: // END SNIPPET: computeResults
108: }