From f882e46b33569cc55cf21c0a32d65cb19de5a8fd Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Mon, 4 Jan 2021 15:21:02 -0300 Subject: [PATCH] Make GroupItem immutable and introduce copy constructors --- .../android/privategroup/list/GroupItem.java | 38 ++++++++++--------- .../privategroup/list/GroupListViewModel.java | 12 +----- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupItem.java b/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupItem.java index a2e05c5dc..00ba7e97e 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupItem.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupItem.java @@ -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 { 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 { 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 { return dissolved; } - void setDissolved() { - dissolved = true; + @Override + public int hashCode() { + return getId().hashCode(); } @Override diff --git a/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupListViewModel.java b/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupListViewModel.java index 5d03f34e7..61842f4fd 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupListViewModel.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/privategroup/list/GroupListViewModel.java @@ -171,11 +171,7 @@ class GroupListViewModel extends DbViewModel implements EventListener { GroupId g = header.getGroupId(); List 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 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)); }