From 252d5172e923afac74cf35275b6fdb9c02ea17cc Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Sat, 29 Nov 2014 13:36:56 +0100 Subject: [PATCH] =?UTF-8?q?Return=20more=20specific=20types=20(e.g.=20Coll?= =?UTF-8?q?ection=20=E2=86=92=20List)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit be generic as possible in what you accept, but more specific in what you return. Also tweak MultiuserChatManager methods a bit. --- .../main/java/org/jivesoftware/smack/Chat.java | 9 ++++----- .../org/jivesoftware/smack/ChatManager.java | 7 +++---- .../java/org/jivesoftware/smack/Roster.java | 8 ++++---- .../org/jivesoftware/smack/RosterEntry.java | 7 +++---- .../org/jivesoftware/smack/RosterGroup.java | 9 ++++----- .../org/jivesoftware/smack/packet/Message.java | 17 ++++++++--------- .../jivesoftware/smack/packet/RosterPacket.java | 9 ++++----- .../smackx/amp/packet/AMPExtension.java | 11 +++++------ .../smackx/bookmarks/BookmarkManager.java | 9 ++++----- .../smackx/iqregister/AccountManager.java | 4 ++-- .../org/jivesoftware/smackx/muc/HostedRoom.java | 5 ++--- .../smackx/muc/MultiUserChatManager.java | 17 +++++++++-------- 12 files changed, 52 insertions(+), 60 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/Chat.java b/smack-core/src/main/java/org/jivesoftware/smack/Chat.java index c1f1a07ef..856c5a0fb 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/Chat.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/Chat.java @@ -21,7 +21,6 @@ import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.packet.Message; import java.util.Set; -import java.util.Collection; import java.util.Collections; import java.util.concurrent.CopyOnWriteArraySet; @@ -141,12 +140,12 @@ public class Chat { } /** - * Returns an unmodifiable collection of all of the listeners registered with this chat. + * Returns an unmodifiable set of all of the listeners registered with this chat. * - * @return an unmodifiable collection of all of the listeners registered with this chat. + * @return an unmodifiable set of all of the listeners registered with this chat. */ - public Collection getListeners() { - return Collections.unmodifiableCollection(listeners); + public Set getListeners() { + return Collections.unmodifiableSet(listeners); } /** diff --git a/smack-core/src/main/java/org/jivesoftware/smack/ChatManager.java b/smack-core/src/main/java/org/jivesoftware/smack/ChatManager.java index c568ed191..55b7d91a4 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/ChatManager.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/ChatManager.java @@ -17,7 +17,6 @@ package org.jivesoftware.smack; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -322,14 +321,14 @@ public class ChatManager extends Manager{ } /** - * Returns an unmodifiable collection of all chat listeners currently registered with this + * Returns an unmodifiable set of all chat listeners currently registered with this * manager. * * @return an unmodifiable collection of all chat listeners currently registered with this * manager. */ - public Collection getChatListeners() { - return Collections.unmodifiableCollection(chatManagerListeners); + public Set getChatListeners() { + return Collections.unmodifiableSet(chatManagerListeners); } private void deliverMessage(Chat chat, Message message) { 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 1de0f3833..c35eae773 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/Roster.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/Roster.java @@ -369,12 +369,12 @@ public class Roster { } /** - * Returns an unmodifiable collection of all entries in the roster, including entries + * Returns a set of all entries in the roster, including entries * that don't belong to any groups. * * @return all entries in the roster. */ - public Collection getEntries() { + public Set getEntries() { Set allEntries = new HashSet(); // Loop through all roster groups and add their entries to the answer for (RosterGroup rosterGroup : getGroups()) { @@ -383,7 +383,7 @@ public class Roster { // Add the roster unfiled entries to the answer allEntries.addAll(unfiledEntries); - return Collections.unmodifiableCollection(allEntries); + return allEntries; } /** @@ -402,7 +402,7 @@ public class Roster { * * @return the unfiled roster entries. */ - public Collection getUnfiledEntries() { + public List getUnfiledEntries() { return Collections.unmodifiableList(unfiledEntries); } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/RosterEntry.java b/smack-core/src/main/java/org/jivesoftware/smack/RosterEntry.java index 4ff1077a5..f03f8bab3 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/RosterEntry.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/RosterEntry.java @@ -19,7 +19,6 @@ package org.jivesoftware.smack; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -112,11 +111,11 @@ public class RosterEntry { } /** - * Returns an unmodifiable collection of the roster groups that this entry belongs to. + * Returns an copied list of the roster groups that this entry belongs to. * * @return an iterator for the groups this entry belongs to. */ - public Collection getGroups() { + public List getGroups() { List results = new ArrayList(); // Loop through all roster groups and find the ones that contain this // entry. This algorithm should be fine @@ -125,7 +124,7 @@ public class RosterEntry { results.add(group); } } - return Collections.unmodifiableCollection(results); + return results; } /** diff --git a/smack-core/src/main/java/org/jivesoftware/smack/RosterGroup.java b/smack-core/src/main/java/org/jivesoftware/smack/RosterGroup.java index 5b76ec242..2fae4c36e 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/RosterGroup.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/RosterGroup.java @@ -18,9 +18,8 @@ package org.jivesoftware.smack; import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; import java.util.LinkedHashSet; +import java.util.List; import java.util.Locale; import java.util.Set; @@ -99,13 +98,13 @@ public class RosterGroup { } /** - * Returns an unmodifiable collection of all entries in the group. + * Returns an copied list of all entries in the group. * * @return all entries in the group. */ - public Collection getEntries() { + public List getEntries() { synchronized (entries) { - return Collections.unmodifiableList(new ArrayList(entries)); + return new ArrayList(entries); } } diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java index 6a81e4e21..13c41f54d 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/Message.java @@ -18,7 +18,6 @@ package org.jivesoftware.smack.packet; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -154,8 +153,8 @@ public final class Message extends Packet { * * @return a collection of all subjects in this message. */ - public Collection getSubjects() { - return Collections.unmodifiableCollection(subjects); + public Set getSubjects() { + return Collections.unmodifiableSet(subjects); } /** @@ -218,7 +217,7 @@ public final class Message extends Packet { * * @return the languages being used for the subjects. */ - public Collection getSubjectLanguages() { + public List getSubjectLanguages() { Subject defaultSubject = getMessageSubject(null); List languages = new ArrayList(); for (Subject subject : subjects) { @@ -226,7 +225,7 @@ public final class Message extends Packet { languages.add(subject.language); } } - return Collections.unmodifiableCollection(languages); + return Collections.unmodifiableList(languages); } /** @@ -274,8 +273,8 @@ public final class Message extends Packet { * @return a collection of all bodies in this Message. * @since 3.0.2 */ - public Collection getBodies() { - return Collections.unmodifiableCollection(bodies); + public Set getBodies() { + return Collections.unmodifiableSet(bodies); } /** @@ -340,7 +339,7 @@ public final class Message extends Packet { * @return the languages being used for the bodies. * @since 3.0.2 */ - public Collection getBodyLanguages() { + public List getBodyLanguages() { Body defaultBody = getMessageBody(null); List languages = new ArrayList(); for (Body body : bodies) { @@ -348,7 +347,7 @@ public final class Message extends Packet { languages.add(body.language); } } - return Collections.unmodifiableCollection(languages); + return Collections.unmodifiableList(languages); } /** diff --git a/smack-core/src/main/java/org/jivesoftware/smack/packet/RosterPacket.java b/smack-core/src/main/java/org/jivesoftware/smack/packet/RosterPacket.java index 470b603e7..c1408a880 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/packet/RosterPacket.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/packet/RosterPacket.java @@ -20,7 +20,6 @@ package org.jivesoftware.smack.packet; import org.jivesoftware.smack.util.XmlStringBuilder; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Locale; @@ -67,13 +66,13 @@ public class RosterPacket extends IQ { } /** - * Returns an unmodifiable collection for the roster items in the packet. + * Returns a copied list of the roster items in the packet. * - * @return an unmodifiable collection for the roster items in the packet. + * @return a copied list of the roster items in the packet. */ - public Collection getRosterItems() { + public List getRosterItems() { synchronized (rosterItems) { - return Collections.unmodifiableList(new ArrayList(rosterItems)); + return new ArrayList(rosterItems); } } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/amp/packet/AMPExtension.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/amp/packet/AMPExtension.java index c94de48a2..44dddd566 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/amp/packet/AMPExtension.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/amp/packet/AMPExtension.java @@ -16,9 +16,8 @@ */ package org.jivesoftware.smackx.amp.packet; -import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import org.jivesoftware.smack.packet.PacketExtension; @@ -82,12 +81,12 @@ public class AMPExtension implements PacketExtension { } /** - * Returns a Collection of the rules in the packet. + * Returns a unmodifiable List of the rules in the packet. * - * @return a Collection of the rules in the packet. + * @return a unmodifiable List of the rules in the packet. */ - public Collection getRules() { - return Collections.unmodifiableList(new ArrayList(rules)); + public List getRules() { + return Collections.unmodifiableList(rules); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/BookmarkManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/BookmarkManager.java index 4c3303503..ecfb4e73f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/BookmarkManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/bookmarks/BookmarkManager.java @@ -17,7 +17,6 @@ package org.jivesoftware.smackx.bookmarks; -import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -95,9 +94,9 @@ public class BookmarkManager { * @throws NotConnectedException * @see BookmarkedConference */ - public Collection getBookmarkedConferences() throws NoResponseException, XMPPErrorException, NotConnectedException { + public List getBookmarkedConferences() throws NoResponseException, XMPPErrorException, NotConnectedException { retrieveBookmarks(); - return Collections.unmodifiableCollection(bookmarks.getBookmarkedConferences()); + return Collections.unmodifiableList(bookmarks.getBookmarkedConferences()); } /** @@ -171,9 +170,9 @@ public class BookmarkManager { * @throws NoResponseException if there was no response from the server. * @throws NotConnectedException */ - public Collection getBookmarkedURLs() throws NoResponseException, XMPPErrorException, NotConnectedException { + public List getBookmarkedURLs() throws NoResponseException, XMPPErrorException, NotConnectedException { retrieveBookmarks(); - return Collections.unmodifiableCollection(bookmarks.getBookmarkedURLS()); + return Collections.unmodifiableList(bookmarks.getBookmarkedURLS()); } /** diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/AccountManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/AccountManager.java index 1111731c1..8d611e18f 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/AccountManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/iqregister/AccountManager.java @@ -17,10 +17,10 @@ package org.jivesoftware.smackx.iqregister; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.WeakHashMap; import org.jivesoftware.smack.Manager; @@ -141,7 +141,7 @@ public class AccountManager extends Manager { * @throws NoResponseException * @throws NotConnectedException */ - public Collection getAccountAttributes() throws NoResponseException, XMPPErrorException, NotConnectedException { + public Set getAccountAttributes() throws NoResponseException, XMPPErrorException, NotConnectedException { if (info == null) { getRegistrationInfo(); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/HostedRoom.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/HostedRoom.java index fa5533293..0c15b352d 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/HostedRoom.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/HostedRoom.java @@ -31,12 +31,11 @@ import org.jivesoftware.smackx.disco.packet.DiscoverItems; */ public class HostedRoom { - private String jid; + private final String jid; - private String name; + private final String name; public HostedRoom(DiscoverItems.Item item) { - super(); jid = item.getEntityID(); name = item.getName(); } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java index e422201ef..1a8e0aeb8 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChatManager.java @@ -18,7 +18,6 @@ package org.jivesoftware.smackx.muc; import java.lang.ref.WeakReference; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -203,11 +202,12 @@ public class MultiUserChatManager extends Manager { */ public List getJoinedRooms(String user) throws NoResponseException, XMPPErrorException, NotConnectedException { - ArrayList answer = new ArrayList(); // Send the disco packet to the user DiscoverItems result = ServiceDiscoveryManager.getInstanceFor(connection()).discoverItems(user, DISCO_NODE); + List items = result.getItems(); + List answer = new ArrayList(items.size()); // Collect the entityID for each returned item - for (DiscoverItems.Item item : result.getItems()) { + for (DiscoverItems.Item item : items) { answer.add(item.getEntityID()); } return answer; @@ -242,7 +242,7 @@ public class MultiUserChatManager extends Manager { } /** - * Returns a collection of HostedRooms where each HostedRoom has the XMPP address of the room and the room's name. + * Returns a List of HostedRooms where each HostedRoom has the XMPP address of the room and the room's name. * Once discovered the rooms hosted by a chat service it is possible to discover more detailed room information or * join the room. * @@ -252,12 +252,13 @@ public class MultiUserChatManager extends Manager { * @throws NoResponseException * @throws NotConnectedException */ - public Collection getHostedRooms(String serviceName) throws NoResponseException, XMPPErrorException, + public List getHostedRooms(String serviceName) throws NoResponseException, XMPPErrorException, NotConnectedException { - List answer = new ArrayList(); ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection()); - DiscoverItems items = discoManager.discoverItems(serviceName); - for (DiscoverItems.Item item : items.getItems()) { + DiscoverItems discoverItems = discoManager.discoverItems(serviceName); + List items = discoverItems.getItems(); + List answer = new ArrayList(items.size()); + for (DiscoverItems.Item item : items) { answer.add(new HostedRoom(item)); } return answer;