如何修复 ‘Optional.get()’ 而不使用 ‘isPresent()’。

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

how to fix 'Optional.get()' without 'isPresent()'

问题

Here is the translated code portion:

我一直在查看一些帖子但找不到任何关于尝试对我的代码部分进行此操作的参考

public static SlotType Motor_IN = new SlotType(
    "motor_in",
    (t, i, d) -> {
        if (t instanceof TileEntityMachineFrame) {
            return Optional.of(
                (new SlotMotor(
                    ((TileEntityMachineFrame) t).itItemHandler.get().motorInputWrapper,
                    i, d.x, d.y
                ))
            );
        }
        return null;
    }
);
英文:

I have been looking through some threads but cannot find any reference to try to do this to my part of code

public static SlotType Motor_IN = new SlotType(
    "motor_in", 
    (t, i, d) ->{
        if(t instanceof TileEntityMachineFrame){
            return Optional.of(
                 (new SlotMotor(
                     ( (TileEntityMachineFrame) t ).itItemHandler.get().motorInputWrapper, 
                     i, d.x, d.y
                  ))
            );
        }
        return null;
     }
);

答案1

得分: 1

You've left off a bit of information. Right now it appears your code returns Optional or null. That is the point of Optional though, it might not have anything.

if (t instanceof TileEntityMachineFrame) {
    //...
} else {
    return Optional.empty();
}

That way you return an empty optional and not null. The next issue is your .get is giving you a warning. Since it is on an Optional, you have to consider that the Optional will be empty. The simplest replacement would be to replace get with Optional.orElse. So what happens to your constructor then?

You could replace the .get call with map.

return ((TileEntityMachineFrame) t).itItemHandler.map(handler -> new SlotMotor(handler.motorInputWrapper, i, d.x, d.y));

From the javadocs for Map, "If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional."

Breaking it down more

I think you've tried to bunch up too much, you should spread it out a bit to better understand what you're doing.

Optional<ItemHandler> opt = ((TileEntityMachineFrame) t).itItemHandler;
ItemHandler handler = opt.get();
SlotMotor motor = new SlotMotor(handler.motorInputWrapper, i, d.x, d.y);
Optional<SlotMotor> optionalMotor = Optional.of(motor);

In that code, you're getting a warning because there is a call to get without using isPresent. One way to change that is to use map.

Optional<SlotMotor> optionalMotor = opt.map(handler -> new SlotMotor(handler.motorInputWrapper, i, d.x, d.y));

There isn't a missing call to get because we're working inside of the lambda passed to map. Plus this automatically gets you the optional you return.

英文:

You've left off a bit of information. Right now it appears your code returns Optional or null. That is the point of Optional though, it might not have anything.

if(t instanceof TileEntityMachineFrame){
    //...
} else{
    return Optional.empty();
}

That way you return an empty optional and not null. The next issue is your .get is giving you a warning. Since it is on an Optional, you have to consider that the Optional will be empty. The simplest replacement would be to replace get with Optional.orElse. So what happens to your constructor then?

You could replace the .get call with map.

return ( (TileEntityMachineFrame) t ).itItemHandler.map( 
    handler-&gt; new SlotMotor( handler.motorInputWrapper, i, d.x, d.y )
);

From the javadocs for Map, "If a value is present, returns an Optional describing (as if by ofNullable(T)) the result of applying the given mapping function to the value, otherwise returns an empty Optional. "

Breaking it down more

I think you've tried to bunch up too much, you should spread it out a bit to better understand what youre doing.

Optional&lt;ItemHandler&gt; opt = ( (TileEntityMachineFrame) t ).itItemHandler;
ItemHandler handler = opt.get();
SlotMotor motor = new SlotMotor( handler.motorInputWrapper, i, d.x, d.y);
Optional&lt;SlotMotor&gt; optionalMotor = Optional.of(motor);

In that code You're getting a warning because there is a call to get without using isPresent one way to change that is to use map.

Optional&lt;SlotMotor&gt; optionalMotor = opt.map( 
     handler -&gt; new SlotMotor( handler.motorInputWrapper, i, d.x, d.y )      
);

There isn't a missing call to get because we're working inside of the lambda passed to map. Plus this automatically gets you the optional you return.

huangapple
  • 本文由 发表于 2020年8月2日 18:06:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63214709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定