有一个对象能够链接连续的方法调用吗?

huangapple go评论65阅读模式
英文:

Is there an object able to chain successive method calls

问题

我有几种方法,它们具有以下模式:

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;

我想要使这个代码更可读。我想象中的代码如下:

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;

在Java 8中有类似这样的对象吗?

英文:

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 ?

答案1

得分: 2

As of now no.
由于这是您的业务案例框架,不会提供此类功能。
您必须自行构建,否则可以连续使用 CompletableFuture.supplyAsync 和 thenApply 方法,以更好地利用资源的线程。

英文:

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.

答案2

得分: 2

使用一个 List<IntSupplier> 和一个 for 循环:

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

调用方式如下:

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

(是的,List.of 是 Java 9 的方法。您可以以任何方式构建您的列表)。

您的 CommandPipe 类会很容易编写:

class CommandPipe {
  int status = success;

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

  int getResult() { return status; }
}

调用方式与您在问题中提到的方式相同。

英文:

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.

答案3

得分: 2

以下是您提供的代码的中文翻译部分:

我认为最简单的方法是自己实现这样一种类型如下所示

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 {
            // 错误处理
        }

        return this;
    }

    public T getResult() {
        return lastResult;
    }
}

请注意,我只提供了代码的翻译部分,没有其他内容。

英文:

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;
    }
}

答案4

得分: 0

你可以这样定义一个带有可变参数的静态方法。

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;
}

以及

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

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

huangapple
  • 本文由 发表于 2020年8月7日 15:00:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63296752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定