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