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.muc;
019
020import org.jivesoftware.smackx.muc.packet.MUCAdmin;
021import org.jivesoftware.smackx.muc.packet.MUCUser;
022import org.jivesoftware.smack.packet.Presence;
023import org.jivesoftware.smack.util.StringUtils;
024
025/**
026 * Represents the information about an occupant in a given room. The information will always have
027 * the affiliation and role of the occupant in the room. The full JID and nickname are optional.
028 *
029 * @author Gaston Dombiak
030 */
031public class Occupant {
032    // Fields that must have a value
033    private String affiliation;
034    private String role;
035    // Fields that may have a value
036    private String jid;
037    private String nick;
038
039    Occupant(MUCAdmin.Item item) {
040        super();
041        this.jid = item.getJid();
042        this.affiliation = item.getAffiliation();
043        this.role = item.getRole();
044        this.nick = item.getNick();
045    }
046
047    Occupant(Presence presence) {
048        super();
049        MUCUser mucUser = (MUCUser) presence.getExtension("x",
050                "http://jabber.org/protocol/muc#user");
051        MUCUser.Item item = mucUser.getItem();
052        this.jid = item.getJid();
053        this.affiliation = item.getAffiliation();
054        this.role = item.getRole();
055        // Get the nickname from the FROM attribute of the presence
056        this.nick = StringUtils.parseResource(presence.getFrom());
057    }
058
059    /**
060     * Returns the full JID of the occupant. If this information was extracted from a presence and
061     * the room is semi or full-anonymous then the answer will be null. On the other hand, if this
062     * information was obtained while maintaining the voice list or the moderator list then we will
063     * always have a full JID.
064     *
065     * @return the full JID of the occupant.
066     */
067    public String getJid() {
068        return jid;
069    }
070
071    /**
072     * Returns the affiliation of the occupant. Possible affiliations are: "owner", "admin",
073     * "member", "outcast". This information will always be available.
074     *
075     * @return the affiliation of the occupant.
076     */
077    public String getAffiliation() {
078        return affiliation;
079    }
080
081    /**
082     * Returns the current role of the occupant in the room. This information will always be
083     * available.
084     *
085     * @return the current role of the occupant in the room.
086     */
087    public String getRole() {
088        return role;
089    }
090
091    /**
092     * Returns the current nickname of the occupant in the room. If this information was extracted
093     * from a presence then the answer will be null.
094     *
095     * @return the current nickname of the occupant in the room or null if this information was
096     *         obtained from a presence.
097     */
098    public String getNick() {
099        return nick;
100    }
101
102    public boolean equals(Object obj) {
103        if(!(obj instanceof Occupant)) {
104            return false;
105        }
106        Occupant occupant = (Occupant)obj;
107        return jid.equals(occupant.jid);
108    }
109
110    public int hashCode() {
111        int result;
112        result = affiliation.hashCode();
113        result = 17 * result + role.hashCode();
114        result = 17 * result + jid.hashCode();
115        result = 17 * result + (nick != null ? nick.hashCode() : 0);
116        return result;
117    }
118}