RemoteRosterEntry.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.xroster;

  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;

  23. import org.jxmpp.jid.Jid;

  24. /**
  25.  * Represents a roster item, which consists of a JID and , their name and
  26.  * the groups the roster item belongs to. This roster item does not belong
  27.  * to the local roster. Therefore, it does not persist in the server.<p>
  28.  *
  29.  * The idea of a RemoteRosterEntry is to be used as part of a roster exchange.
  30.  *
  31.  * @author Gaston Dombiak
  32.  */
  33. public class RemoteRosterEntry {

  34.     private final Jid user;
  35.     private final String name;
  36.     private final List<String> groupNames = new ArrayList<>();

  37.     /**
  38.      * Creates a new remote roster entry.
  39.      *
  40.      * @param user the user.
  41.      * @param name the user's name.
  42.      * @param groups the list of group names the entry will belong to, or <code>null</code> if the
  43.      *      the roster entry won't belong to a group.
  44.      */
  45.     public RemoteRosterEntry(Jid user, String name, String[] groups) {
  46.         this.user = user;
  47.         this.name = name;
  48.         if (groups != null) {
  49.             groupNames.addAll(Arrays.asList(groups));
  50.         }
  51.     }

  52.     /**
  53.      * Returns the user.
  54.      *
  55.      * @return the user.
  56.      */
  57.     public Jid getUser() {
  58.         return user;
  59.     }

  60.     /**
  61.      * Returns the user's name.
  62.      *
  63.      * @return the user's name.
  64.      */
  65.     public String getName() {
  66.         return name;
  67.     }

  68.     /**
  69.      * Returns an Iterator for the group names (as Strings) that the roster entry
  70.      * belongs to.
  71.      *
  72.      * @return an Iterator for the group names.
  73.      */
  74.     public Iterator<String> getGroupNames() {
  75.         synchronized (groupNames) {
  76.             return Collections.unmodifiableList(groupNames).iterator();
  77.         }
  78.     }

  79.     /**
  80.      * Returns a String array for the group names that the roster entry
  81.      * belongs to.
  82.      *
  83.      * @return a String[] for the group names.
  84.      */
  85.     public String[] getGroupArrayNames() {
  86.         synchronized (groupNames) {
  87.             return Collections.unmodifiableList(groupNames).toArray(new String[groupNames.size()]);
  88.         }
  89.     }

  90.     public String toXML() {
  91.         StringBuilder buf = new StringBuilder();
  92.         buf.append("<item jid=\"").append(user).append('"');
  93.         if (name != null) {
  94.             buf.append(" name=\"").append(name).append('"');
  95.         }
  96.         buf.append('>');
  97.         synchronized (groupNames) {
  98.             for (String groupName : groupNames) {
  99.                 buf.append("<group>").append(groupName).append("</group>");
  100.             }
  101.         }
  102.         buf.append("</item>");
  103.         return buf.toString();
  104.     }

  105. }