Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. public List<Customer> filterStepbyStep(List<Customer> customers,
  2. String searchString,
  3. List<Tag> selectedTags,
  4. List<LocationId> selectedLocations) {
  5. List<Customer> filteredCustomers = new ArrayList<Customer>(customers);
  6. boolean searchStringNotEmpty = searchString != null && !searchString.trim().isEmpty();
  7. boolean locationsNotEmpty = selectedLocations != null && selectedLocations.size() > 0;
  8. boolean selectedTagsNotEmpty = selectedTags != null && selectedTags.size() > 0;
  9.  
  10. filteredCustomers.removeIf(customer -> {
  11. boolean valid = true;
  12.  
  13. //searchString
  14. if (valid && searchStringNotEmpty) {
  15. valid = customer.name.contains(searchString);
  16. }
  17.  
  18. //locations
  19. if (valid && locationsNotEmpty) {
  20. valid = selectedLocations.stream().anyMatch(locationId -> locationId.equals(customer.locationId));
  21. }
  22.  
  23. //tags
  24. if (valid && selectedTagsNotEmpty) {
  25. boolean tagValid = false;
  26. for (Tag selectedTag : selectedTags) {
  27. for (Tag customerTag : customer.getTags()) {
  28. if (selectedTag == customerTag) {
  29. tagValid = true;
  30. break;
  31. }
  32. }
  33. if (tagValid) {
  34. break;
  35. }
  36. }
  37. valid = tagValid;
  38. }
  39.  
  40. return !valid;
  41. });
  42.  
  43. return filteredCustomers;
  44. }
  45.  
  46. public List<Customer> filterWithLambda(List<Customer> customers,
  47. String searchString,
  48. List<Tag> selectedTags,
  49. List<LocationId> selectedLocations) {
  50. List<Customer> filteredCustomers = new ArrayList<Customer>(customers);
  51. boolean searchStringNotEmpty = searchString != null && !searchString.trim().isEmpty();
  52. boolean locationsNotEmpty = selectedLocations != null && selectedLocations.size() > 0;
  53. boolean selectedTagsNotEmpty = selectedTags != null && selectedTags.size() > 0;
  54.  
  55. filteredCustomers.removeIf(customer -> {
  56. return (searchStringNotEmpty && !customer.name.contains(searchString))
  57. || (locationsNotEmpty && !selectedLocations.stream().anyMatch(locationId -> locationId.equals(customer.locationId)))
  58. || (selectedTagsNotEmpty && !selectedTags.stream().anyMatch(selectedTag ->
  59. customer.getTags().stream().anyMatch(customerTag ->
  60. selectedTag == customerTag)));
  61. });
  62.  
  63. return filteredCustomers;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement