Unit tests and bugfixes for DatabaseComponent. Merged code from various unique ID classes into a common superclass.

This commit is contained in:
akwizgran
2011-07-05 16:58:44 +01:00
parent f97393f160
commit 13b3d4cc03
11 changed files with 590 additions and 90 deletions

View File

@@ -3,26 +3,10 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
/** Type-safe wrapper for a byte array that uniquely identifies an author. */
public class AuthorId {
public static final int LENGTH = 32;
// FIXME: Replace this with an isSelf() method that compares an AuthorId
// to any and all local AuthorIds.
public static final AuthorId SELF = new AuthorId(new byte[] {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
});
private final byte[] id;
public class AuthorId extends UniqueId {
public AuthorId(byte[] id) {
assert id.length == LENGTH;
this.id = id;
}
public byte[] getBytes() {
return id;
super(id);
}
@Override
@@ -31,9 +15,4 @@ public class AuthorId {
return Arrays.equals(id, ((AuthorId) o).id);
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
}

View File

@@ -6,19 +6,10 @@ import java.util.Arrays;
* Type-safe wrapper for a byte array that uniquely identifies a batch of
* messages.
*/
public class BatchId {
public static final int LENGTH = 32;
private final byte[] id;
public class BatchId extends UniqueId {
public BatchId(byte[] id) {
assert id.length == LENGTH;
this.id = id;
}
public byte[] getBytes() {
return id;
super(id);
}
@Override
@@ -27,9 +18,4 @@ public class BatchId {
return Arrays.equals(id, ((BatchId) o).id);
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
}

View File

@@ -6,24 +6,15 @@ import java.util.Arrays;
* Type-safe wrapper for a byte array that uniquely identifies a bundle of
* acknowledgements, subscriptions, and batches of messages.
*/
public class BundleId {
public class BundleId extends UniqueId {
public static final BundleId NONE = new BundleId(new byte[] {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
});
public static final int LENGTH = 32;
private final byte[] id;
public BundleId(byte[] id) {
assert id.length == LENGTH;
this.id = id;
}
public byte[] getBytes() {
return id;
super(id);
}
@Override
@@ -32,9 +23,4 @@ public class BundleId {
return Arrays.equals(id, ((BundleId) o).id);
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
}

View File

@@ -6,19 +6,10 @@ import java.util.Arrays;
* Type-safe wrapper for a byte array that uniquely identifies a group to which
* users may subscribe.
*/
public class GroupId {
public static final int LENGTH = 32;
private final byte[] id;
public class GroupId extends UniqueId {
public GroupId(byte[] id) {
assert id.length == LENGTH;
this.id = id;
}
public byte[] getBytes() {
return id;
super(id);
}
@Override
@@ -27,9 +18,4 @@ public class GroupId {
return Arrays.equals(id, ((GroupId) o).id);
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
}

View File

@@ -3,7 +3,7 @@ package net.sf.briar.api.protocol;
import java.util.Arrays;
/** Type-safe wrapper for a byte array that uniquely identifies a message. */
public class MessageId {
public class MessageId extends UniqueId {
/** Used to indicate that the first message in a thread has no parent. */
public static final MessageId NONE = new MessageId(new byte[] {
@@ -11,17 +11,8 @@ public class MessageId {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
});
public static final int LENGTH = 32;
private final byte[] id;
public MessageId(byte[] id) {
assert id.length == LENGTH;
this.id = id;
}
public byte[] getBytes() {
return id;
super(id);
}
@Override
@@ -30,9 +21,4 @@ public class MessageId {
return Arrays.equals(id, ((MessageId) o).id);
return false;
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
}

View File

@@ -0,0 +1,24 @@
package net.sf.briar.api.protocol;
import java.util.Arrays;
public abstract class UniqueId {
public static final int LENGTH = 32;
protected final byte[] id;
protected UniqueId(byte[] id) {
assert id.length == LENGTH;
this.id = id;
}
public byte[] getBytes() {
return id;
}
@Override
public int hashCode() {
return Arrays.hashCode(id);
}
}