java 番石榴 可选。如何正确使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11561789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:35:02  来源:igfitidea点击:

Guava Optional. How to use the correct

javaguavaoptional

提问by Aleksandr

I have a class

我有一堂课

private class TouchCommand {
  private int action;
  private int x;
  private int y;
...

When the command is executed, it is necessary to verify the field values ??- null / not null, and depending on it to produce longitudinal action. I want to use Options from Google Guava.

命令执行时,需要验证字段值??-null / not null,并根据它产生纵向动作。我想使用 Google Guava 中的选项。

Which solution is right? this:

哪种解决方案是正确的?这:

public boolean executeCommand() {
  Optional<Integer> optionalAction = Optional.fromNullable(action);
  ...

or:

或者:

private class TouchCommand {
  private Optional<Integer> action;
  private Optional<Integer> x;
  private Optional<Integer> y;
...

Given that the call to parseAction may also return a null (or absent):

鉴于对 parseAction 的调用也可能返回 null(或不存在):

TouchCommand touchCommand = new TouchCommand();
touchCommand.mAction = parseAction(xmlParser.getAttributeValue(namespace, "action"));
...

Questions:

问题:

  1. whether or not to do so: the method parseAction (and similar) returns Optional ?
  2. whether or not to do so: the field of class objects Optional ?
  3. whether or not to do so: when checking the fields of the class (assuming that they can be null) to convert them into objects Optional ?
  1. 是否这样做:方法 parseAction(和类似的)返回 Optional ?
  2. 是否这样做:类对象的字段 Optional ?
  3. 是否这样做:在检查类的字段(假设它们可以为空)以将它们转换为对象时 Optional ?

Thx.

谢谢。

回答by Louis Wasserman

Guava contributor here...

番石榴贡献者在这里...

Any or all of these things are fine, but some of them may be overkill.

这些东西中的任何一个或所有都很好,但其中一些可能是矫枉过正。

Generally, as discussed in this StackOverflow answer, Optionalis primarily used for two things: to make it clearer what you would've meantby null, and in method return values to make sure the caller takes care of the "absent" case (which it's easier to forget with null). We certainlydon't advocate replacing every nullable value with an Optionaleverywhere in your code -- we certainly don't do that within Guava itself!

一般来说,在讨论这个StackOverflow的答案Optional主要用于两件事情:这样可以很清楚你会一直意味着通过null,并在方法的返回值,以确保呼叫方采取“缺席”情况护理(它更容易忘记了null)。我们当然不提倡用Optional代码中的任何地方替换每个可为 null 的值——我们当然不会在 Guava 本身中这样做!

A lot of this will have to be your decision -- there's no universal rule, it's a relatively subjective judgement, and I don't have enough context to determine what I'd do in your place -- but based on what context you've provided, I'd consider making the methods return Optional, but probably wouldn't change any of the other fields or anything.

其中很多必须由你决定——没有普遍的规则,这是一个相对主观的判断,我没有足够的背景来决定我在你的位置上做什么——但基于你的背景”已经提供,我会考虑使方法 return Optional,但可能不会更改任何其他字段或任何内容。