001/**
002 *
003 * Copyright 2016 Fernando Ramirez
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.blocking.element;
018
019import java.util.ArrayList;
020import java.util.Collections;
021import java.util.List;
022
023import org.jivesoftware.smack.packet.IQ;
024
025import org.jivesoftware.smackx.blocking.BlockingCommandManager;
026
027import org.jxmpp.jid.Jid;
028
029/**
030 * Block list IQ class.
031 *
032 * @author Fernando Ramirez
033 * @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
034 *      Command</a>
035 */
036public class BlockListIQ extends IQ {
037
038    /**
039     * block list element.
040     */
041    public static final String ELEMENT = "blocklist";
042
043    /**
044     * the IQ NAMESPACE.
045     */
046    public static final String NAMESPACE = BlockingCommandManager.NAMESPACE;
047
048    private final List<Jid> jids;
049
050    /**
051     * Block list IQ constructor.
052     *
053     * @param jids TODO javadoc me please
054     */
055    public BlockListIQ(List<Jid> jids) {
056        super(ELEMENT, NAMESPACE);
057        if (jids == null) {
058            this.jids = Collections.emptyList();
059        } else {
060            this.jids = Collections.unmodifiableList(jids);
061        }
062    }
063
064    /**
065     * Block list IQ constructor.
066     */
067    public BlockListIQ() {
068        this(null);
069    }
070
071    /**
072     * Get the JIDs as unmodifiable list.
073     *
074     * @return the blocked JIDs
075     */
076    public List<Jid> getBlockedJids() {
077        return jids;
078    }
079
080    /**
081     * Get a copy of the blocked list JIDs. This copy is modifiable.
082     *
083     * @return the blocked JIDs
084     */
085    public List<Jid> getBlockedJidsCopy() {
086        return new ArrayList<>(getBlockedJids());
087    }
088
089    @Override
090    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
091        if (jids.isEmpty()) {
092            xml.setEmptyElement();
093        } else {
094            xml.rightAngleBracket();
095
096            for (Jid jid : jids) {
097                xml.halfOpenElement("item");
098                xml.attribute("jid", jid);
099                xml.closeEmptyElement();
100            }
101        }
102
103        return xml;
104    }
105
106}