// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.Optional;

public class OptionalDemo {
    public static String oldStyleAPI(int v) {
        if(v > 0) {
            return Integer.toString(v);
        } else {
            return null;
        }
    }
    public static Optional<String> newStyleAPI(int v) {
        if(v > 0) {
            return Optional.of(Integer.toString(v));
        } else {
            return Optional.empty();
        }
    }
    public static void testOldStyleAPI(int v) {
        String s = oldStyleAPI(v);
        if(s != null) {
            System.out.println(s);
        } else {
            System.out.println("No result");
        }
    }
    public static void testNewStyleAPI(int v) {
        Optional<String> s = newStyleAPI(v);
        if(s.isPresent()) {
            System.out.println(s.get());
        } else {
            System.out.println("No result");
        }
    }
    public static void testNewStyleAPISmarter(int v) {
        Optional<String> s = newStyleAPI(v);
        System.out.println(s.orElse("No result"));
    }
    public static void main(String[] args) {
        testOldStyleAPI(123);
        testOldStyleAPI(0);
        testNewStyleAPI(123);
        testNewStyleAPI(0);
        testNewStyleAPISmarter(123);
        testNewStyleAPISmarter(0);
    }
}
