KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smack > RosterEntry


1 /**
2  * $RCSfile$
3  * $Revision: 2408 $
4  * $Date: 2004-11-02 20:53:30 -0300 (Tue, 02 Nov 2004) $
5  *
6  * Copyright 2003-2004 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.smack;
22
23 import org.jivesoftware.smack.packet.RosterPacket;
24 import org.jivesoftware.smack.packet.IQ;
25
26 import java.util.*;
27
28 /**
29  * Each user in your roster is represented by a roster entry, which contains the user's
30  * JID and a name or nickname you assign.
31  *
32  * @author Matt Tucker
33  */

34 public class RosterEntry {
35
36     private String JavaDoc user;
37     private String JavaDoc name;
38     private RosterPacket.ItemType type;
39     private XMPPConnection connection;
40
41     /**
42      * Creates a new roster entry.
43      *
44      * @param user the user.
45      * @param name the nickname for the entry.
46      * @param type the subscription type.
47      * @param connection a connection to the XMPP server.
48      */

49     RosterEntry(String JavaDoc user, String JavaDoc name, RosterPacket.ItemType type, XMPPConnection connection) {
50         this.user = user;
51         this.name = name;
52         this.type = type;
53         this.connection = connection;
54     }
55
56     /**
57      * Returns the JID of the user associated with this entry.
58      *
59      * @return the user associated with this entry.
60      */

61     public String JavaDoc getUser() {
62         return user;
63     }
64
65     /**
66      * Returns the name associated with this entry.
67      *
68      * @return the name.
69      */

70     public String JavaDoc getName() {
71         return name;
72     }
73
74     /**
75      * Sets the name associated with this entry.
76      *
77      * @param name the name.
78      */

79     public void setName(String JavaDoc name) {
80         // Do nothing if the name hasn't changed.
81
if (name != null && name.equals(this.name)) {
82             return;
83         }
84         this.name = name;
85         RosterPacket packet = new RosterPacket();
86         packet.setType(IQ.Type.SET);
87         packet.addRosterItem(toRosterItem(this));
88         connection.sendPacket(packet);
89     }
90
91     /**
92      * Updates the state of the entry with the new values.
93      *
94      * @param name the nickname for the entry.
95      * @param type the subscription type.
96      */

97     void updateState(String JavaDoc name, RosterPacket.ItemType type) {
98         this.name = name;
99         this.type = type;
100     }
101
102     /**
103      * Returns an iterator for all the roster groups that this entry belongs to.
104      *
105      * @return an iterator for the groups this entry belongs to.
106      */

107     public Iterator getGroups() {
108         List results = new ArrayList();
109         // Loop through all roster groups and find the ones that contain this
110
// entry. This algorithm should be fine
111
for (Iterator i=connection.roster.getGroups(); i.hasNext(); ) {
112             RosterGroup group = (RosterGroup)i.next();
113             if (group.contains(this)) {
114                 results.add(group);
115             }
116         }
117         return results.iterator();
118     }
119
120     /**
121      * Returns the roster subscription type of the entry. When the type is
122      * RosterPacket.ItemType.NONE, the subscription request is pending.
123      *
124      * @return the type.
125      */

126     public RosterPacket.ItemType getType() {
127         return type;
128     }
129
130     public String JavaDoc toString() {
131         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
132         if (name != null) {
133             buf.append(name).append(": ");
134         }
135         buf.append(user);
136         Iterator groups = getGroups();
137         if (groups.hasNext()) {
138             buf.append(" [");
139             RosterGroup group = (RosterGroup)groups.next();
140             buf.append(group.getName());
141             while (groups.hasNext()) {
142             buf.append(", ");
143                 group = (RosterGroup)groups.next();
144                 buf.append(group.getName());
145             }
146             buf.append("]");
147         }
148         return buf.toString();
149     }
150
151     public boolean equals(Object JavaDoc object) {
152         if (this == object) {
153             return true;
154         }
155         if (object != null && object instanceof RosterEntry) {
156             return user.toLowerCase().equals(((RosterEntry)object).getUser().toLowerCase());
157         }
158         else {
159             return false;
160         }
161     }
162
163     static RosterPacket.Item toRosterItem(RosterEntry entry) {
164         RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), entry.getName());
165         item.setItemType(entry.getType());
166         // Set the correct group names for the item.
167
for (Iterator j=entry.getGroups(); j.hasNext(); ) {
168             RosterGroup group = (RosterGroup)j.next();
169             item.addGroupName(group.getName());
170         }
171         return item;
172     }
173 }
Popular Tags