Skip to content

Method: 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: * 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: // This could be generated by Lombok's @RequiredArgsConstructor
46: // START SNIPPET: public-constructor-and-fields
47: static class Status
48: {
49: Status (@Nonnull final List<Person> persons,
50: @Nonnull final Pattern firstNamePattern,
51: @Nonnull final Pattern lastNamePattern)
52: {
53: this.persons = persons;
54: this.firstNamePattern = firstNamePattern;
55: this.lastNamePattern = lastNamePattern;
56: }
57:
58: @Nonnull
59: final List<Person> persons;
60:
61: @Nonnull
62: final Pattern firstNamePattern;
63:
64: @Nonnull
65: final Pattern lastNamePattern;
66: }
67:
68: @Nonnull
69: private final Status status;
70:
71: // This is for public use
72: public PersonFinderImpl2b (@Nonnull final List<Person> persons)
73: {
74: this(new Status(persons, Pattern.compile(".*"), Pattern.compile(".*")));
75: }
76: // END SNIPPET: public-constructor-and-fields
77:
78: // This is for internal purposes
79: // This could be generated by Lombok's @RequiredArgsConstructor
80: // START SNIPPET: private-constructor
81: private PersonFinderImpl2b (@Nonnull final Status status)
82: {
83: this.status = status;
84: }
85: // END SNIPPET: private-constructor
86:
87: // START SNIPPET: clone-constructor
88: public PersonFinderImpl2b (@Nonnull final PersonFinderImpl2b other, @Nonnull final Object override)
89: {
90: super(other, override);
91: final Status source = getSource(Status.class, other.status, override);
92: this.status = new Status(source.persons, source.firstNamePattern, source.lastNamePattern);
93: }
94: // END SNIPPET: clone-constructor
95:
96: // START SNIPPET: new-methods
97: @Override @Nonnull
98: public PersonFinder withFirstName (@Nonnull final String regex)
99: {
100: return clonedWith(new Status(status.persons, Pattern.compile(regex), status.lastNamePattern));
101: }
102:
103: @Override @Nonnull
104: public PersonFinder withLastName (@Nonnull final String regex)
105: {
106: return clonedWith(new Status(status.persons, status.firstNamePattern, Pattern.compile(regex)));
107: }
108: // END SNIPPET: new-methods
109:
110: // START SNIPPET: computeResults
111: @Override @Nonnull
112: protected List<? extends Person> computeResults()
113: {
114: return status.persons.stream()
115: .filter(p -> status.firstNamePattern.matcher(p.getFirstName()).matches()
116: && status.lastNamePattern.matcher(p.getLastName()).matches())
117: .collect(Collectors.toList());
118: }
119: // END SNIPPET: computeResults
120: }