alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/**
 * $RCSfile: RosterEntry.java,v $
 * $Revision: 1.1 $
 * $Date: 2005/02/25 21:41:42 $
 *
 * Copyright 2003-2004 Jive Software.
 *
 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.jivesoftware.smack;

import org.jivesoftware.smack.packet.RosterPacket;
import org.jivesoftware.smack.packet.IQ;

import java.util.*;

/**
 * Each user in your roster is represented by a roster entry, which contains the user's
 * JID and a name or nickname you assign.
 *
 * @author Matt Tucker
 */
public class RosterEntry {

    private String user;
    private String name;
    private RosterPacket.ItemType type;
    private XMPPConnection connection;

    /**
     * Creates a new roster entry.
     *
     * @param user the user.
     * @param name the nickname for the entry.
     * @param type the subscription type.
     * @param connection a connection to the XMPP server.
     */
    RosterEntry(String user, String name, RosterPacket.ItemType type, XMPPConnection connection) {
        this.user = user;
        this.name = name;
        this.type = type;
        this.connection = connection;
    }

    /**
     * Returns the JID of the user associated with this entry.
     *
     * @return the user associated with this entry.
     */
    public String getUser() {
        return user;
    }

    /**
     * Returns the name associated with this entry.
     *
     * @return the name.
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name associated with this entry.
     *
     * @param name the name.
     */
    public void setName(String name) {
        // Do nothing if the name hasn't changed.
        if (name != null && name.equals(this.name)) {
            return;
        }
        this.name = name;
        RosterPacket packet = new RosterPacket();
        packet.setType(IQ.Type.SET);
        packet.addRosterItem(toRosterItem(this));
        connection.sendPacket(packet);
    }

    /**
     * Updates the state of the entry with the new values.
     *
     * @param name the nickname for the entry.
     * @param type the subscription type.
     */
    void updateState(String name, RosterPacket.ItemType type) {
        this.name = name;
        this.type = type;
    }

    /**
     * Returns an iterator for all the roster groups that this entry belongs to.
     *
     * @return an iterator for the groups this entry belongs to.
     */
    public Iterator getGroups() {
        List results = new ArrayList();
        // Loop through all roster groups and find the ones that contain this
        // entry. This algorithm should be fine
        for (Iterator i=connection.roster.getGroups(); i.hasNext(); ) {
            RosterGroup group = (RosterGroup)i.next();
            if (group.contains(this)) {
                results.add(group);
            }
        }
        return results.iterator();
    }

    /**
     * Returns the roster subscription type of the entry. When the type is
     * RosterPacket.ItemType.NONE, the subscription request is pending.
     *
     * @return the type.
     */
    public RosterPacket.ItemType getType() {
        return type;
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        if (name != null) {
            buf.append(name).append(": ");
        }
        buf.append(user);
        Iterator groups = getGroups();
        if (groups.hasNext()) {
            buf.append(" [");
            RosterGroup group = (RosterGroup)groups.next();
            buf.append(group.getName());
            while (groups.hasNext()) {
            buf.append(", ");
                group = (RosterGroup)groups.next();
                buf.append(group.getName());
            }
            buf.append("]");
        }
        return buf.toString();
    }

    public boolean equals(Object object) {
        if (this == object) {
            return true;
        }
        if (object != null && object instanceof RosterEntry) {
            return user.toLowerCase().equals(((RosterEntry)object).getUser().toLowerCase());
        }
        else {
            return false;
        }
    }

    public XMPPConnection getConnection() {
    	return connection;
    }
    
    static RosterPacket.Item toRosterItem(RosterEntry entry) {
        RosterPacket.Item item = new RosterPacket.Item(entry.getUser(), entry.getName());
        item.setItemType(entry.getType());
        // Set the correct group names for the item.
        for (Iterator j=entry.getGroups(); j.hasNext(); ) {
            RosterGroup group = (RosterGroup)j.next();
            item.addGroupName(group.getName());
        }
        return item;
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.