From afb2fb1326266b05dca272796db602ace8a644d4 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 13 Nov 2020 20:37:32 +0100 Subject: [PATCH] use CoW data structure for read markers. fixes #3904 --- .../siacs/conversations/entities/Message.java | 6 ++--- .../conversations/entities/ReadByMarker.java | 24 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index 51a8438db..ab9c64940 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -15,10 +15,10 @@ import java.lang.ref.WeakReference; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; -import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; import eu.siacs.conversations.Config; import eu.siacs.conversations.crypto.axolotl.FingerprintStatus; @@ -113,7 +113,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable private Message mPreviousMessage = null; private String axolotlFingerprint = null; private String errorMessage = null; - private Set readByMarkers = new HashSet<>(); + private Set readByMarkers = new CopyOnWriteArraySet<>(); private Boolean isGeoUri = null; private Boolean isEmojisOnly = null; @@ -206,7 +206,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable this.edits = Edit.fromJson(edited); this.oob = oob; this.errorMessage = errorMessage; - this.readByMarkers = readByMarkers == null ? new HashSet<>() : readByMarkers; + this.readByMarkers = readByMarkers == null ? new CopyOnWriteArraySet<>() : readByMarkers; this.markable = markable; this.deleted = deleted; this.bodyLanguage = bodyLanguage; diff --git a/src/main/java/eu/siacs/conversations/entities/ReadByMarker.java b/src/main/java/eu/siacs/conversations/entities/ReadByMarker.java index 9e1aaa5c7..4b1254f07 100644 --- a/src/main/java/eu/siacs/conversations/entities/ReadByMarker.java +++ b/src/main/java/eu/siacs/conversations/entities/ReadByMarker.java @@ -5,8 +5,8 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.Collection; -import java.util.HashSet; import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; import eu.siacs.conversations.xmpp.Jid; @@ -66,8 +66,8 @@ public class ReadByMarker { return jsonObject; } - public static Set fromJson(JSONArray jsonArray) { - HashSet readByMarkers = new HashSet<>(); + public static Set fromJson(final JSONArray jsonArray) { + final Set readByMarkers = new CopyOnWriteArraySet<>(); for(int i = 0; i < jsonArray.length(); ++i) { try { readByMarkers.add(fromJson(jsonArray.getJSONObject(i))); @@ -100,7 +100,7 @@ public class ReadByMarker { } public static Set from(Collection users) { - final HashSet markers = new HashSet<>(); + final Set markers = new CopyOnWriteArraySet<>(); for(MucOptions.User user : users) { markers.add(from(user)); } @@ -125,21 +125,21 @@ public class ReadByMarker { public static Set fromJsonString(String json) { try { return fromJson(new JSONArray(json)); - } catch (JSONException | NullPointerException e) { - return new HashSet<>(); + } catch (final JSONException | NullPointerException e) { + return new CopyOnWriteArraySet<>(); } } - public static JSONArray toJson(Set readByMarkers) { - JSONArray jsonArray = new JSONArray(); - for(ReadByMarker marker : readByMarkers) { + public static JSONArray toJson(final Set readByMarkers) { + final JSONArray jsonArray = new JSONArray(); + for(final ReadByMarker marker : readByMarkers) { jsonArray.put(marker.toJson()); } return jsonArray; } - public static boolean contains(ReadByMarker needle, Set readByMarkers) { - for(ReadByMarker marker : readByMarkers) { + public static boolean contains(ReadByMarker needle, final Set readByMarkers) { + for(final ReadByMarker marker : readByMarkers) { if (marker.realJid != null && needle.realJid != null) { if (marker.realJid.asBareJid().equals(needle.realJid.asBareJid())) { return true; @@ -163,7 +163,7 @@ public class ReadByMarker { } public static boolean allUsersRepresented(Collection users, Set markers, ReadByMarker marker) { - HashSet markersCopy = new HashSet<>(markers); + final Set markersCopy = new CopyOnWriteArraySet<>(markers); markersCopy.add(marker); return allUsersRepresented(users, markersCopy); }