Absent
Absent

Reputation: 914

How to distinguish between overloaded methods without if statements

In my program I receive a message from a call, that has a variable of the type Optional and depending on whether something is inside of this variable or not I shall make a call of a method with a parameter or a call to the same method without a parameter, standard overloading.

The problem I am having is that the produced code becomes ugly, especially the more optionals I receive the more distinguishing the method call becomes. Currently the next method call is determined via an if-else.

Here is a simplified code for this question, first the Message Class:

public class FooMessage {

  public Optional<String> receivedMessage;

  public FooMessage(String message) {
      this.receivedMessage = Optional.ofNullable(message);
  }
}

and then the Main class:

public class Main {

  public static FooMessage receiveMessageWithNull(){
      return new FooMessage(null);
  }

  public static FooMessage receiveMessage(String s){
      return new FooMessage(s);
  }

  public static void fooMethod() {
      System.out.println("Message == null");
  }

  public static void fooMethod(String message) {
      System.out.println("Message != null");
  }

  public static void main(String[] args) {
      //Calls that return a Message either with content or without
      FooMessage message = receiveMessage("foo");
      FooMessage messageWithNull = receiveMessageWithNull();
      //Resolving which version of the overloaded method to call
      if (message.receivedMessage.isPresent()) {
          fooMethod(message.receivedMessage.get());
      } else {
          fooMethod();
      }

      if (messageWithNull.receivedMessage.isPresent()) {
          fooMethod(messageWithNull.receivedMessage.get());
      } else {
          fooMethod();
      }
  }
}

My question is if there is a possibility to clean the code up in a way that the method call itself is written to resolve the checks currently done in the if statements. I was thinking about something like:

fooMethod(message.receivedMessage.isPresent() ? message.receivedMessage.get() : ... );

Instead of ... there would be something that told the method to ignore the parameter.

Note: I cannot change fooMethod. I have to resolve which version of fooMethod has to be called in the calling method.

Upvotes: 1

Views: 152

Answers (1)

payloc91
payloc91

Reputation: 3809

If you need to execute the method only if the Optional value is present, and you do not care for an absent value, you may go with

message.receivedMessage.ifPresent(Main::fooMethod);

I would avoid passing the Optional to a method that then distinguishes whether the value is present, but you can implement a support function that would hide the implementation details

private static void distinguish(String s) {
    if (s == null) fooMethod();
    else fooMethod(s);
}

and cal it via

distinguish(message.receivedMessage.orElse(null));

This is an acceptable way of using Òptional::orElse.

From the docs:

 /**
 * Return the value if present, otherwise return {@code other}.
 *
 * @param other the value to be returned if there is no value present, may
 * be null
 * @return the value, if present, otherwise {@code other}
 */
public T orElse(T other) {
    return value != null ? value : other;
}

I would personally just stick with if (optional.isPresent()) as this is what optionals are intended for, so I wouldn't worry too much.

Upvotes: 1

Related Questions