blob: 3b199a593627651417dd425026826d0d7c66d2b0 [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001/**
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
21package org.jivesoftware.smackx.muc;
22
23import org.jivesoftware.smackx.packet.MUCAdmin;
24import org.jivesoftware.smackx.packet.MUCUser;
25import org.jivesoftware.smack.packet.Presence;
26import org.jivesoftware.smack.util.StringUtils;
27
28/**
29 * Represents the information about an occupant in a given room. The information will always have
30 * the affiliation and role of the occupant in the room. The full JID and nickname are optional.
31 *
32 * @author Gaston Dombiak
33 */
34public class Occupant {
35 // Fields that must have a value
36 private String affiliation;
37 private String role;
38 // Fields that may have a value
39 private String jid;
40 private String nick;
41
42 Occupant(MUCAdmin.Item item) {
43 super();
44 this.jid = item.getJid();
45 this.affiliation = item.getAffiliation();
46 this.role = item.getRole();
47 this.nick = item.getNick();
48 }
49
50 Occupant(Presence presence) {
51 super();
52 MUCUser mucUser = (MUCUser) presence.getExtension("x",
53 "http://jabber.org/protocol/muc#user");
54 MUCUser.Item item = mucUser.getItem();
55 this.jid = item.getJid();
56 this.affiliation = item.getAffiliation();
57 this.role = item.getRole();
58 // Get the nickname from the FROM attribute of the presence
59 this.nick = StringUtils.parseResource(presence.getFrom());
60 }
61
62 /**
63 * Returns the full JID of the occupant. If this information was extracted from a presence and
64 * the room is semi or full-anonymous then the answer will be null. On the other hand, if this
65 * information was obtained while maintaining the voice list or the moderator list then we will
66 * always have a full JID.
67 *
68 * @return the full JID of the occupant.
69 */
70 public String getJid() {
71 return jid;
72 }
73
74 /**
75 * Returns the affiliation of the occupant. Possible affiliations are: "owner", "admin",
76 * "member", "outcast". This information will always be available.
77 *
78 * @return the affiliation of the occupant.
79 */
80 public String getAffiliation() {
81 return affiliation;
82 }
83
84 /**
85 * Returns the current role of the occupant in the room. This information will always be
86 * available.
87 *
88 * @return the current role of the occupant in the room.
89 */
90 public String getRole() {
91 return role;
92 }
93
94 /**
95 * Returns the current nickname of the occupant in the room. If this information was extracted
96 * from a presence then the answer will be null.
97 *
98 * @return the current nickname of the occupant in the room or null if this information was
99 * obtained from a presence.
100 */
101 public String getNick() {
102 return nick;
103 }
104
105 public boolean equals(Object obj) {
106 if(!(obj instanceof Occupant)) {
107 return false;
108 }
109 Occupant occupant = (Occupant)obj;
110 return jid.equals(occupant.jid);
111 }
112
113 public int hashCode() {
114 int result;
115 result = affiliation.hashCode();
116 result = 17 * result + role.hashCode();
117 result = 17 * result + jid.hashCode();
118 result = 17 * result + (nick != null ? nick.hashCode() : 0);
119 return result;
120 }
121}