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