EnzoMolion
EnzoMolion

Reputation: 1047

Is there an object able to chain successive method calls

I have several methods that have the following pattern:

int status = method0();
if(status = success){
  status = method1();
}
if(status = success){
  status = method2();
}
if(status = success){
  status = method3();
}
if(status = success){
  status = method4();
}
return status

And I would like to make this more readable. I imagine something like this:

final CommandPipe pipe = new CommandPipe();
status = pipe.next(() -> method0(arg0))
              .next(() -> method1(arg1))
              .next(() -> method2(arg2))
              .next(() -> method3(arg3))
              .next(() -> method4(arg4))
              .getResult();
return status; 

Is there an object doing this in Java 8 ?

Upvotes: 1

Views: 122

Answers (4)

Andy Turner
Andy Turner

Reputation: 140494

Use a List<IntSupplier> and a for loop:

int method(List<IntSupplier> list) {
  int status = success;
  for (Iterator<IntSupplier> it = list.iterator(); status == success && it.hasNext();) {
    status = it.next().get();
  }
  return status;
}

Invoke like:

int status = method(List.of(
    () -> method0(arg0),
    () -> method1(arg1) /* etc */));

(Sure, List.of is a Java 9 method. Construct your list however you like).

Your CommandPipe class would be quite easy to write:

class CommandPipe {
  int status = success;

  CommandPipe next(IntSupplier supplier) {
    if (status == success) {
      status = supplier.get();
    }
    return this;
  }

  int getResult() { return status; }
}

Invoke like you wrote in the question.

Upvotes: 2

user4910279
user4910279

Reputation:

You can define a static method with varargs like this.

public static int pipe(IntSupplier... methods) {
    int success = 0;
    int status = success;
    for (IntSupplier m : methods) {
        status = m.getAsInt();
        if (status != success)
            break;
    }
    return status;
}

and

int status = pipe(
    () -> method0(arg0),
    () -> method1(arg0),
    () -> method2(arg0),
    () -> method3(arg0),
    () -> method4(arg0));

Upvotes: 0

Ackdari
Ackdari

Reputation: 3498

I think the easiest is to implement such a type yourself, like:

public class CommandPipe<T> {
    private Predicate<T> predicate;
    private T lastResult = null;
    private boolean firstInvotion = true;
    public CommandPipe(Predicate<T> predicate) {
      this.predicate = predicate;
    }

    public CommandPipe next(Supplier<T> supplier) {
        boolean doStep;

        if (firstInvotion) {
            firstInvotion = false;
            doStep = true;
        } else {
            doStep = predicate.test(lastResult);
        }

        if (doStep) {
            lastResult = supplier.get();
        } else {
            // error handling
        }

        return this;
    }

    public T getResult() {
        return lastResult;
    }
}

Upvotes: 2

Hakuna Matata
Hakuna Matata

Reputation: 761

As of now no. Since its your business case frameworks wont provide such a things. You have to construct yourself or else you can use CompletableFuture.supplyAsync and thenApply methods successively for better thread utilization of resources.

Upvotes: 2

Related Questions