Skip to content

Package: FinderExample2

FinderExample2

nameinstructionbranchcomplexitylinemethod
FinderExample2()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
lambda$main$0(Person)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
main(String[])
M: 0 C: 108
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 47
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

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