Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<Customer> filterStepbyStep(List<Customer> customers,
- String searchString,
- List<Tag> selectedTags,
- List<LocationId> selectedLocations) {
- List<Customer> filteredCustomers = new ArrayList<Customer>(customers);
- boolean searchStringNotEmpty = searchString != null && !searchString.trim().isEmpty();
- boolean locationsNotEmpty = selectedLocations != null && selectedLocations.size() > 0;
- boolean selectedTagsNotEmpty = selectedTags != null && selectedTags.size() > 0;
- filteredCustomers.removeIf(customer -> {
- boolean valid = true;
- //searchString
- if (valid && searchStringNotEmpty) {
- valid = customer.name.contains(searchString);
- }
- //locations
- if (valid && locationsNotEmpty) {
- valid = selectedLocations.stream().anyMatch(locationId -> locationId.equals(customer.locationId));
- }
- //tags
- if (valid && selectedTagsNotEmpty) {
- boolean tagValid = false;
- for (Tag selectedTag : selectedTags) {
- for (Tag customerTag : customer.getTags()) {
- if (selectedTag == customerTag) {
- tagValid = true;
- break;
- }
- }
- if (tagValid) {
- break;
- }
- }
- valid = tagValid;
- }
- return !valid;
- });
- return filteredCustomers;
- }
- public List<Customer> filterWithLambda(List<Customer> customers,
- String searchString,
- List<Tag> selectedTags,
- List<LocationId> selectedLocations) {
- List<Customer> filteredCustomers = new ArrayList<Customer>(customers);
- boolean searchStringNotEmpty = searchString != null && !searchString.trim().isEmpty();
- boolean locationsNotEmpty = selectedLocations != null && selectedLocations.size() > 0;
- boolean selectedTagsNotEmpty = selectedTags != null && selectedTags.size() > 0;
- filteredCustomers.removeIf(customer -> {
- return (searchStringNotEmpty && !customer.name.contains(searchString))
- || (locationsNotEmpty && !selectedLocations.stream().anyMatch(locationId -> locationId.equals(customer.locationId)))
- || (selectedTagsNotEmpty && !selectedTags.stream().anyMatch(selectedTag ->
- customer.getTags().stream().anyMatch(customerTag ->
- selectedTag == customerTag)));
- });
- return filteredCustomers;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement