Use MailboxId instead of String for type-safety

This commit is contained in:
Torsten Grote
2022-01-24 13:50:58 -03:00
parent 61ea7ff8de
commit f057f0859b
10 changed files with 125 additions and 65 deletions

View File

@@ -0,0 +1,47 @@
package org.briarproject.bramble.api.mailbox;
import com.fasterxml.jackson.annotation.JsonValue;
import org.briarproject.bramble.api.UniqueId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import java.util.Locale;
import javax.annotation.concurrent.ThreadSafe;
import static org.briarproject.bramble.util.StringUtils.fromHexString;
import static org.briarproject.bramble.util.StringUtils.toHexString;
@ThreadSafe
@NotNullByDefault
public class MailboxId extends UniqueId {
public MailboxId(byte[] id) {
super(id);
}
/**
* Creates a {@link MailboxId} from the given string.
*
* @throws IllegalArgumentException if token is not valid.
*/
public static MailboxId fromString(String token) {
if (token.length() != 64) throw new IllegalArgumentException();
return new MailboxId(fromHexString(token));
}
/**
* Returns the string representation expected by the mailbox API.
* Also used for serialization.
*/
@Override
@JsonValue
public String toString() {
return toHexString(getBytes()).toLowerCase(Locale.US);
}
@Override
public boolean equals(Object o) {
return o instanceof MailboxId && super.equals(o);
}
}

View File

@@ -8,10 +8,11 @@ import javax.annotation.concurrent.Immutable;
@NotNullByDefault
public class MailboxProperties {
private final String onionAddress, authToken;
private final String onionAddress;
private final MailboxId authToken;
private final boolean owner;
public MailboxProperties(String onionAddress, String authToken,
public MailboxProperties(String onionAddress, MailboxId authToken,
boolean owner) {
this.onionAddress = onionAddress;
this.authToken = authToken;
@@ -22,7 +23,7 @@ public class MailboxProperties {
return onionAddress;
}
public String getAuthToken() {
public MailboxId getAuthToken() {
return authToken;
}