From 7874daa59bf8a5e5631a23211b410333a7161595 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 22 Dec 2014 16:12:34 +0100 Subject: [PATCH] Use switch-case for Presence.getType() --- .../java/org/jivesoftware/smack/Roster.java | 44 ++++++++++--------- .../smackx/muc/MultiUserChat.java | 10 +++-- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/Roster.java b/smack-core/src/main/java/org/jivesoftware/smack/Roster.java index ea2b21e0b..5d98ee735 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/Roster.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/Roster.java @@ -849,12 +849,15 @@ public class Roster { Presence presence = (Presence) packet; String from = presence.getFrom(); String key = getPresenceMapKey(from); + Map userPresences; + RosterEntry entry; + Presence response = null; // If an "available" presence, add it to the presence map. Each presence // map will hold for a particular user a map with the presence // packets saved for each resource. - if (presence.getType() == Presence.Type.available) { - Map userPresences; + switch (presence.getType()) { + case available: // Get the user presence map if (presenceMap.get(key) == null) { userPresences = new ConcurrentHashMap(); @@ -869,17 +872,16 @@ public class Roster { // Add the new presence, using the resources as a key. userPresences.put(XmppStringUtils.parseResource(from), presence); // If the user is in the roster, fire an event. - RosterEntry entry = entries.get(key); + entry = entries.get(key); if (entry != null) { fireRosterPresenceEvent(presence); } - } + break; // If an "unavailable" packet. - else if (presence.getType() == Presence.Type.unavailable) { + case unavailable: // If no resource, this is likely an offline presence as part of // a roster presence flood. In that case, we store it. if ("".equals(XmppStringUtils.parseResource(from))) { - Map userPresences; // Get the user presence map if (presenceMap.get(key) == null) { userPresences = new ConcurrentHashMap(); @@ -892,19 +894,18 @@ public class Roster { } // Otherwise, this is a normal offline presence. else if (presenceMap.get(key) != null) { - Map userPresences = presenceMap.get(key); + userPresences = presenceMap.get(key); // Store the offline presence, as it may include extra information // such as the user being on vacation. userPresences.put(XmppStringUtils.parseResource(from), presence); } // If the user is in the roster, fire an event. - RosterEntry entry = entries.get(key); + entry = entries.get(key); if (entry != null) { fireRosterPresenceEvent(presence); } - } - else if (presence.getType() == Presence.Type.subscribe) { - Presence response = null; + break; + case subscribe: switch (subscriptionMode) { case accept_all: // Accept all subscription requests. @@ -923,24 +924,24 @@ public class Roster { response.setTo(presence.getFrom()); connection.sendPacket(response); } - } - else if (presence.getType() == Presence.Type.unsubscribe) { + break; + case unsubscribe: if (subscriptionMode != SubscriptionMode.manual) { // Acknowledge and accept unsubscription notification so that the // server will stop sending notifications saying that the contact // has unsubscribed to our presence. - Presence response = new Presence(Presence.Type.unsubscribed); + response = new Presence(Presence.Type.unsubscribed); response.setTo(presence.getFrom()); connection.sendPacket(response); } // Otherwise, in manual mode so ignore. - } + break; // Error presence packets from a bare JID mean we invalidate all existing // presence info for the user. - else if (presence.getType() == Presence.Type.error && - "".equals(XmppStringUtils.parseResource(from))) - { - Map userPresences; + case error: + if (!"".equals(XmppStringUtils.parseResource(from))) { + break; + } if (!presenceMap.containsKey(key)) { userPresences = new ConcurrentHashMap(); presenceMap.put(key, userPresences); @@ -953,10 +954,13 @@ public class Roster { // Set the new presence using the empty resource as a key. userPresences.put("", presence); // If the user is in the roster, fire an event. - RosterEntry entry = entries.get(key); + entry = entries.get(key); if (entry != null) { fireRosterPresenceEvent(presence); } + break; + default: + break; } } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java index cd3b26d65..33e73d4fa 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java @@ -159,7 +159,8 @@ public class MultiUserChat { String from = presence.getFrom(); String myRoomJID = MultiUserChat.this.room + "/" + nickname; boolean isUserStatusModification = presence.getFrom().equals(myRoomJID); - if (presence.getType() == Presence.Type.available) { + switch (presence.getType()) { + case available: Presence oldPresence = occupantsMap.put(from, presence); if (oldPresence != null) { // Get the previous occupant's affiliation & role @@ -187,8 +188,8 @@ public class MultiUserChat { } } } - } - else if (presence.getType() == Presence.Type.unavailable) { + break; + case unavailable: occupantsMap.remove(from); MUCUser mucUser = MUCUser.from(packet); if (mucUser != null && mucUser.getStatus() != null) { @@ -206,6 +207,9 @@ public class MultiUserChat { } } } + break; + default: + break; } for (PresenceListener listener : presenceListeners) { listener.processPresence(presence);