io.katharsis.utils.java.Optional Maven / Gradle / Ivy
package io.katharsis.utils.java;
import java.util.NoSuchElementException;
import java.util.Objects;
public class Optional {
private static final Optional> EMPTY = new Optional(null);
private final T value;
private Optional(T value) {
this.value = value;
}
public boolean isPresent() {
return value != null;
}
public T get() {
if (value == null) {
throw new NoSuchElementException("No value present");
}
return value;
}
@SuppressWarnings("unchecked")
public static Optional empty() {
return (Optional) EMPTY;
}
public static Optional of(T value) {
Objects.requireNonNull(value);
return new Optional(value);
}
public static Optional ofNullable(T value) {
return value == null ? Optional.empty() : of(value);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy