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