英文:
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-> 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<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论