⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 buddylist.java

📁 J2me实现的MSN Messeger客户端程序。聊天、添加好友、删除好友、阻止好友
💻 JAVA
字号:
/*
 * @(#)BuddyList.java
 *
 * Copyright (c) 2001-2002, JangHo Hwang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 	1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 	2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 	3. Neither the name of the JangHo Hwang nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *    $Id: BuddyList.java,v 2.0 2004/12/21 16:15:08 chenzs Exp $
 */
package vitular.msnp;

import java.util.Vector; //由list改为Vector 2004/5/29 ceze
import java.util.Hashtable;//由Map改为Hashtable 2004/5/29 ceze
import java.util.Enumeration;//Iterator改为Enmeration 2004/5/29 ceze
import vitular.msnp.entity.MsnFriend;

/**
 * 好友列表
 * <p>Title: MSNP Lib</p>
 * <p>Description: Unija 版Msn协议</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: Vitular</p>
 * @author ceze
 * @version 1.0
 */
public class BuddyList {
  private final String name;  //列表名称
  private Vector list = null;
  private Hashtable map = null;

  /**
   * 构造函数
   * @param name String
   */
  public BuddyList(String name) {
    this.name = name;
    list = new Vector();
    map = new Hashtable();

  }

  public String getName() {
    return this.name;
  }

  /**
   * 添加好友
   * @param friend MsnFriend
   */
  public void add(MsnFriend friend) {
    String loginName = friend.getLoginName();
    Object o = null;
    //以在好友名单中的
    if ( (o = map.get(loginName)) != null) {
      MsnFriend old = (MsnFriend) o;
      old.setFriendlyName(friend.getFriendlyName());
      old.setStatus(friend.getStatus());
    }
    else {
      list.addElement(friend);
      map.put(loginName, friend);
    }
  }

  /**
   * 删除好友
   * @param friend MsnFriend
   */
  public void remove(MsnFriend friend) {
    list.removeElement(friend);
    map.remove(friend.getLoginName());
  }

  /**
   * 删除好友
   * @param loginName String 用户id
   */
  public void remove(String loginName) {
    Object o = map.remove(loginName);
    if (o != null)
      list.removeElement(o);
  }

  public MsnFriend get(int index) {
    return (MsnFriend) list.elementAt(index);
  }

  public MsnFriend get(String loginName) {
    return (MsnFriend) map.get(loginName);
  }

  /**
   * 重新设置好友状态 , 如果好友不再列表中抛出"not found"异常
   * @param friend MsnFriend
   */
  public void set(MsnFriend friend) {
    MsnFriend mf = get(friend.getLoginName());
    if (mf != null) {
      mf.setFriendlyName(friend.getFriendlyName());
      mf.setStatus(friend.getStatus());
    }
    else
      throw new IllegalArgumentException(friend.getLoginName() +
                                         " not found on " + getName());
  }

  /**
   * 好友下线
   * @param loginName String
   */
  public void setOffline(String loginName) {
    MsnFriend mf = get(loginName);
    if (mf != null){
      if(this.name == "OL"){
        list.removeElement(mf);
        map.remove(loginName);
      }
      mf.setStatus(UserStatus.OFFLINE);
    }
    else
      throw new IllegalArgumentException(loginName + " not found on " + getName());
  }


  public Enumeration enumeration() {
    return list.elements();
  }

  public int size() {
    return list.size();
  }

/*
* 排序 删除 2004/5/29 ceze
*/
//  public synchronized void sort(Comparator comp) {
//    Collections.sort(list, comp);
//  }

  public void clear() {
    list.removeAllElements();
    map.clear();
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -