1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

Return Collections (or sublcasses) instead of Iterators

This allows us to exploid Java 8 streams and Java 5 for-each
loops. The returned Collections are usually unmodifiable. We decided
against returning Iterable because this would mean determining the
size in O(n) compared to Collection.size() which is often faster
(e.g. O(1)).
This commit is contained in:
Florian Schmaus 2014-04-09 08:26:28 +02:00
parent 6ea1d65e73
commit 4cff008708
31 changed files with 219 additions and 282 deletions

View file

@ -22,7 +22,6 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@ -583,17 +582,18 @@ public class Roster {
* updates.
*
* @param user a XMPP ID, e.g. jdoe@example.com.
* @return an iterator (of Presence objects) for all the user's current presences,
* @return an Collection (of Presence objects) for all the user's current presences,
* or an unavailable presence if the user is offline or if no presence information
* is available.
*/
public Iterator<Presence> getPresences(String user) {
public Collection<Presence> getPresences(String user) {
Collection<Presence> res;
String key = getPresenceMapKey(user);
Map<String, Presence> userPresences = presenceMap.get(key);
if (userPresences == null) {
Presence presence = new Presence(Presence.Type.unavailable);
presence.setFrom(user);
return Arrays.asList(presence).iterator();
res = Arrays.asList(presence);
}
else {
Collection<Presence> answer = new ArrayList<Presence>();
@ -603,14 +603,15 @@ public class Roster {
}
}
if (!answer.isEmpty()) {
return answer.iterator();
res = answer;
}
else {
Presence presence = new Presence(Presence.Type.unavailable);
presence.setFrom(user);
return Arrays.asList(presence).iterator();
res = Arrays.asList(presence);
}
}
return Collections.unmodifiableCollection(res);
}
/**

View file

@ -190,11 +190,11 @@ public class XMPPError {
}
/**
* Returns an Iterator for the error extensions attached to the xmppError.
* Returns a List of the error extensions attached to the xmppError.
* An application MAY provide application-specific error information by including a
* properly-namespaced child in the error element.
*
* @return an Iterator for the error extensions.
* @return a List of the error extensions.
*/
public synchronized List<PacketExtension> getExtensions() {
if (applicationExtensions == null) {