Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add logging to removeIf in java 8?

I am using Java 8.

The following code is working fine:

public void testMethod(List<String> prop1, EmailJson randomModel) {
    prop1.stream().forEach(s -> randomModel.getSomeList()
            .removeIf(model -> model.getSomeProp().equalsIgnoreCase(s)));
}

Is it possible to log a message if the condition is true?

I'm looking for something similar to:

public void testMethod(List<String> prop1, EmailJson randomModel) {
    prop1.stream().forEach(s -> randomModel.getSomeList()
            .removeIf(model -> model.getSomeProp().equalsIgnoreCase(s))
                    - > if this is true then log some action);
}
like image 606
sumit Avatar asked May 08 '17 09:05

sumit


2 Answers

You can always replace

removeIf(model -> model.getSomeProp().equalsIgnoreCase(s))

with

removeIf(model -> {
                    boolean ret = model.getSomeProp().equalsIgnoreCase(s);
                    if (ret) {
                        // add logging
                    }
                    return ret;
                  })
like image 164
Eran Avatar answered Oct 18 '22 09:10

Eran


If this is a recurring problem, you may create a helper method generalizing the task of decorating a predicate with another action, e.g. logging:

static <T> Predicate<T> logging(Predicate<T> p, BiConsumer<T,Boolean> log, boolean all) {
    return t -> {
        final boolean result = p.test(t);
        if(all || result) log.accept(t, result);
        return result;
    };
}

which you may use like

public void testMethod(List<String> prop1, EmailJson randomModel){
    prop1.forEach(s -> randomModel.getSomeList()
        .removeIf(logging(model -> model.getSomeProp().equalsIgnoreCase(s),
            (model,b) -> LOGGER.info(() -> "matched: "+model.getSomeProp()), false)));
}

though, in this specific case, it might be unnecessary to decorate the predicate itself, as removeIf return a boolean telling whether there were matches to remove, and the match value is still in scope:

public void testMethod(List<String> prop1, EmailJson randomModel){
    prop1.stream().forEach(s -> {
        if(randomModel.getSomeList()
                      .removeIf(model -> model.getSomeProp().equalsIgnoreCase(s)))
            LOGGER.info(() -> "there were matches of: "+s);
    });
}
like image 43
Holger Avatar answered Oct 18 '22 08:10

Holger