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;
024import org.jivesoftware.smackx.blocking.BlockingCommandManager;
025import org.jxmpp.jid.Jid;
026
027/**
028 * Block list IQ class.
029 * 
030 * @author Fernando Ramirez
031 * @see <a href="http://xmpp.org/extensions/xep-0191.html">XEP-0191: Blocking
032 *      Command</a>
033 */
034public class BlockListIQ extends IQ {
035
036    /**
037     * block list element.
038     */
039    public static final String ELEMENT = "blocklist";
040
041    /**
042     * the IQ NAMESPACE.
043     */
044    public static final String NAMESPACE = BlockingCommandManager.NAMESPACE;
045
046    private final List<Jid> jids;
047
048    /**
049     * Block list IQ constructor.
050     * 
051     * @param jids
052     */
053    public BlockListIQ(List<Jid> jids) {
054        super(ELEMENT, NAMESPACE);
055        if (jids == null) {
056            this.jids = Collections.emptyList();
057        } else {
058            this.jids = Collections.unmodifiableList(jids);
059        }
060    }
061
062    /**
063     * Block list IQ constructor.
064     */
065    public BlockListIQ() {
066        this(null);
067    }
068
069    /**
070     * Get the JIDs as unmodifiable list.
071     * 
072     * @return the blocked JIDs
073     */
074    public List<Jid> getBlockedJids() {
075        return jids;
076    }
077
078    /**
079     * Get a copy of the blocked list JIDs. This copy is modifiable.
080     *
081     * @return the blocked JIDs
082     */
083    public List<Jid> getBlockedJidsCopy() {
084        return new ArrayList<>(getBlockedJids());
085    }
086
087    @Override
088    protected IQChildElementXmlStringBuilder getIQChildElementBuilder(IQChildElementXmlStringBuilder xml) {
089        if (jids.isEmpty()) {
090            xml.setEmptyElement();
091        } else {
092            xml.rightAngleBracket();
093
094            for (Jid jid : jids) {
095                xml.halfOpenElement("item");
096                xml.attribute("jid", jid);
097                xml.closeEmptyElement();
098            }
099        }
100
101        return xml;
102    }
103
104}