Make GroupItem immutable and introduce copy constructors

This commit is contained in:
Torsten Grote
2021-01-04 15:21:02 -03:00
parent 205b4f77b2
commit f882e46b33
2 changed files with 22 additions and 28 deletions

View File

@@ -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.GroupMessageHeader;
import org.briarproject.briar.api.privategroup.PrivateGroup; import org.briarproject.briar.api.privategroup.PrivateGroup;
import javax.annotation.concurrent.Immutable;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
// This class is not thread-safe @Immutable
@NotNullByDefault @NotNullByDefault
class GroupItem implements Comparable<GroupItem> { class GroupItem implements Comparable<GroupItem> {
private final PrivateGroup privateGroup; private final PrivateGroup privateGroup;
private final AuthorInfo authorInfo; private final AuthorInfo authorInfo;
private int messageCount, unreadCount; private final int messageCount, unreadCount;
private long timestamp; private final long timestamp;
private boolean dissolved; private final boolean dissolved;
GroupItem(PrivateGroup privateGroup, AuthorInfo authorInfo, GroupItem(PrivateGroup privateGroup, AuthorInfo authorInfo,
GroupCount count, boolean dissolved) { GroupCount count, boolean dissolved) {
@@ -30,23 +32,22 @@ class GroupItem implements Comparable<GroupItem> {
this.dissolved = dissolved; 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.privateGroup = item.privateGroup;
this.authorInfo = item.authorInfo; this.authorInfo = item.authorInfo;
this.messageCount = item.messageCount; this.messageCount = item.messageCount;
this.unreadCount = item.unreadCount; this.unreadCount = item.unreadCount;
this.timestamp = item.timestamp; this.timestamp = item.timestamp;
this.dissolved = item.dissolved; this.dissolved = isDissolved;
}
void addMessageHeader(GroupMessageHeader header) {
messageCount++;
if (header.getTimestamp() > timestamp) {
timestamp = header.getTimestamp();
}
if (!header.isRead()) {
unreadCount++;
}
} }
GroupId getId() { GroupId getId() {
@@ -85,8 +86,9 @@ class GroupItem implements Comparable<GroupItem> {
return dissolved; return dissolved;
} }
void setDissolved() { @Override
dissolved = true; public int hashCode() {
return getId().hashCode();
} }
@Override @Override

View File

@@ -171,11 +171,7 @@ class GroupListViewModel extends DbViewModel implements EventListener {
GroupId g = header.getGroupId(); GroupId g = header.getGroupId();
List<GroupItem> list = updateListItem(groupItems, List<GroupItem> list = updateListItem(groupItems,
itemToTest -> itemToTest.getId().equals(g), itemToTest -> itemToTest.getId().equals(g),
itemToUpdate -> { itemToUpdate -> new GroupItem(itemToUpdate, header));
GroupItem newItem = new GroupItem(itemToUpdate);
newItem.addMessageHeader(header);
return newItem;
});
if (list == null) return; if (list == null) return;
// re-sort as the order of items may have changed // re-sort as the order of items may have changed
Collections.sort(list); Collections.sort(list);
@@ -186,11 +182,7 @@ class GroupListViewModel extends DbViewModel implements EventListener {
private void onGroupDissolved(GroupId groupId) { private void onGroupDissolved(GroupId groupId) {
List<GroupItem> list = updateListItem(groupItems, List<GroupItem> list = updateListItem(groupItems,
itemToTest -> itemToTest.getId().equals(groupId), itemToTest -> itemToTest.getId().equals(groupId),
itemToUpdate -> { itemToUpdate -> new GroupItem(itemToUpdate, true));
GroupItem newItem = new GroupItem(itemToUpdate);
newItem.setDissolved();
return newItem;
});
if (list == null) return; if (list == null) return;
groupItems.setValue(new LiveResult<>(list)); groupItems.setValue(new LiveResult<>(list));
} }