Replace Maybe with nullable transaction method.

This commit is contained in:
akwizgran
2018-11-06 17:29:08 +00:00
parent 52c778dce3
commit c386a0f5eb
7 changed files with 59 additions and 69 deletions

View File

@@ -1,33 +0,0 @@
package org.briarproject.bramble.api;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.NoSuchElementException;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* Minimal stand-in for `java.util.Optional`. Functionality from `Optional`
* can be added as needed.
*/
@Immutable
@NotNullByDefault
public class Maybe<T> {
@Nullable
private final T value;
public Maybe(@Nullable T value) {
this.value = value;
}
public boolean isPresent() {
return value != null;
}
public T get() {
if (value == null) throw new NoSuchElementException();
return value;
}
}

View File

@@ -89,6 +89,14 @@ public interface DatabaseComponent {
<R, E extends Exception> R transactionWithResult(boolean readOnly,
DbCallable<R, E> task) throws DbException, E;
/**
* Runs the given task within a transaction and returns the result of the
* task, which may be null.
*/
@Nullable
<R, E extends Exception> R transactionWithNullableResult(boolean readOnly,
DbCallable<R, E> task) throws DbException, E;
/**
* Stores a contact associated with the given local and remote pseudonyms,
* and returns an ID for the contact.

View File

@@ -1,6 +1,12 @@
package org.briarproject.bramble.api.db;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
@NotNullByDefault
public interface DbCallable<R, E extends Exception> {
@Nullable
R call(Transaction txn) throws DbException, E;
}

View File

@@ -1,5 +1,8 @@
package org.briarproject.bramble.api.db;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
@NotNullByDefault
public interface DbRunnable<E extends Exception> {
void run(Transaction txn) throws DbException, E;