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