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