mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 03:09:04 +01:00
Introduce MailboxId sub-classes for even more type-safety
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public class InvalidMailboxIdException extends Exception {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxAuthToken extends MailboxId {
|
||||
public MailboxAuthToken(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxAuthToken} from the given string.
|
||||
*
|
||||
* @throws InvalidMailboxIdException if token is not valid.
|
||||
*/
|
||||
public static MailboxAuthToken fromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
return new MailboxAuthToken(bytesFromString(token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxFileId extends MailboxId {
|
||||
public MailboxFileId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxFileId} from the given string.
|
||||
*
|
||||
* @throws IllegalArgumentException if token is not valid.
|
||||
*/
|
||||
public static MailboxFileId fromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
return new MailboxFileId(bytesFromString(token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.briarproject.bramble.api.mailbox;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxFolderId extends MailboxId {
|
||||
public MailboxFolderId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxFolderId} from the given string.
|
||||
*
|
||||
* @throws IllegalArgumentException if token is not valid.
|
||||
*/
|
||||
public static MailboxFolderId fromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
return new MailboxFolderId(bytesFromString(token));
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
@@ -14,20 +15,26 @@ import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
public class MailboxId extends UniqueId {
|
||||
|
||||
public MailboxId(byte[] id) {
|
||||
public abstract class MailboxId extends UniqueId {
|
||||
MailboxId(byte[] id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link MailboxId} from the given string.
|
||||
* Returns valid {@link MailboxId} bytes from the given string.
|
||||
*
|
||||
* @throws IllegalArgumentException if token is not valid.
|
||||
* @throws InvalidMailboxIdException if token is not valid.
|
||||
*/
|
||||
public static MailboxId fromString(String token) {
|
||||
if (token.length() != 64) throw new IllegalArgumentException();
|
||||
return new MailboxId(fromHexString(token));
|
||||
static byte[] bytesFromString(@Nullable String token)
|
||||
throws InvalidMailboxIdException {
|
||||
if (token == null || token.length() != 64) {
|
||||
throw new InvalidMailboxIdException();
|
||||
}
|
||||
try {
|
||||
return fromHexString(token);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new InvalidMailboxIdException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -39,9 +46,4 @@ public class MailboxId extends UniqueId {
|
||||
public String toString() {
|
||||
return toHexString(getBytes()).toLowerCase(Locale.US);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof MailboxId && super.equals(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,10 @@ import javax.annotation.concurrent.Immutable;
|
||||
public class MailboxProperties {
|
||||
|
||||
private final String onionAddress;
|
||||
private final MailboxId authToken;
|
||||
private final MailboxAuthToken authToken;
|
||||
private final boolean owner;
|
||||
|
||||
public MailboxProperties(String onionAddress, MailboxId authToken,
|
||||
public MailboxProperties(String onionAddress, MailboxAuthToken authToken,
|
||||
boolean owner) {
|
||||
this.onionAddress = onionAddress;
|
||||
this.authToken = authToken;
|
||||
@@ -23,7 +23,7 @@ public class MailboxProperties {
|
||||
return onionAddress;
|
||||
}
|
||||
|
||||
public MailboxId getAuthToken() {
|
||||
public MailboxAuthToken getAuthToken() {
|
||||
return authToken;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user