MUCLightBlockingIQ.java

  1. /**
  2.  *
  3.  * Copyright 2016 Fernando Ramirez
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.muclight.element;

  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.Map;

  21. import org.jivesoftware.smack.packet.IQ;

  22. import org.jivesoftware.smackx.muclight.MultiUserChatLight;
  23. import org.jivesoftware.smackx.muclight.element.MUCLightElements.BlockingElement;

  24. import org.jxmpp.jid.Jid;

  25. /**
  26.  * MUC Light blocking IQ class.
  27.  *
  28.  * @author Fernando Ramirez
  29.  *
  30.  */
  31. public class MUCLightBlockingIQ extends IQ {

  32.     public static final String ELEMENT = QUERY_ELEMENT;
  33.     public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.BLOCKING;

  34.     private final HashMap<Jid, Boolean> rooms;
  35.     private final HashMap<Jid, Boolean> users;

  36.     /**
  37.      * MUC Light blocking IQ constructor.
  38.      *
  39.      * @param rooms TODO javadoc me please
  40.      * @param users TODO javadoc me please
  41.      */
  42.     public MUCLightBlockingIQ(HashMap<Jid, Boolean> rooms, HashMap<Jid, Boolean> users) {
  43.         super(ELEMENT, NAMESPACE);
  44.         this.rooms = rooms;
  45.         this.users = users;
  46.     }

  47.     /**
  48.      * Get rooms JIDs with booleans (true if allow, false if deny).
  49.      *
  50.      * @return the rooms JIDs with booleans (true if allow, false if deny)
  51.      */
  52.     public HashMap<Jid, Boolean> getRooms() {
  53.         return rooms;
  54.     }

  55.     /**
  56.      * Get users JIDs with booleans (true if allow, false if deny).
  57.      *
  58.      * @return the users JIDs with booleans (true if allow, false if deny)
  59.      */
  60.     public HashMap<Jid, Boolean> getUsers() {
  61.         return users;
  62.     }

  63.     @Override
  64.     protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
  65.         xml.rightAngleBracket();

  66.         if (rooms != null) {
  67.             parseBlocking(xml, rooms, true);
  68.         }

  69.         if (users != null) {
  70.             parseBlocking(xml, users, false);
  71.         }

  72.         return xml;
  73.     }

  74.     private static void parseBlocking(IQChildElementXmlStringBuilder xml, HashMap<Jid, Boolean> map, boolean isRoom) {
  75.         Iterator<Map.Entry<Jid, Boolean>> it = map.entrySet().iterator();
  76.         while (it.hasNext()) {
  77.             Map.Entry<Jid, Boolean> pair = it.next();
  78.             xml.append(new BlockingElement(pair.getKey(), pair.getValue(), isRoom));
  79.         }
  80.     }

  81. }