1

i'm working on a filter function that can filter with many parameters and to do that i'm using Java Streams. So that is my code :

public void filter(String cours,String prof,String salle,String group) {

this.appointments().addAll(getTimeTable().getCreneauxsList().stream()
            .filter(e->e.getProf().equalsIgnoreCase(prof) )
            .filter(e->e.getCours().equalsIgnoreCase(cours) )
            .filter(e->e.getSalle().equalsIgnoreCase(salle) )
            .filter(e->e.getGroup().equalsIgnoreCase(group) )
            .collect(Collectors.toList()));
}

I want to see if one or many the parameters cours,salle,prof,group are null or its trim () = "", it's not worth it to filter with it cause the result is not what i expected to get.

But I do not know how to do it.

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38
Salif Ano
  • 21
  • 2

3 Answers3

2

You can create a method to handle the null or empty string during filter like below:

public static boolean compareNullableString(String str, String filterStr) {

    return (filterStr == null || filterStr.trim().equals("")) ? true : filterStr.equalsIgnoreCase(str);

}

And then modify your code like:

this.appointments().addAll(getTimeTable().getCreneauxsList().stream()
                .filter(e->compareNullableString(e.getProf,prof) )
                .filter(e->compareNullableString(e.getCours,cours))
                .filter(e->compareNullableString(e.getSalle,salle) )
                .filter(e->compareNullableString(e.getGroup,group) )
                .collect(Collectors.toList()));
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • 3
    Maybe it's just me, but `return filterStr == null || filterStr.trim().isEmpty() || filterStr.equalsIgnoreCase(str);` seems more readable – ernest_k Feb 17 '19 at 09:40
  • @ernest_k You are right. That's a good suggestion. It will make the statement brief and more readable. Thanks!!! – Amit Bera Feb 17 '19 at 09:43
0

You can use Strings.isNullOrEmpty() from guava:

boolean isNullOrEmpty = Strings.isNullOrEmpty(e.getProf());

Or there is a set of methods from apache commons:

StringUtils.isBlank, StringUtils.isNotBlank, StringUtils.isEmpty, StringUtils.isNotEmpty

This might be useful: StringUtils.isBlank() vs String.isEmpty()

It your case isNotBlank exactly what you are looking for:

BiPredicate<String, String> myPredicate = (field, eqString) ->
        StringUtils.isNotBlank(field) && field.equalsIgnoreCase(eqString);

    ...
    .filter(e -> myPredicate.test(e.getProf(), prof))
    .filter(e -> myPredicate.test(e.getCours(), cours))
    .filter(e -> myPredicate.test(e.getSalle(), salle))
    .filter(e -> myPredicate.test(e.getGroup(), group))
    ...
Ruslan
  • 6,090
  • 1
  • 21
  • 36
0

This answer can be improved.

Method compareNullableString can be as it is. You can get rid of creating List and adding element in forEach. Filtering can be also extracted to Predicate.

List<String> appointments = this.appointments();

Stream<Predicate<Creneauxs>> filterStream = Stream.of(
        e->compareNullableString(e.getProf,prof),
        e->compareNullableString(e.getCours,cours),
        e->compareNullableString(e.getSalle,salle),
        e->compareNullableString(e.getGroup,group)
);
Predicate<Creneauxs> filter = filterStream.reduce(Predicate::and).orElse(x->false);

getTimeTable().getCreneauxsList().stream()
  .filter(filter)
  .forEach(appointments::add);
lczapski
  • 4,026
  • 3
  • 16
  • 32