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

se.ugli.commons.Option Maven / Gradle / Ivy

package se.ugli.commons;

import java.util.NoSuchElementException;

public class Option extends ValueObject {

	private static final long serialVersionUID = 5493989609477736170L;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static  Option apply(final T value) {
		return new Option(value);
	}

	public static  Option none() {
		return apply(null);
	}

	private Option(final T value) {
		super(value);
	}

	public T get() {
		if (isDefined())
			return value;
		throw new NoSuchElementException();
	}

	public T getOrElse(final T defaultValue) {
		if (isDefined())
			return value;
		return defaultValue;
	}

	public T getOrNull() {
		return getOrElse(null);
	}

	public boolean isDefined() {
		return value != null;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy