From 048e5b679cafb1c8bb52f8f27482d586bc72ab22 Mon Sep 17 00:00:00 2001 From: Daniel Henninger Date: Thu, 27 Dec 2007 15:54:56 +0000 Subject: [PATCH] [SMACK-235] Switched to a much faster, but also thread safe, concurrenthashmap for entries list. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@9687 b35dd754-fafc-0310-a699-88a17e54d16e --- source/org/jivesoftware/smack/Roster.java | 43 +++++++++-------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/source/org/jivesoftware/smack/Roster.java b/source/org/jivesoftware/smack/Roster.java index 20bc4cccf..5f739fd20 100644 --- a/source/org/jivesoftware/smack/Roster.java +++ b/source/org/jivesoftware/smack/Roster.java @@ -57,7 +57,7 @@ public class Roster { private XMPPConnection connection; private final Map groups; - private final List entries; + private final Map entries; private final List unfiledEntries; private final List rosterListeners; private Map> presenceMap; @@ -101,7 +101,7 @@ public class Roster { this.connection = connection; groups = new ConcurrentHashMap(); unfiledEntries = new CopyOnWriteArrayList(); - entries = new CopyOnWriteArrayList(); + entries = new ConcurrentHashMap(); rosterListeners = new CopyOnWriteArrayList(); presenceMap = new ConcurrentHashMap>(); // Listen for any roster packets. @@ -271,7 +271,7 @@ public class Roster { public void removeEntry(RosterEntry entry) throws XMPPException { // Only remove the entry if it's in the entry list. // The actual removal logic takes place in RosterPacketListenerprocess>>Packet(Packet) - if (!entries.contains(entry)) { + if (!entries.containsKey(entry.getUser())) { return; } RosterPacket packet = new RosterPacket(); @@ -353,13 +353,7 @@ public class Roster { if (user == null) { return null; } - String userLowerCase = user.toLowerCase(); - for (RosterEntry entry : entries) { - if (entry.getUser().equals(userLowerCase)) { - return entry; - } - } - return null; + return entries.get(user.toLowerCase()); } /** @@ -671,11 +665,9 @@ public class Roster { // Add the new presence, using the resources as a key. userPresences.put(StringUtils.parseResource(from), presence); // If the user is in the roster, fire an event. - for (RosterEntry entry : entries) { - if (entry.getUser().equals(key)) { - fireRosterPresenceEvent(presence); - } - } + RosterEntry entry = entries.get(key); + if (entry != null) + fireRosterPresenceEvent(presence); } // If an "unavailable" packet. else if (presence.getType() == Presence.Type.unavailable) { @@ -701,11 +693,9 @@ public class Roster { userPresences.put(StringUtils.parseResource(from), presence); } // If the user is in the roster, fire an event. - for (RosterEntry entry : entries) { - if (entry.getUser().equals(key)) { - fireRosterPresenceEvent(presence); - } - } + RosterEntry entry = entries.get(key); + if (entry != null) + fireRosterPresenceEvent(presence); } else if (presence.getType() == Presence.Type.subscribe) { if (subscriptionMode == SubscriptionMode.accept_all) { @@ -756,8 +746,8 @@ public class Roster { // If the packet is of the type REMOVE then remove the entry if (RosterPacket.ItemType.remove.equals(item.getItemType())) { // Remove the entry from the entry list. - if (entries.contains(entry)) { - entries.remove(entry); + if (entries.containsKey(item.getUser())) { + entries.remove(item.getUser()); } // Remove the entry from the unfiled entry list. if (unfiledEntries.contains(entry)) { @@ -773,16 +763,15 @@ public class Roster { } else { // Make sure the entry is in the entry list. - if (!entries.contains(entry)) { - entries.add(entry); + if (!entries.containsKey(item.getUser())) { + entries.put(item.getUser(), entry); // Keep note that an entry has been added addedEntries.add(item.getUser()); } else { // If the entry was in then list then update its state with the new values - RosterEntry existingEntry = entries.get(entries.indexOf(entry)); - existingEntry - .updateState(entry.getName(), entry.getType(), entry.getStatus()); + entries.put(item.getUser(), entry); + // Keep note that an entry has been updated updatedEntries.add(item.getUser()); }