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