mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 18:59:06 +01:00
Make GroupItem immutable and introduce copy constructors
This commit is contained in:
@@ -8,17 +8,19 @@ import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||
import org.briarproject.briar.api.privategroup.GroupMessageHeader;
|
||||
import org.briarproject.briar.api.privategroup.PrivateGroup;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
// This class is not thread-safe
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class GroupItem implements Comparable<GroupItem> {
|
||||
|
||||
private final PrivateGroup privateGroup;
|
||||
private final AuthorInfo authorInfo;
|
||||
private int messageCount, unreadCount;
|
||||
private long timestamp;
|
||||
private boolean dissolved;
|
||||
private final int messageCount, unreadCount;
|
||||
private final long timestamp;
|
||||
private final boolean dissolved;
|
||||
|
||||
GroupItem(PrivateGroup privateGroup, AuthorInfo authorInfo,
|
||||
GroupCount count, boolean dissolved) {
|
||||
@@ -30,23 +32,22 @@ class GroupItem implements Comparable<GroupItem> {
|
||||
this.dissolved = dissolved;
|
||||
}
|
||||
|
||||
GroupItem(GroupItem item) {
|
||||
GroupItem(GroupItem item, GroupMessageHeader header) {
|
||||
this.privateGroup = item.privateGroup;
|
||||
this.authorInfo = item.authorInfo;
|
||||
this.messageCount = item.messageCount + 1;
|
||||
this.unreadCount = item.unreadCount + (header.isRead() ? 0 : 1);
|
||||
this.timestamp = Math.max(header.getTimestamp(), item.timestamp);
|
||||
this.dissolved = item.dissolved;
|
||||
}
|
||||
|
||||
GroupItem(GroupItem item, boolean isDissolved) {
|
||||
this.privateGroup = item.privateGroup;
|
||||
this.authorInfo = item.authorInfo;
|
||||
this.messageCount = item.messageCount;
|
||||
this.unreadCount = item.unreadCount;
|
||||
this.timestamp = item.timestamp;
|
||||
this.dissolved = item.dissolved;
|
||||
}
|
||||
|
||||
void addMessageHeader(GroupMessageHeader header) {
|
||||
messageCount++;
|
||||
if (header.getTimestamp() > timestamp) {
|
||||
timestamp = header.getTimestamp();
|
||||
}
|
||||
if (!header.isRead()) {
|
||||
unreadCount++;
|
||||
}
|
||||
this.dissolved = isDissolved;
|
||||
}
|
||||
|
||||
GroupId getId() {
|
||||
@@ -85,8 +86,9 @@ class GroupItem implements Comparable<GroupItem> {
|
||||
return dissolved;
|
||||
}
|
||||
|
||||
void setDissolved() {
|
||||
dissolved = true;
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return getId().hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -171,11 +171,7 @@ class GroupListViewModel extends DbViewModel implements EventListener {
|
||||
GroupId g = header.getGroupId();
|
||||
List<GroupItem> list = updateListItem(groupItems,
|
||||
itemToTest -> itemToTest.getId().equals(g),
|
||||
itemToUpdate -> {
|
||||
GroupItem newItem = new GroupItem(itemToUpdate);
|
||||
newItem.addMessageHeader(header);
|
||||
return newItem;
|
||||
});
|
||||
itemToUpdate -> new GroupItem(itemToUpdate, header));
|
||||
if (list == null) return;
|
||||
// re-sort as the order of items may have changed
|
||||
Collections.sort(list);
|
||||
@@ -186,11 +182,7 @@ class GroupListViewModel extends DbViewModel implements EventListener {
|
||||
private void onGroupDissolved(GroupId groupId) {
|
||||
List<GroupItem> list = updateListItem(groupItems,
|
||||
itemToTest -> itemToTest.getId().equals(groupId),
|
||||
itemToUpdate -> {
|
||||
GroupItem newItem = new GroupItem(itemToUpdate);
|
||||
newItem.setDissolved();
|
||||
return newItem;
|
||||
});
|
||||
itemToUpdate -> new GroupItem(itemToUpdate, true));
|
||||
if (list == null) return;
|
||||
groupItems.setValue(new LiveResult<>(list));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user