Occupant.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.muc;

  18. import java.util.logging.Logger;

  19. import org.jivesoftware.smack.packet.Presence;

  20. import org.jivesoftware.smackx.muc.packet.MUCItem;
  21. import org.jivesoftware.smackx.muc.packet.MUCUser;

  22. import org.jxmpp.jid.EntityFullJid;
  23. import org.jxmpp.jid.Jid;
  24. import org.jxmpp.jid.parts.Resourcepart;

  25. /**
  26.  * Represents the information about an occupant in a given room. The information will always have
  27.  * the affiliation and role of the occupant in the room. The full JID and nickname are optional.
  28.  *
  29.  * @author Gaston Dombiak
  30.  */
  31. public class Occupant {

  32.     private static final Logger LOGGER = Logger.getLogger(Occupant.class.getName());

  33.     // Fields that must have a value
  34.     private final MUCAffiliation affiliation;
  35.     private final MUCRole role;
  36.     // Fields that may have a value
  37.     private final Jid jid;
  38.     private final Resourcepart nick;

  39.     Occupant(MUCItem item) {
  40.         this.jid = item.getJid();
  41.         this.affiliation = item.getAffiliation();
  42.         this.role = item.getRole();
  43.         this.nick = item.getNick();
  44.     }

  45.     Occupant(Presence presence) {
  46.         MUCUser mucUser = (MUCUser) presence.getExtensionElement("x",
  47.                 "http://jabber.org/protocol/muc#user");
  48.         MUCItem item = mucUser.getItem();
  49.         this.jid = item.getJid();
  50.         this.affiliation = item.getAffiliation();
  51.         this.role = item.getRole();
  52.         // Get the nickname from the FROM attribute of the presence
  53.         EntityFullJid from = presence.getFrom().asEntityFullJidIfPossible();
  54.         if (from == null) {
  55.             LOGGER.warning("Occupant presence without resource: " + presence.getFrom());
  56.             this.nick = null;
  57.         } else {
  58.             this.nick = from.getResourcepart();
  59.         }
  60.     }

  61.     /**
  62.      * Returns the full JID of the occupant. If this information was extracted from a presence and
  63.      * the room is semi or full-anonymous then the answer will be null. On the other hand, if this
  64.      * information was obtained while maintaining the voice list or the moderator list then we will
  65.      * always have a full JID.
  66.      *
  67.      * @return the full JID of the occupant.
  68.      */
  69.     public Jid getJid() {
  70.         return jid;
  71.     }

  72.     /**
  73.      * Returns the affiliation of the occupant. Possible affiliations are: "owner", "admin",
  74.      * "member", "outcast". This information will always be available.
  75.      *
  76.      * @return the affiliation of the occupant.
  77.      */
  78.     public MUCAffiliation getAffiliation() {
  79.         return affiliation;
  80.     }

  81.     /**
  82.      * Returns the current role of the occupant in the room. This information will always be
  83.      * available.
  84.      *
  85.      * @return the current role of the occupant in the room.
  86.      */
  87.     public MUCRole getRole() {
  88.         return role;
  89.     }

  90.     /**
  91.      * Returns the current nickname of the occupant in the room. If this information was extracted
  92.      * from a presence then the answer will be null.
  93.      *
  94.      * @return the current nickname of the occupant in the room or null if this information was
  95.      *         obtained from a presence.
  96.      */
  97.     public Resourcepart getNick() {
  98.         return nick;
  99.     }

  100.     @Override
  101.     public boolean equals(Object obj) {
  102.         if (!(obj instanceof Occupant)) {
  103.             return false;
  104.         }
  105.         Occupant occupant = (Occupant) obj;
  106.         return jid.equals(occupant.jid);
  107.     }

  108.     @Override
  109.     public int hashCode() {
  110.         int result;
  111.         result = affiliation.hashCode();
  112.         result = 17 * result + role.hashCode();
  113.         result = 17 * result + (jid != null ? jid.hashCode() : 0);
  114.         result = 17 * result + (nick != null ? nick.hashCode() : 0);
  115.         return result;
  116.     }
  117. }