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

javatest.utils.OptionalTest Maven / Gradle / Ivy

package javatest.utils;

import java.util.NoSuchElementException;
import java.util.Optional;

public class OptionalTest {
	static public void main(String[] args) {
		System.out.println("OptionalTest.main:");
		test(Optional.empty(), null);
		test(Optional.empty(), 1);
		test(Optional.of(1), 2);
		test(Optional.ofNullable(1), 2);
		test(Optional.ofNullable(null), 2);
		testInt(Optional.empty());
		testInt(Optional.of(7));
	}

	static private  void test(Optional opt, T optDefault) {
		System.out.println(opt.isPresent());
		System.out.println(opt.orElse(optDefault));
		try {
			System.out.println(opt.get());
		} catch (NoSuchElementException e) {
			System.out.println(e.getMessage());
		}
	}

	static private void testInt(Optional opt) {
		System.out.println(opt.map(it -> it * 2));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy