All Downloads are FREE. Search and download functionalities are using the official Maven repository.

java8.AboutOptional Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
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