From 3e619a735e63a1222e71060d9e65b354a156b158 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 28 Jan 2015 23:45:38 +0100 Subject: o Major refactoring on the BtPromise, mainly internal. Renaming BtPromise to BtSequence and BtSequencer. --- bt/src/main/java/io/trygvis/android/Optional.java | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 bt/src/main/java/io/trygvis/android/Optional.java (limited to 'bt/src/main/java/io/trygvis/android/Optional.java') diff --git a/bt/src/main/java/io/trygvis/android/Optional.java b/bt/src/main/java/io/trygvis/android/Optional.java new file mode 100644 index 0000000..616f9a7 --- /dev/null +++ b/bt/src/main/java/io/trygvis/android/Optional.java @@ -0,0 +1,45 @@ +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(); + } +} -- cgit v1.2.3