lundi 16 mars 2020

Providing an Empty List to an Optional?

Suppose I have this layout:

public void caller(@NonNull List<Integer> param){
  // param COULD BE EMPTY, but it CANNOT be NULL, its not nullable
  if(!param.isEmpty()) function(Optional.of(param));
  // If param is empty then just dont call "function"
}


public void function(Optional<List<Integer>> someParam){
  someParam.ifPresent(...);
}

Here is the question: Is not creating an Optional based on an empty list correct usage? The confusion I am having is whether we should be providing an Optional regardless if param in caller is empty or not because it is not null, but I am wondering if this is the right usage?

Aucun commentaire:

Enregistrer un commentaire