以不同的行为解决价值,最好没有其他行为

发布于 2025-02-08 18:17:39 字数 426 浏览 0 评论 0原文

这是我想做的

首先启动可选变量,然后执行过程。 如果程序a收益率为null,请尝试过程B,依此类推。 如果所有过程仍然产生无效,请抛出例外情况,

#pseudocode

Optional<> someVariable;

if (someVariable is empty):
  fetch value through procedure A
  assign to someVariable
if (someVariable is empty):
  fetch value through procedure B
  assign to someVariable
.
.
.
if still empty:
  throw exception

问题是我真的不想通过已解决该值的过程A,并且如果添加更多过程,我认为它是可扩展的 有任何输入吗?

Here is what I'm trying to do

First initiate an optional variable, then do procedure A.
if procedure A yield null then try procedure B and so on.
if all the procedure still yield null, throw exception

#pseudocode

Optional<> someVariable;

if (someVariable is empty):
  fetch value through procedure A
  assign to someVariable
if (someVariable is empty):
  fetch value through procedure B
  assign to someVariable
.
.
.
if still empty:
  throw exception

The problem is I don't really want to go through procedure B if procedure A already resolve the value and I don't think it is scalable if more procedure is added
any input?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

没有伤那来痛 2025-02-15 18:17:39

这没有其他

public static void main(String[] args) {

    Integer result = null;
    if ((result = procA()) == null && (result = procB()) == null){
        System.out.println("throw ex");
    }
}

static Integer procA ()  {
    
    System.out.println("procA");
    return null;
}

static Integer procB () {
    System.out.println("procB");
    return null;
}

尝试更改方法返回的值的值。

This has no else

public static void main(String[] args) {

    Integer result = null;
    if ((result = procA()) == null && (result = procB()) == null){
        System.out.println("throw ex");
    }
}

static Integer procA ()  {
    
    System.out.println("procA");
    return null;
}

static Integer procB () {
    System.out.println("procB");
    return null;
}

Try changing the values of what the methods return.

你的他你的她 2025-02-15 18:17:39

使用JDK 11(可能是JDK 8+):

public class MyClass {

  private Integer procedureA() {
    return ...
  }

  private Integer procedureB() {
    return ...
  }

  ...
  
  private final List<Supplier<Integer>> procedures = List.of(
    this::procedureA,
    this::procedureB,
    // ... as many as you want
  );

  // This code will stay the same, no matter how many procedures you add
  public Optional<Integer> readValue() {
    Integer someValue =  procedures
            .stream()
            .map( Supplier::get )
            .filter( Objects::nonNull )
            .findFirst()
            .orElseThrow( () -> new RuntimeException( "Exception!" )   );
   // I don't think it makes sense to return an optional in this case
   // but you can do it if you want to
   return Optional.of(someValue);
  }
}

这几乎与其他人建议的方法相同,但是使用最新的Java,您可以编写比我在其他答案中看到的代码更紧凑和可以理解的。

With JDK 11 (probably JDK 8+):

public class MyClass {

  private Integer procedureA() {
    return ...
  }

  private Integer procedureB() {
    return ...
  }

  ...
  
  private final List<Supplier<Integer>> procedures = List.of(
    this::procedureA,
    this::procedureB,
    // ... as many as you want
  );

  // This code will stay the same, no matter how many procedures you add
  public Optional<Integer> readValue() {
    Integer someValue =  procedures
            .stream()
            .map( Supplier::get )
            .filter( Objects::nonNull )
            .findFirst()
            .orElseThrow( () -> new RuntimeException( "Exception!" )   );
   // I don't think it makes sense to return an optional in this case
   // but you can do it if you want to
   return Optional.of(someValue);
  }
}

This is pretty much the same approach everybody else has suggested, but with the latest Java you can write code that's much more compact and understandable than the one I've seen in the other answers.

兲鉂ぱ嘚淚 2025-02-15 18:17:39

一个详细的,但希望可以阅读的方法:

Object someVariable = Stream.of(initialValue)
    .map(obj -> obj == null ? procedureA() : obj)
    .map(obj -> obj == null ? procedureB() : obj)
    .findFirst()
    .orElseThrow(NoSuchElementException::new); // or whatever exception you want

A slightly verbose but hopefully readable approach:

Object someVariable = Stream.of(initialValue)
    .map(obj -> obj == null ? procedureA() : obj)
    .map(obj -> obj == null ? procedureB() : obj)
    .findFirst()
    .orElseThrow(NoSuchElementException::new); // or whatever exception you want
冷…雨湿花 2025-02-15 18:17:39

如果您的过程相对简单,并且可以称为运行 s。然后,我们可以创建一个可运行的列表,并使用for loop执行运行方法:

private class ProcedureA implements Runnable {
    @Override
    public void run() {
        someVariable = try fetching value
    }
}

// private class ProcedureB/C/D/E implements Runnable {}

List<Runnable> procedures = List.of(new ProcedureA(), new ProcedureB(), ...);
for (Runnable procedure : procedures) {
    if (someVariable.isPresent()) {
        break;
    }
    procedure.run();
}

if (someVariable.isEmpty()) {
    throw exception;
}

如果过程有些复杂,并且会返回值或进行参数,那么我们可以使用callable 或使用运行方法定义自定义过程接口。

public interface Procedure {
   V running(arg1, arg2);
}

public class ProcedureA implements Procedure {
    @Override
    public V running(arg1, arg2) {}
}

List<Procedure> procedures = List.of(new ProcedureA() ...);

If your procedures are relatively simple and can be declared as Runnables. Then we can create a list of Runnables and execute the run method with a for loop:

private class ProcedureA implements Runnable {
    @Override
    public void run() {
        someVariable = try fetching value
    }
}

// private class ProcedureB/C/D/E implements Runnable {}

List<Runnable> procedures = List.of(new ProcedureA(), new ProcedureB(), ...);
for (Runnable procedure : procedures) {
    if (someVariable.isPresent()) {
        break;
    }
    procedure.run();
}

if (someVariable.isEmpty()) {
    throw exception;
}

If the procedures are somewhat complicated, and will return values or take arguments, then we can use Callable or define a custom Procedure interface with a running method.

public interface Procedure {
   V running(arg1, arg2);
}

public class ProcedureA implements Procedure {
    @Override
    public V running(arg1, arg2) {}
}

List<Procedure> procedures = List.of(new ProcedureA() ...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文