1

My code:

public static boolean searchLineOnLogFile(String...keywords)
{
    Collection select = null;

    try (final Stream<String> lines = Files.lines(get(getServerLog().toString())))
    {
        select = CollectionUtils.select(lines.collect(Collectors.toCollection(LinkedList::new)),
                new Predicate()
                {
                    public boolean evaluate(Object object)
                    {
                        String line = (String) object;
                        return Arrays.stream(keywords).allMatch(line::contains);
                    }
                });

    } catch (IOException e)
    {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }

    select.stream().findFirst().ifPresent(firstLine -> LogAutomation.info((String)firstLine));

    return select.size() > 0;
}    

select.stream().findFirst().ifPresent(firstLine -> log.info((String)firstLine));

Why I get 'uncheck call to isPresent' inspection ? how can I improve my code?

full inspection message

from what I 'v read all the idea is to avoid null check:

"So instead of writing something like:

if(optional.isPresent){
    doSomething(optional.get);
}

You can write:

optional.ifPresent(val->doSomething(val));

or if you prefer:

optional.ifPresent(this::doSomething);
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
Catchy
  • 29
  • 3
  • 2
    Could you share the exact warning you are getting? I guess the problem is in `(String)firstLine` and the warning has nothing to do with the Optional method – Andrew Tobilko Aug 20 '19 at 10:49
  • Show us the declaration of `select`. – Klitos Kyriacou Aug 20 '19 at 10:58
  • To validate Andrew's comment, you can change the call to log.info and pass a static string, such as log.info("first line is not null"). – Jaywalker Aug 20 '19 at 11:13
  • 2
    Likely because your `select` variable has been declared using a [*raw type*](https://stackoverflow.com/q/2770321/2711488). – Holger Aug 20 '19 at 11:56
  • 2
    As I suspected, `select` has a *raw type*. Change the declaration to `Collection select`. It’s still correct what others pointed out, your exception handling … needs improvement, as after catching an exception, you’d proceed with `select` being `null`. But this is not the cause for the compiler warning. – Holger Aug 21 '19 at 12:37
  • 1
    Besides the compiler warning caused by the *raw type* use, it is very twisted, to collect a stream to a collection, insisting on `LinkedList` for no reason, then use the 3rd party library method `CollectionUtils.select`, to finally acquire a stream again. You can directly filter the original stream: `Optional o = lines.filter(line -> Arrays.stream(keywords) .allMatch(line::contains)) .findFirst(); o.ifPresent(firstLine -> LogAutomation.info((String)firstLine)); return o.isPresent();`. No need to collect into a `LinkedList`, no need for a 3rd party library. – Holger Aug 21 '19 at 12:50

2 Answers2

1

You can use Optional.ofNullable, as answered by Butiri. You can also use Objects.requireNonNullElse.

With this second case you can define a default value if is empty. Example:

public class Main {

    public static void main(String[] args) {
        Collection<Integer> select = null;

        if (Math.random() > 0.5) {
            select = Arrays.asList(1, 2, 3);
        }

        Objects.requireNonNullElse(select, Collections.singletonList(99)).stream().
                findFirst().
                ifPresent(e -> System.out.println("found: " + e));
    }

}
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
Topera
  • 12,223
  • 15
  • 67
  • 104
0

The warning is because the select can be null. Can be fixed by Optional.ofNullable

Optional.ofNullable(select).stream().findFirst().ifPresent(firstLine -> LogAutomation.info((String) firstLine));
Butiri Dan
  • 1,759
  • 5
  • 12
  • 18
  • 2
    You are correct in that `select` can be `null` and that this can cause problems. But this is *not* the reason for an *unchecked* warning. Besides that, calling `.stream()` on an optional requires Java 9 and is not the same as calling `stream()` on the contained collection. – Holger Aug 21 '19 at 12:45