From 603f64166babf8ac68ca383ca7d2b2674b8a0f83 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 26 Jan 2015 16:24:32 +0100 Subject: [PATCH] Improve Roster, RosterEntry and RosterGroup Use final where possible, add javadoc. Fix duplicate comment in Roster. Use createPacketCollectorAndSend(packet).nextResultOrThrow when possible, and only set the entry name if the IQ set is successful. --- .../org/jivesoftware/smack/roster/Roster.java | 4 +--- .../smack/roster/RosterEntry.java | 19 +++++++++++++++---- .../smack/roster/RosterGroup.java | 10 ++++++---- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java index 69491fae7..ce8ae40f1 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java @@ -944,9 +944,7 @@ public class Roster extends Manager { unfiledEntries.remove(entry); } - // Add the user to the new groups - - // Add the entry to the groups + // Add the entry/user to the groups List newGroupNames = new ArrayList(); for (String groupName : item.getGroupNames()) { // Add the group name to the list. diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java index bc3870405..f9ff82a97 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterEntry.java @@ -22,8 +22,10 @@ import java.util.Collection; import java.util.Iterator; import java.util.List; +import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.SmackException.NotConnectedException; +import org.jivesoftware.smack.XMPPException.XMPPErrorException; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.roster.packet.RosterPacket; @@ -36,7 +38,11 @@ import org.jivesoftware.smack.roster.packet.RosterPacket; */ public class RosterEntry { - private String user; + /** + * The JID of the entity/user. + */ + private final String user; + private String name; private RosterPacket.ItemType type; private RosterPacket.ItemStatus status; @@ -85,17 +91,22 @@ public class RosterEntry { * * @param name the name. * @throws NotConnectedException + * @throws XMPPErrorException + * @throws NoResponseException */ - public void setName(String name) throws NotConnectedException { + public void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException { // Do nothing if the name hasn't changed. if (name != null && name.equals(this.name)) { return; } - this.name = name; + RosterPacket packet = new RosterPacket(); packet.setType(IQ.Type.set); packet.addRosterItem(toRosterItem(this)); - connection.sendPacket(packet); + connection.createPacketCollectorAndSend(packet).nextResultOrThrow(); + + // We have received a result response to the IQ set, the name was successfully changed + this.name = name; } /** diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java index 502be1b12..f80f472d9 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/RosterGroup.java @@ -40,8 +40,8 @@ import org.jxmpp.util.XmppStringUtils; */ public class RosterGroup { - private String name; - private XMPPConnection connection; + private final String name; + private final XMPPConnection connection; private final Set entries; /** @@ -73,8 +73,10 @@ public class RosterGroup { * * @param name the name of the group. * @throws NotConnectedException + * @throws XMPPErrorException + * @throws NoResponseException */ - public void setName(String name) throws NotConnectedException { + public void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException { synchronized (entries) { for (RosterEntry entry : entries) { RosterPacket packet = new RosterPacket(); @@ -83,7 +85,7 @@ public class RosterGroup { item.removeGroupName(this.name); item.addGroupName(name); packet.addRosterItem(item); - connection.sendPacket(packet); + connection.createPacketCollectorAndSend(packet).nextResultOrThrow(); } } }