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

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

Is there an object able to chain successive method calls

问题

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

  1. int status = method0();
  2. if (status == success) {
  3. status = method1();
  4. }
  5. if (status == success) {
  6. status = method2();
  7. }
  8. if (status == success) {
  9. status = method3();
  10. }
  11. if (status == success) {
  12. status = method4();
  13. }
  14. return status;

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

  1. final CommandPipe pipe = new CommandPipe();
  2. status = pipe.next(() -> method0(arg0))
  3. .next(() -> method1(arg1))
  4. .next(() -> method2(arg2))
  5. .next(() -> method3(arg3))
  6. .next(() -> method4(arg4))
  7. .getResult();
  8. return status;

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

英文:

I have several methods that have the following pattern:

  1. int status = method0();
  2. if(status = success){
  3. status = method1();
  4. }
  5. if(status = success){
  6. status = method2();
  7. }
  8. if(status = success){
  9. status = method3();
  10. }
  11. if(status = success){
  12. status = method4();
  13. }
  14. return status

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

  1. final CommandPipe pipe = new CommandPipe();
  2. status = pipe.next(() -> method0(arg0))
  3. .next(() -> method1(arg1))
  4. .next(() -> method2(arg2))
  5. .next(() -> method3(arg3))
  6. .next(() -> method4(arg4))
  7. .getResult();
  8. 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 循环:

  1. int method(List<IntSupplier> list) {
  2. int status = success;
  3. for (Iterator<IntSupplier> it = list.iterator(); status == success && it.hasNext();) {
  4. status = it.next().get();
  5. }
  6. return status;
  7. }

调用方式如下:

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

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

您的 CommandPipe 类会很容易编写:

  1. class CommandPipe {
  2. int status = success;
  3. CommandPipe next(IntSupplier supplier) {
  4. if (status == success) {
  5. status = supplier.get();
  6. }
  7. return this;
  8. }
  9. int getResult() { return status; }
  10. }

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

英文:

Use a List<IntSupplier> and a for loop:

  1. int method(List<IntSupplier> list) {
  2. int status = success;
  3. for (Iterator<IntSupplier> it = list.iterator(); status == success && it.hasNext();) {
  4. status = it.next().get();
  5. }
  6. return status;
  7. }

Invoke like:

  1. int status = method(List.of(
  2. () -> method0(arg0),
  3. () -> 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:

  1. class CommandPipe {
  2. int status = success;
  3. CommandPipe next(IntSupplier supplier) {
  4. if (status == success) {
  5. status = supplier.get();
  6. }
  7. return this;
  8. }
  9. int getResult() { return status; }
  10. }

Invoke like you wrote in the question.

答案3

得分: 2

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

  1. 我认为最简单的方法是自己实现这样一种类型如下所示
  2. public class CommandPipe<T> {
  3. private Predicate<T> predicate;
  4. private T lastResult = null;
  5. private boolean firstInvotion = true;
  6. public CommandPipe(Predicate<T> predicate) {
  7. this.predicate = predicate;
  8. }
  9. public CommandPipe next(Supplier<T> supplier) {
  10. boolean doStep;
  11. if (firstInvotion) {
  12. firstInvotion = false;
  13. doStep = true;
  14. } else {
  15. doStep = predicate.test(lastResult);
  16. }
  17. if (doStep) {
  18. lastResult = supplier.get();
  19. } else {
  20. // 错误处理
  21. }
  22. return this;
  23. }
  24. public T getResult() {
  25. return lastResult;
  26. }
  27. }

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

英文:

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

  1. public class CommandPipe<T> {
  2. private Predicate<T> predicate;
  3. private T lastResult = null;
  4. private boolean firstInvotion = true;
  5. public CommandPipe(Predicate<T> predicate) {
  6. this.predicate = predicate;
  7. }
  8. public CommandPipe next(Supplier<T> supplier) {
  9. boolean doStep;
  10. if (firstInvotion) {
  11. firstInvotion = false;
  12. doStep = true;
  13. } else {
  14. doStep = predicate.test(lastResult);
  15. }
  16. if (doStep) {
  17. lastResult = supplier.get();
  18. } else {
  19. // error handling
  20. }
  21. return this;
  22. }
  23. public T getResult() {
  24. return lastResult;
  25. }
  26. }

答案4

得分: 0

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

  1. public static int pipe(IntSupplier... methods) {
  2. int success = 0;
  3. int status = success;
  4. for (IntSupplier m : methods) {
  5. status = m.getAsInt();
  6. if (status != success)
  7. break;
  8. }
  9. return status;
  10. }

以及

  1. int status = pipe(
  2. () -> method0(arg0),
  3. () -> method1(arg0),
  4. () -> method2(arg0),
  5. () -> method3(arg0),
  6. () -> method4(arg0));
英文:

You can define a static method with varargs like this.

  1. public static int pipe(IntSupplier... methods) {
  2. int success = 0;
  3. int status = success;
  4. for (IntSupplier m : methods) {
  5. status = m.getAsInt();
  6. if (status != success)
  7. break;
  8. }
  9. return status;
  10. }

and

  1. int status = pipe(
  2. () -> method0(arg0),
  3. () -> method1(arg0),
  4. () -> method2(arg0),
  5. () -> method3(arg0),
  6. () -> 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:

确定