Home | History | Annotate | Download | only in smackx
      1 /**
      2  * $RCSfile$
      3  * $Revision$
      4  * $Date$
      5  *
      6  * Copyright 2003-2007 Jive Software.
      7  *
      8  * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
      9  * you may not use this file except in compliance with the License.
     10  * You may obtain a copy of the License at
     11  *
     12  *     http://www.apache.org/licenses/LICENSE-2.0
     13  *
     14  * Unless required by applicable law or agreed to in writing, software
     15  * distributed under the License is distributed on an "AS IS" BASIS,
     16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     17  * See the License for the specific language governing permissions and
     18  * limitations under the License.
     19  */
     20 
     21 package org.jivesoftware.smackx;
     22 
     23 import java.util.*;
     24 
     25 /**
     26  * Represents a roster item, which consists of a JID and , their name and
     27  * the groups the roster item belongs to. This roster item does not belong
     28  * to the local roster. Therefore, it does not persist in the server.<p>
     29  *
     30  * The idea of a RemoteRosterEntry is to be used as part of a roster exchange.
     31  *
     32  * @author Gaston Dombiak
     33  */
     34 public class RemoteRosterEntry {
     35 
     36     private String user;
     37     private String name;
     38     private final List<String> groupNames = new ArrayList<String>();
     39 
     40     /**
     41      * Creates a new remote roster entry.
     42      *
     43      * @param user the user.
     44      * @param name the user's name.
     45      * @param groups the list of group names the entry will belong to, or <tt>null</tt> if the
     46      *      the roster entry won't belong to a group.
     47      */
     48     public RemoteRosterEntry(String user, String name, String [] groups) {
     49         this.user = user;
     50         this.name = name;
     51 		if (groups != null) {
     52             groupNames.addAll(Arrays.asList(groups));
     53 		}
     54     }
     55 
     56     /**
     57      * Returns the user.
     58      *
     59      * @return the user.
     60      */
     61     public String getUser() {
     62         return user;
     63     }
     64 
     65     /**
     66      * Returns the user's name.
     67      *
     68      * @return the user's name.
     69      */
     70     public String getName() {
     71         return name;
     72     }
     73 
     74     /**
     75      * Returns an Iterator for the group names (as Strings) that the roster entry
     76      * belongs to.
     77      *
     78      * @return an Iterator for the group names.
     79      */
     80     public Iterator<String> getGroupNames() {
     81         synchronized (groupNames) {
     82             return Collections.unmodifiableList(groupNames).iterator();
     83         }
     84     }
     85 
     86     /**
     87      * Returns a String array for the group names that the roster entry
     88      * belongs to.
     89      *
     90      * @return a String[] for the group names.
     91      */
     92     public String[] getGroupArrayNames() {
     93         synchronized (groupNames) {
     94             return Collections.unmodifiableList(groupNames).toArray(new String[groupNames.size()]);
     95         }
     96     }
     97 
     98     public String toXML() {
     99         StringBuilder buf = new StringBuilder();
    100         buf.append("<item jid=\"").append(user).append("\"");
    101         if (name != null) {
    102             buf.append(" name=\"").append(name).append("\"");
    103         }
    104         buf.append(">");
    105         synchronized (groupNames) {
    106             for (String groupName : groupNames) {
    107                 buf.append("<group>").append(groupName).append("</group>");
    108             }
    109         }
    110         buf.append("</item>");
    111         return buf.toString();
    112     }
    113 
    114 }
    115