1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

Remove duplicate code in Roster

By introducing getUserPresences(String) in PresencePacketListener.
This commit is contained in:
Florian Schmaus 2014-12-29 19:11:25 +01:00
parent c8631cf45c
commit 31e372bafd

View file

@ -845,6 +845,23 @@ public class Roster {
*/ */
private class PresencePacketListener implements PacketListener { private class PresencePacketListener implements PacketListener {
/**
* Retrieve the user presences (a map from resource to {@link Presence}) for a given key (usually a JID without
* a resource). If the {@link #presenceMap} does not contain already a user presence map, then it will be
* created.
*
* @param key the presence map key
* @return the user presences
*/
private Map<String, Presence> getUserPresences(String key) {
Map<String, Presence> userPresences = presenceMap.get(key);
if (userPresences == null) {
userPresences = new ConcurrentHashMap<>();
presenceMap.put(key, userPresences);
}
return userPresences;
}
public void processPacket(Packet packet) throws NotConnectedException { public void processPacket(Packet packet) throws NotConnectedException {
Presence presence = (Presence) packet; Presence presence = (Presence) packet;
String from = presence.getFrom(); String from = presence.getFrom();
@ -858,13 +875,7 @@ public class Roster {
switch (presence.getType()) { switch (presence.getType()) {
case available: case available:
// Get the user presence map // Get the user presence map
if (presenceMap.get(key) == null) { userPresences = getUserPresences(key);
userPresences = new ConcurrentHashMap<String, Presence>();
presenceMap.put(key, userPresences);
}
else {
userPresences = presenceMap.get(key);
}
// See if an offline presence was being stored in the map. If so, remove // See if an offline presence was being stored in the map. If so, remove
// it since we now have an online presence. // it since we now have an online presence.
userPresences.remove(""); userPresences.remove("");
@ -881,13 +892,7 @@ public class Roster {
// a roster presence flood. In that case, we store it. // a roster presence flood. In that case, we store it.
if ("".equals(XmppStringUtils.parseResource(from))) { if ("".equals(XmppStringUtils.parseResource(from))) {
// Get the user presence map // Get the user presence map
if (presenceMap.get(key) == null) { userPresences = getUserPresences(key);
userPresences = new ConcurrentHashMap<String, Presence>();
presenceMap.put(key, userPresences);
}
else {
userPresences = presenceMap.get(key);
}
userPresences.put("", presence); userPresences.put("", presence);
} }
// Otherwise, this is a normal offline presence. // Otherwise, this is a normal offline presence.
@ -939,15 +944,10 @@ public class Roster {
if (!"".equals(XmppStringUtils.parseResource(from))) { if (!"".equals(XmppStringUtils.parseResource(from))) {
break; break;
} }
if (!presenceMap.containsKey(key)) { userPresences = getUserPresences(key);
userPresences = new ConcurrentHashMap<String, Presence>();
presenceMap.put(key, userPresences);
}
else {
userPresences = presenceMap.get(key);
// Any other presence data is invalidated by the error packet. // Any other presence data is invalidated by the error packet.
userPresences.clear(); userPresences.clear();
}
// Set the new presence using the empty resource as a key. // Set the new presence using the empty resource as a key.
userPresences.put("", presence); userPresences.put("", presence);
// If the user is in the roster, fire an event. // If the user is in the roster, fire an event.