summaryrefslogtreecommitdiff
path: root/app/src/main/java/io/trygvis/android/Optional.java
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-01-18 12:52:17 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2015-01-18 12:52:17 +0100
commitfe238450f161a503d61c5ae59ecdd82c60c0e9ec (patch)
tree075ac1aa9b90d6895505d203a806d262bc41f273 /app/src/main/java/io/trygvis/android/Optional.java
parentf3288422d8dec949fcad33a84e413d8aa45f4500 (diff)
downloadio.trygvis.soilmoisture-android-1.0.tar.gz
io.trygvis.soilmoisture-android-1.0.tar.bz2
io.trygvis.soilmoisture-android-1.0.tar.xz
io.trygvis.soilmoisture-android-1.0.zip
BtPromise: Adding a distinction between successful and failure.1.0
o Chain can now call fail() instead of stop() to signal failure. o The finally handlers can be changed to get this info later. o Should probably make all callbacks take the BtDevice as a callback instead of BluetoothGatt and make the gatt instance available through the device. This way state can be kept in the device's tag. BtPromise: always discover services when operating inside a connection.
Diffstat (limited to 'app/src/main/java/io/trygvis/android/Optional.java')
-rw-r--r--app/src/main/java/io/trygvis/android/Optional.java45
1 files changed, 45 insertions, 0 deletions
diff --git a/app/src/main/java/io/trygvis/android/Optional.java b/app/src/main/java/io/trygvis/android/Optional.java
new file mode 100644
index 0000000..616f9a7
--- /dev/null
+++ b/app/src/main/java/io/trygvis/android/Optional.java
@@ -0,0 +1,45 @@
+package io.trygvis.android;
+
+public final class Optional<T> {
+ 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<T> consumer) {
+ if (value == null) {
+ return;
+ }
+
+ consumer.accept(value);
+ }
+
+ public static <T> Optional<T> of(T t) {
+ if (t == null) {
+ throw new IllegalArgumentException("t can't be null");
+ }
+
+ return new Optional<>(t);
+ }
+
+ public static <T> Optional<T> empty() {
+ return new Optional<>(null);
+ }
+
+ public static <T> Optional<T> ofNullable(T t) {
+ return t != null ? of(t) : empty();
+ }
+}