Improve MultiUserChat's API to query hosted rooms

Return a Map instead of a List. This makes it possible to check for
the existence of MUC by looking up the MUC's address in the Map's key
set.
This commit is contained in:
Florian Schmaus 2018-09-12 19:56:46 +02:00
parent e7372b05ea
commit 87fac888c6
1 changed files with 30 additions and 2 deletions

View File

@ -19,6 +19,7 @@ package org.jivesoftware.smackx.muc;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -392,19 +393,46 @@ public final class MultiUserChatManager extends Manager {
* @throws NotConnectedException
* @throws InterruptedException
* @throws NotAMucServiceException
* @deprecated use {@link #getRoomsHostedBy(DomainBareJid)} instead.
*/
@Deprecated
// TODO: Remove in Smack 4.4.
public List<HostedRoom> getHostedRooms(DomainBareJid serviceName) throws NoResponseException, XMPPErrorException,
NotConnectedException, InterruptedException, NotAMucServiceException {
Map<EntityBareJid, HostedRoom> hostedRooms = getRoomsHostedBy(serviceName);
return new ArrayList<>(hostedRooms.values());
}
/**
* Returns a Map 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.
*
* @param serviceName the service that is hosting the rooms to discover.
* @return a map from the room's address to its HostedRoom information.
* @throws XMPPErrorException
* @throws NoResponseException
* @throws NotConnectedException
* @throws InterruptedException
* @throws NotAMucServiceException
* @since 4.3.1
*/
public Map<EntityBareJid, HostedRoom> getRoomsHostedBy(DomainBareJid serviceName) throws NoResponseException, XMPPErrorException,
NotConnectedException, InterruptedException, NotAMucServiceException {
if (!providesMucService(serviceName)) {
throw new NotAMucServiceException(serviceName);
}
ServiceDiscoveryManager discoManager = ServiceDiscoveryManager.getInstanceFor(connection());
DiscoverItems discoverItems = discoManager.discoverItems(serviceName);
List<DiscoverItems.Item> items = discoverItems.getItems();
List<HostedRoom> answer = new ArrayList<HostedRoom>(items.size());
Map<EntityBareJid, HostedRoom> answer = new HashMap<>(items.size());
for (DiscoverItems.Item item : items) {
answer.add(new HostedRoom(item));
HostedRoom hostedRoom = new HostedRoom(item);
HostedRoom previousRoom = answer.put(hostedRoom.getJid(), hostedRoom);
assert previousRoom == null;
}
return answer;
}