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

fr.vergne.pester.util.optional.BiOptional Maven / Gradle / Ivy

The newest version!
package fr.vergne.pester.util.optional;

import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

public class BiOptional {

	private final Optional u;
	private final Optional v;

	BiOptional(Optional u, Optional v) {
		this.u = u;
		this.v = v;
	}

	public static  BiOptional ofNullables(U u, V v) {
		return new BiOptional<>(Optional.ofNullable(u), Optional.ofNullable(v));
	}

	public  BiOptional mapEach(Function mapper) {
		return new BiOptional<>(u.map(mapper), v.map(mapper));
	}

	public  Optional mapBoth(BiFunction mapper, Function mapperFirst,
			Function mapperSecond) {
		if (u.isPresent() && v.isPresent()) {
			return Optional.ofNullable(mapper.apply(u.get(), v.get()));
		} else if (u.isPresent()) {
			return u.map(mapperFirst);
		} else if (v.isPresent()) {
			return v.map(mapperSecond);
		} else {
			return Optional.empty();
		}
	}

}