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?
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);