mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Add utility method for null checks.
This commit is contained in:
@@ -6,10 +6,20 @@ import javax.annotation.Nullable;
|
||||
public class NullSafety {
|
||||
|
||||
/**
|
||||
* Stand-in for `Objects.requireNonNull()`.
|
||||
* Stand-in for {@code Objects.requireNonNull()}.
|
||||
*/
|
||||
public static <T> T requireNonNull(@Nullable T t) {
|
||||
if (t == null) throw new NullPointerException();
|
||||
return t;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that exactly one of the arguments is null.
|
||||
*
|
||||
* @throws AssertionError If both or neither of the arguments are null
|
||||
*/
|
||||
public static void requireExactlyOneNull(@Nullable Object a,
|
||||
@Nullable Object b) {
|
||||
if ((a == null) == (b == null)) throw new AssertionError();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireExactlyOneNull;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class StreamContext {
|
||||
@@ -26,8 +28,7 @@ public class StreamContext {
|
||||
@Nullable PendingContactId pendingContactId,
|
||||
TransportId transportId, SecretKey tagKey, SecretKey headerKey,
|
||||
long streamNumber, boolean handshakeMode) {
|
||||
if ((contactId == null) == (pendingContactId == null))
|
||||
throw new IllegalArgumentException();
|
||||
requireExactlyOneNull(contactId, pendingContactId);
|
||||
this.contactId = contactId;
|
||||
this.pendingContactId = pendingContactId;
|
||||
this.transportId = transportId;
|
||||
|
||||
@@ -7,6 +7,8 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireExactlyOneNull;
|
||||
|
||||
/**
|
||||
* A set of keys for communicating with a given contact or pending contact
|
||||
* over a given transport.
|
||||
@@ -24,8 +26,7 @@ public class TransportKeySet {
|
||||
|
||||
public TransportKeySet(KeySetId keySetId, @Nullable ContactId contactId,
|
||||
@Nullable PendingContactId pendingContactId, TransportKeys keys) {
|
||||
if ((contactId == null) == (pendingContactId == null))
|
||||
throw new IllegalArgumentException();
|
||||
requireExactlyOneNull(contactId, pendingContactId);
|
||||
this.keySetId = keySetId;
|
||||
this.contactId = contactId;
|
||||
this.pendingContactId = pendingContactId;
|
||||
|
||||
@@ -8,6 +8,8 @@ import org.briarproject.bramble.api.transport.KeySetId;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireExactlyOneNull;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
class MutableTransportKeySet {
|
||||
@@ -22,8 +24,7 @@ class MutableTransportKeySet {
|
||||
MutableTransportKeySet(KeySetId keySetId, @Nullable ContactId contactId,
|
||||
@Nullable PendingContactId pendingContactId,
|
||||
MutableTransportKeys keys) {
|
||||
if ((contactId == null) == (pendingContactId == null))
|
||||
throw new IllegalArgumentException();
|
||||
requireExactlyOneNull(contactId, pendingContactId);
|
||||
this.keySetId = keySetId;
|
||||
this.contactId = contactId;
|
||||
this.pendingContactId = pendingContactId;
|
||||
|
||||
@@ -36,6 +36,7 @@ import javax.annotation.concurrent.ThreadSafe;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireExactlyOneNull;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.MAX_CLOCK_DIFFERENCE;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.PROTOCOL_VERSION;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
|
||||
@@ -136,6 +137,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
private void addKeys(KeySetId keySetId, @Nullable ContactId contactId,
|
||||
@Nullable PendingContactId pendingContactId,
|
||||
MutableTransportKeys keys) {
|
||||
requireExactlyOneNull(contactId, pendingContactId);
|
||||
MutableTransportKeySet ks = new MutableTransportKeySet(keySetId,
|
||||
contactId, pendingContactId, keys);
|
||||
this.keys.put(keySetId, ks);
|
||||
@@ -184,6 +186,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
@Nullable
|
||||
private MutableTransportKeySet getOutgoingKeySet(@Nullable ContactId c,
|
||||
@Nullable PendingContactId p) {
|
||||
requireExactlyOneNull(c, p);
|
||||
if (c == null) return pendingContactOutContexts.get(p);
|
||||
else return contactOutContexts.get(c);
|
||||
}
|
||||
@@ -475,6 +478,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
|
||||
@Nullable PendingContactId pendingContactId,
|
||||
MutableIncomingKeys inKeys, long streamNumber,
|
||||
boolean handshakeMode) {
|
||||
requireExactlyOneNull(contactId, pendingContactId);
|
||||
this.keySetId = keySetId;
|
||||
this.contactId = contactId;
|
||||
this.pendingContactId = pendingContactId;
|
||||
|
||||
Reference in New Issue
Block a user