java8.AboutOptional Maven / Gradle / Ivy
package java8;
import com.sandwich.koan.Koan;
import java.util.Optional;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutOptional {
boolean optionalIsPresentField = false;
@Koan
public void isPresent() {
boolean optionalIsPresent = false;
Optional value = notPresent();
if (value.isPresent()) {
optionalIsPresent = true;
}
assertEquals(optionalIsPresent, __);
}
@Koan
public void ifPresentLambda() {
Optional value = notPresent();
value.ifPresent(x -> optionalIsPresentField = true);
assertEquals(optionalIsPresentField, __);
}
//use optional on api to signal that value is optional
public Optional notPresent() {
return Optional.empty();
}
private Optional present() {
return Optional.of("is present");
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy