Skip to content

Method: lambda$main$0(Person)

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.Comparator;
31: import java.util.regex.Pattern;
32: import java.util.stream.Collectors;
33: import it.tidalwave.thesefoolishthings.examples.person.Person;
34: import it.tidalwave.thesefoolishthings.examples.person.PersonRegistryHelper;
35: import lombok.extern.slf4j.Slf4j;
36: import static it.tidalwave.thesefoolishthings.examples.inmemoryfinderexample.PersonSortCriteria.*;
37: import static it.tidalwave.util.Finder.SortDirection.DESCENDING;
38:
39: /***********************************************************************************************************************
40: *
41: * @author Fabrizio Giudici
42: * @hidden
43: *
44: **********************************************************************************************************************/
45: @Slf4j
46: public class FinderExample2
47: {
48: public static void main (@Nonnull final String ... args)
49: {
50: final PersonRegistry2 registry = new PersonRegistryImpl2a();
51: PersonRegistryHelper.populate(registry);
52:
53: log.info("All: {}", registry.findPerson().results());
54:
55: log.info("All, sorted by first name: {}",
56: registry.findPerson()
57: .sort(BY_FIRST_NAME)
58: .results());
59:
60: log.info("All, sorted by last name, descending: {}",
61: registry.findPerson()
62: .sort(BY_LAST_NAME, DESCENDING)
63: .results());
64:
65: log.info("Two persons from the 3rd position: {}",
66: registry.findPerson()
67: .from(3)
68: .max(2)
69: .results());
70:
71: // START SNIPPET: extended-example
72: log.info("Whose first name starts with B: {}",
73: registry.findPerson()
74: .withFirstName("B.*")
75: .results());
76:
77: log.info("Whose first name starts with B, sorted by first name: {}",
78: registry.findPerson()
79: .sort(BY_FIRST_NAME)
80: .withFirstName("B.*")
81: .results());
82: // END SNIPPET: extended-example
83:
84: log.info("The first found whose last name is Bernini: {}",
85: registry.findPerson()
86: .withLastName("Bernini")
87: .optionalFirstResult());
88:
89: // START SNIPPET: stream-example
90: // Here both filtering and sorting are performed by the Finder, which could make them happen in the data source.
91: log.info("Whose first name starts with B, sorted by first name: {}",
92: registry.findPerson()
93: .withFirstName("B.*")
94: .sort(BY_FIRST_NAME)
95: .results());
96:
97: // Here filtering is performed as above, but sorting is done in memory after all data have been retrieved.
98: log.info("Whose first name starts with B, sorted by first name: {}",
99: registry.findPerson()
100: .withFirstName("B.*")
101: .stream()
102: .sorted(Comparator.comparing(Person::getFirstName))
103: .collect(Collectors.toList()));
104:
105: // Here both filtering and sorting are performed in memory.
106: log.info("Whose first name starts with B, sorted by first name: {}",
107: registry.findPerson()
108: .stream()
109: .filter(p -> Pattern.matches("B.*", p.getFirstName()))
110: .sorted(Comparator.comparing(Person::getFirstName))
111: .collect(Collectors.toList()));
112: // END SNIPPET: stream-example
113: }
114: }