package io.trygvis.android; public final class Optional { private final T value; public Optional(T value) { this.value = value; } public T get() { if (value == null) { throw new IllegalStateException("get() on empty"); } return value; } public boolean isPresent() { return value != null; } public void ifPresent(Consumer consumer) { if (value == null) { return; } consumer.accept(value); } public static Optional of(T t) { if (t == null) { throw new IllegalArgumentException("t can't be null"); } return new Optional<>(t); } public static Optional empty() { return new Optional<>(null); } public static Optional ofNullable(T t) { return t != null ? of(t) : empty(); } }