001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smackx.xroster;
019
020import java.util.ArrayList;
021import java.util.Arrays;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025
026import org.jxmpp.jid.Jid;
027
028/**
029 * Represents a roster item, which consists of a JID and , their name and
030 * the groups the roster item belongs to. This roster item does not belong
031 * to the local roster. Therefore, it does not persist in the server.<p>
032 *
033 * The idea of a RemoteRosterEntry is to be used as part of a roster exchange.
034 *
035 * @author Gaston Dombiak
036 */
037public class RemoteRosterEntry {
038
039    private final Jid user;
040    private final String name;
041    private final List<String> groupNames = new ArrayList<>();
042
043    /**
044     * Creates a new remote roster entry.
045     *
046     * @param user the user.
047     * @param name the user's name.
048     * @param groups the list of group names the entry will belong to, or <code>null</code> if the
049     *      the roster entry won't belong to a group.
050     */
051    public RemoteRosterEntry(Jid user, String name, String[] groups) {
052        this.user = user;
053        this.name = name;
054        if (groups != null) {
055            groupNames.addAll(Arrays.asList(groups));
056        }
057    }
058
059    /**
060     * Returns the user.
061     *
062     * @return the user.
063     */
064    public Jid getUser() {
065        return user;
066    }
067
068    /**
069     * Returns the user's name.
070     *
071     * @return the user's name.
072     */
073    public String getName() {
074        return name;
075    }
076
077    /**
078     * Returns an Iterator for the group names (as Strings) that the roster entry
079     * belongs to.
080     *
081     * @return an Iterator for the group names.
082     */
083    public Iterator<String> getGroupNames() {
084        synchronized (groupNames) {
085            return Collections.unmodifiableList(groupNames).iterator();
086        }
087    }
088
089    /**
090     * Returns a String array for the group names that the roster entry
091     * belongs to.
092     *
093     * @return a String[] for the group names.
094     */
095    public String[] getGroupArrayNames() {
096        synchronized (groupNames) {
097            return Collections.unmodifiableList(groupNames).toArray(new String[groupNames.size()]);
098        }
099    }
100
101    public String toXML() {
102        StringBuilder buf = new StringBuilder();
103        buf.append("<item jid=\"").append(user).append('"');
104        if (name != null) {
105            buf.append(" name=\"").append(name).append('"');
106        }
107        buf.append('>');
108        synchronized (groupNames) {
109            for (String groupName : groupNames) {
110                buf.append("<group>").append(groupName).append("</group>");
111            }
112        }
113        buf.append("</item>");
114        return buf.toString();
115    }
116
117}