mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Made UniqueId a subclass of Bytes.
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
package org.briarproject.api;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
/** A wrapper around a byte array, to allow it to be stored in maps etc. */
|
||||
public class Bytes {
|
||||
/**
|
||||
* A wrapper around a byte array, to allow it to be stored in maps etc.
|
||||
*/
|
||||
public class Bytes implements Comparable<Bytes> {
|
||||
|
||||
public static final BytesComparator COMPARATOR = new BytesComparator();
|
||||
|
||||
private final byte[] bytes;
|
||||
|
||||
@@ -27,8 +32,26 @@ public class Bytes {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o instanceof Bytes)
|
||||
return Arrays.equals(bytes, ((Bytes) o).bytes);
|
||||
return false;
|
||||
return o instanceof Bytes && Arrays.equals(bytes, ((Bytes) o).bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Bytes other) {
|
||||
byte[] aBytes = bytes, bBytes = other.bytes;
|
||||
int length = Math.min(aBytes.length, bBytes.length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
int aUnsigned = aBytes[i] & 0xFF, bUnsigned = bBytes[i] & 0xFF;
|
||||
if (aUnsigned < bUnsigned) return -1;
|
||||
if (aUnsigned > bUnsigned) return 1;
|
||||
}
|
||||
return aBytes.length - bBytes.length;
|
||||
}
|
||||
|
||||
public static class BytesComparator implements Comparator<Bytes> {
|
||||
|
||||
@Override
|
||||
public int compare(Bytes a, Bytes b) {
|
||||
return a.compareTo(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.briarproject.api;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/** Type-safe wrapper for a byte array that uniquely identifies a device. */
|
||||
/**
|
||||
* Type-safe wrapper for a byte array that uniquely identifies a device.
|
||||
*/
|
||||
public class DeviceId extends UniqueId {
|
||||
|
||||
public DeviceId(byte[] id) {
|
||||
@@ -11,6 +11,6 @@ public class DeviceId extends UniqueId {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof DeviceId && Arrays.equals(id, ((DeviceId) o).id);
|
||||
return o instanceof DeviceId && super.equals(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,47 +1,12 @@
|
||||
package org.briarproject.api;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public abstract class UniqueId {
|
||||
public abstract class UniqueId extends Bytes {
|
||||
|
||||
/** The length of a unique identifier in bytes. */
|
||||
public static final int LENGTH = 32;
|
||||
|
||||
protected final byte[] id;
|
||||
|
||||
private int hashCode = -1;
|
||||
|
||||
protected UniqueId(byte[] id) {
|
||||
super(id);
|
||||
if (id.length != LENGTH) throw new IllegalArgumentException();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public byte[] getBytes() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
// Thread-safe because if two or more threads check and update the
|
||||
// value, they'll calculate the same value
|
||||
if (hashCode == -1) hashCode = Arrays.hashCode(id);
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
public static class IdComparator implements Comparator<UniqueId> {
|
||||
|
||||
public static final IdComparator INSTANCE = new IdComparator();
|
||||
|
||||
@Override
|
||||
public int compare(UniqueId a, UniqueId b) {
|
||||
byte[] aBytes = a.getBytes(), bBytes = b.getBytes();
|
||||
for (int i = 0; i < UniqueId.LENGTH; i++) {
|
||||
int aUnsigned = aBytes[i] & 0xFF, bUnsigned = bBytes[i] & 0xFF;
|
||||
if (aUnsigned < bUnsigned) return -1;
|
||||
if (aUnsigned > bUnsigned) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.api.identity;
|
||||
import org.briarproject.api.UniqueId;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a byte array that uniquely identifies an
|
||||
@@ -11,7 +10,9 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class AuthorId extends UniqueId {
|
||||
|
||||
/** Label for hashing authors to calculate their identities. */
|
||||
/**
|
||||
* Label for hashing authors to calculate their identities.
|
||||
*/
|
||||
public static final byte[] LABEL =
|
||||
"AUTHOR_ID".getBytes(Charset.forName("US-ASCII"));
|
||||
|
||||
@@ -21,6 +22,6 @@ public class AuthorId extends UniqueId {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof AuthorId && Arrays.equals(id, ((AuthorId) o).id);
|
||||
return o instanceof AuthorId && super.equals(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package org.briarproject.api.sync;
|
||||
|
||||
import org.briarproject.api.UniqueId;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a byte array that uniquely identifies a sync client.
|
||||
*/
|
||||
@@ -15,6 +13,6 @@ public class ClientId extends UniqueId {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof ClientId && Arrays.equals(id, ((ClientId) o).id);
|
||||
return o instanceof ClientId && super.equals(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,15 @@ package org.briarproject.api.sync;
|
||||
import org.briarproject.api.UniqueId;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a byte array that uniquely identifies a {@link Group}.
|
||||
*/
|
||||
public class GroupId extends UniqueId {
|
||||
|
||||
/** Label for hashing groups to calculate their identifiers. */
|
||||
/**
|
||||
* Label for hashing groups to calculate their identifiers.
|
||||
*/
|
||||
public static final byte[] LABEL =
|
||||
"GROUP_ID".getBytes(Charset.forName("US-ASCII"));
|
||||
|
||||
@@ -20,6 +21,6 @@ public class GroupId extends UniqueId {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof GroupId && Arrays.equals(id, ((GroupId) o).id);
|
||||
return o instanceof GroupId && super.equals(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.api.sync;
|
||||
import org.briarproject.api.UniqueId;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Type-safe wrapper for a byte array that uniquely identifies a
|
||||
@@ -11,7 +10,9 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class MessageId extends UniqueId {
|
||||
|
||||
/** Label for hashing messages to calculate their identifiers. */
|
||||
/**
|
||||
* Label for hashing messages to calculate their identifiers.
|
||||
*/
|
||||
public static final byte[] LABEL =
|
||||
"MESSAGE_ID".getBytes(Charset.forName("US-ASCII"));
|
||||
|
||||
@@ -21,6 +22,6 @@ public class MessageId extends UniqueId {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
return o instanceof MessageId && Arrays.equals(id, ((MessageId) o).id);
|
||||
return o instanceof MessageId && super.equals(o);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package org.briarproject.sync;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
|
||||
import org.briarproject.api.UniqueId;
|
||||
import org.briarproject.api.Bytes;
|
||||
import org.briarproject.api.contact.Contact;
|
||||
import org.briarproject.api.data.BdfWriter;
|
||||
import org.briarproject.api.data.BdfWriterFactory;
|
||||
@@ -40,7 +40,7 @@ class PrivateGroupFactoryImpl implements PrivateGroupFactory {
|
||||
BdfWriter w = bdfWriterFactory.createWriter(out);
|
||||
try {
|
||||
w.writeListStart();
|
||||
if (UniqueId.IdComparator.INSTANCE.compare(local, remote) < 0) {
|
||||
if (Bytes.COMPARATOR.compare(local, remote) < 0) {
|
||||
w.writeRaw(local.getBytes());
|
||||
w.writeRaw(remote.getBytes());
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user