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.
This commit is contained in:
Florian Schmaus 2015-01-26 16:24:32 +01:00
parent 763139684b
commit 603f64166b
3 changed files with 22 additions and 11 deletions

View File

@ -944,9 +944,7 @@ public class Roster extends Manager {
unfiledEntries.remove(entry); unfiledEntries.remove(entry);
} }
// Add the user to the new groups // Add the entry/user to the groups
// Add the entry to the groups
List<String> newGroupNames = new ArrayList<String>(); List<String> newGroupNames = new ArrayList<String>();
for (String groupName : item.getGroupNames()) { for (String groupName : item.getGroupNames()) {
// Add the group name to the list. // Add the group name to the list.

View File

@ -22,8 +22,10 @@ import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.roster.packet.RosterPacket; import org.jivesoftware.smack.roster.packet.RosterPacket;
@ -36,7 +38,11 @@ import org.jivesoftware.smack.roster.packet.RosterPacket;
*/ */
public class RosterEntry { public class RosterEntry {
private String user; /**
* The JID of the entity/user.
*/
private final String user;
private String name; private String name;
private RosterPacket.ItemType type; private RosterPacket.ItemType type;
private RosterPacket.ItemStatus status; private RosterPacket.ItemStatus status;
@ -85,17 +91,22 @@ public class RosterEntry {
* *
* @param name the name. * @param name the name.
* @throws NotConnectedException * @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. // Do nothing if the name hasn't changed.
if (name != null && name.equals(this.name)) { if (name != null && name.equals(this.name)) {
return; return;
} }
this.name = name;
RosterPacket packet = new RosterPacket(); RosterPacket packet = new RosterPacket();
packet.setType(IQ.Type.set); packet.setType(IQ.Type.set);
packet.addRosterItem(toRosterItem(this)); 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;
} }
/** /**

View File

@ -40,8 +40,8 @@ import org.jxmpp.util.XmppStringUtils;
*/ */
public class RosterGroup { public class RosterGroup {
private String name; private final String name;
private XMPPConnection connection; private final XMPPConnection connection;
private final Set<RosterEntry> entries; private final Set<RosterEntry> entries;
/** /**
@ -73,8 +73,10 @@ public class RosterGroup {
* *
* @param name the name of the group. * @param name the name of the group.
* @throws NotConnectedException * @throws NotConnectedException
* @throws XMPPErrorException
* @throws NoResponseException
*/ */
public void setName(String name) throws NotConnectedException { public void setName(String name) throws NotConnectedException, NoResponseException, XMPPErrorException {
synchronized (entries) { synchronized (entries) {
for (RosterEntry entry : entries) { for (RosterEntry entry : entries) {
RosterPacket packet = new RosterPacket(); RosterPacket packet = new RosterPacket();
@ -83,7 +85,7 @@ public class RosterGroup {
item.removeGroupName(this.name); item.removeGroupName(this.name);
item.addGroupName(name); item.addGroupName(name);
packet.addRosterItem(item); packet.addRosterItem(item);
connection.sendPacket(packet); connection.createPacketCollectorAndSend(packet).nextResultOrThrow();
} }
} }
} }