为何guava/java相对于Optional.isPresent(possible)可能使用了exist.isPresent()? [英] Why did guava/java use possible.isPresent() as opposed to Optional.isPresent(possible)?

查看:285
本文介绍了为何guava/java相对于Optional.isPresent(possible)可能使用了exist.isPresent()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

https://code.google.com/p/guava-libraries上/wiki/UsingAndAvoidingNullExplained 的解释是,番石榴(以及后来的Java 8)添加了一个通用类Optional,以清除空检查.

On https://code.google.com/p/guava-libraries/wiki/UsingAndAvoidingNullExplained it is explained that guava (and later java 8) adds a generic class Optional in order to clear up null checking.

如果函数返回Optional,则要求调用者在使用字符串之前先对其进行拆包.

If a function returns an Optional it requires the caller to unwrap the string before using it.

通常以

Optional<String> possible = returnAnAbsentOptional();
if(possible.isPresent()){
    System.out.println(possible.get())
}

如果returnAnAbsentOptional返回null,则我们再次拥有NPE.

If returnAnAbsentOptional returns null, we have a NPE all over again.

我的问题是,为什么Guava/Java会使用optional.isPresent(possible)而不可能对null值做出相应的选择?

My question is, why did Guava/Java use possible.isPresent() as opposed to Optional.isPresent(possible) which could react to the null value accordingly?

推荐答案

对空值做出反应的正确方法是抛出NullPointerException,这就是在possible == null时调用possible.isPresent()时得到的结果

Because the correct way to react to the null value is by throwing a NullPointerException, and that's what you get when you call possible.isPresent() when possible == null.

Java作为一种语言,允许引用类型的任何值都为null. Java用户可以根据需要选择null值,而不必使用null值,并在需要时正确处理它们.当他们失败时,可能会抛出NullPointerException.

Java as a language allows any value of a reference type to be null. It's up to the users of Java to not have null values when they don't want them, and to handle them correctly when they do want them. When they fail at that, a NullPointerException might be thrown.

Optional不能替代编写== null. Optional使用 null的替代方法.如果您选择不使用null,然后仍然使用null,则说明您已经创建了一个错误.

Optional is not an alternative to writing == null. Optional is an alternative to using null. If you choose to not use null, and then use null anyway, you've created a bug.

对程序员引入的错误做出反应的正确方法是什么?让我们在示例中尝试您的Optional.isPresent(value)想法:

What's the correct way to react to a programmer-introduced bug? Let's try your Optional.isPresent(value) idea in an example:

public abstract class BaseClass {
  static boolean optionalIsPresent(Optional<?> possible) {
    return (possible == null) ? false : possible.isPresent();
    // return possible.isPresent();
  }

  public final String name;

  public Optional<Integer> getID();

  protected BaseClass() {
    if (optionalIsPresent(getID())) {
      name = "number " + getID().get();
    } else {
      name = "nameless";
    }
  }
}

public class DerivedClass extends BaseClass {
  private final int id;

  public DerivedClass(int id) {
    this.id = id;
  }

  public Optional<Integer> getID() {
    return Optional.of(id);
  }
}

这里有一个程序员错误,他们试图在设置最终字段之前尝试使用它.

There is a programmer bug here, where they are trying to use a final field before it's set.

如果optionalIsPresent(null)返回false,则上面的代码没有错误执行,并且我们有一个对象,其行为与我们认为的指定行为不同:getID()存在,但name"nameless" .我们得到的结果就像我们一开始从未使用过Optional一样,只是对不存在"和存在"使用了null和non-null值.

If optionalIsPresent(null) returns false, then the code above executes with no errors, and we have an object with behavior different from what we thought we specified: getID() is present, but name is "nameless". We get the same result as if we had never used Optional in the first place, and just used null and non-null values for "absent" and "present".

但是,通过仅使用absent()表示absent(),我们的错误代码将抛出NullPointerException.

However, by using only absent() to represent absent(), our incorrect code throws a NullPointerException.

这篇关于为何guava/java相对于Optional.isPresent(possible)可能使用了exist.isPresent()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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