检查方法链中的最后一个getter是否不为空 [英] Check if last getter in method chain is not null

查看:140
本文介绍了检查方法链中的最后一个getter是否不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中,我们有很多链式方法,例如 obj.getA()。getB()。getC()。getD()。我想创建帮助类,它将检查方法 getD()是否不为null,但在此之前,我需要检查所有以前的getter。我可以这样做:

In code we have got a lot of chain methods, for example obj.getA().getB().getC().getD(). I want to create helper class which will check if method getD() isn't null, but before that I need to check all previous getters. I can do it in this way:

try {
    obj.getA().getB().getC().getD();
}
catch (NullPointerException e) {
    // some getter is null
}

或(这是傻)

if (obj!null && obj.getA()!=null && obj.getA().getB()!=null && ...) {
    obj.getA().getB().getC().getD();
}
else {
    // some getter is null
}

我不想每次使用我的代码中的 try {} catch()检查它。为此目的最好的解决方案是什么?

I don't want to check it every time using try{} catch() in my code. What is the best solution for this purpose?

我认为最好的是:


  1. obj.getA()。getB()。getC()。getD()。isNull() - 为此,我将需要更改我的所有getter,例如实现一些包含 isNull()方法的接口。

  2. NullObjectHelper.isNull(obj。 getA()。getB()。getC()。getD()); - 这将是最好的(我想是这样)但是如何实现这个?

  1. obj.getA().getB().getC().getD().isNull() - for this purpose I will need to change all of my getters, for example implement some interface which contains isNull() method.
  2. NullObjectHelper.isNull(obj.getA().getB().getC().getD()); - this will be the best (I think so) but how to implement this?


推荐答案

您可以通过 Option pattern获得所需的结果。这将强制您更改方法签名,但基本上如果您的方法返回一些类型 T ,则它保证它具有一些非空值,如果返回选项< T> 这意味着它有值T或null。

You can achieve the desired result with Option pattern. This enforces you to change a method signature, but basically if your method returns some type T, it guarantees it has some non-null value, and if it returnsOption<T> it means it either has value T, or null.

Java 7有一些称为零安全的功能,但它从最终版本中删除。你可以做:

Java 7 had some feature called null safety, but it was removed from the final release. You could do:

obj?.getA()?.getB()?.getC()?.getD()

此外,Java 8将添加一个名为的功能可选所以你可以安全地执行。

Moreover, Java 8 will add a feature called Optional so you would do it safely.

事实上,如果你真的想使用它,现在尝试 Null Object 模式。这意味着,您可以返回某些默认值,而不是返回纯粹的 null ,这不会触发NullPointerException。虽然,您需要添加一些更改您的getter

In fact, if you really want to use that now, try Null Object pattern. It means that instead of returning plain null you can return some sort of default value, which won't trigger NullPointerException. Though, you need add some changes to your getters

class Object {
   A getA() {
     // ...
     return a == null ? A.NULL : a;
   }
}

class A {
   static A NULL = new A(); // some default behaviour
   B getB() {
     if (this == NULL) return B.NULL;
     // ...
     return b == null ? B.NULL : b;
   }
}

编辑:想要实现它可以将它包装在一些功能界面中,然后调用它。

If you want utility to do it you can wrap it in some functional interface and then call it.

static boolean isNullResult(Callable call) throws Exception {
    try {
        return call.call() == null;
    } catch (NullPointerException npe) {
        return true;
    }
}

使用方法如下:

isNullResult(new Callable<Integer>() {
    @Override
    public Integer call() throws Exception {
        return new A().getB().getC().getInt();
    }
});

不需要您更改现有功能

这篇关于检查方法链中的最后一个getter是否不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆