Optional.ofNullable with ifPresent

Asked

Viewed 284 times

-2

If the property ano is null but the model and year are filled, will save the car data in the repository ?

public Class Carro {
 int ano;
 String modelo;
 int valor;
}

public void carro(final Carro carro){
 Carrorepository carroRepository;
Optiona.ofNullable(carro).ifPresent({
carroRepository.save(carro)
 });
}
  • The estate ano cannot be null because it is a int, not a Integer. Of all the luck, the ifPresent will judge who is in the Optional and in its subsequent mappings, then it will call yes the save function.

1 answer

4


There are some problems in the question. The first is language itself: "if ano is null but [...] year is filled".

Algo de errado não está certo

Okay, let’s assume it was a mistake and if you meant the third camp of Carro, the field valor, as being filled in.

The other problem is typing. Any combination of bits makes a binary number valid. So if there was a value that represented the null for the int one should choose some valid integer that would represent that null, then decrease the range of the integers. And several other complications.

Nulls only exist for objects in the Java world. Is there any null for the whole? Yes, there is, but not with the int. It’s with the class casing Integer. The variables in that class or are envelopes of int or are pointing to null. That said, I’m going to assume that this was a typo that, when testing the code, the development team would be scratching their hair when a 0 appeared where one expected a null...

The last problem is lambda. There are two notations for lambda in Java: arrow notations -> for function declared in lambda, and the double-two-point notation :: for instance method reference, static method and constructors. In this case, it seems that you wished to have used arrow notation:

c -> { carroRepository.save(c); }

However, as one would expect Consumer<Carro> and no other operation is done, we could use the lambda notation of a single command, which does not need the code block indicators:

c -> carroRepository(c)

But once again, only one method is used. Direct this. One could call the reference to this method:

carroRepository::save

All right, let’s get down to business.

The Optional looks simply at the object it is involving. It does not look at the content of that object (therefore, a priori, ignores such fields completely).

The Optional.ofNullable can assume two values:

  • Optional.empty, a static variable representing an object that is absent
  • an envelope containing the object in question

If the first case is ifPresent never will be executed. End of chat. Already, if the second case is, the method that consumes objects of the class involved will be called at all times.

There are cases where you catch a Optional completed and obtained a Optional empty. The main ones I remember are:

  • map, which transforms the object involved into another (often a field of that object, but not necessarily)
  • filter, which explicitly transforms into a Optional.empty if the predicate is not met

So that being said, what is no name filter nor map about the Optional created on the variable carro, case carro != null, then the method carroRepository::save will be called and the car will be saved.


Recommended reading:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.