Buddies.java :  » UnTagged » androidgin » person » bangbang » im » Androidgin » Framework » Android Open Source

Android Open Source » UnTagged » androidgin 
androidgin » person » bangbang » im » Androidgin » Framework » Buddies.java
package person.bangbang.im.Androidgin.Framework;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.MethodNotSupportedException;

import person.bangbang.im.Androidgin.Util.Log;
/*
 * a collection of all buddies.
 * corresponding to pidgin's buddy [subsystem]
 * 
 * @author bangbang.song@gmail.com
 */
public class Buddies {
  private static final String TAG = "Framework.Buddies";
  private static Buddies _instance;

  /*
   * the buddy data, this mab be change in future for optimization.
   */
  private List<Buddy> mList = new ArrayList<Buddy>();

  private List<OnBuddiesEvent> mBuddyListeners = new ArrayList<OnBuddiesEvent>();

  private Buddies() {

  }
  /**
   * do Initialization work. e.g load saved buddies.
   */
  private void init(){
    
  }
  public static Buddies getInstance() {
    if (null == _instance) {
      _instance = new Buddies();
      _instance.init();
    }

    return _instance;
  }
  /**
   * 
   * @param buddyId NOTE format [protoclId]:[userName]
   * @return
   */
  public Buddy findBuddyById(String buddyId) {
    for (Buddy b : mList) {
      if (b.getID().equals(buddyId)) {
        return b;
      }
    }
    
    return null;
  }
  public List<Buddy> getAll() {
    return mList;
  }
  
  public void rem(Buddy b) {

    Buddy bb = findBuddyById(b.getID());
    
    if (null != bb) {
      mList.remove(bb);
      
      doBuddyRem(bb);
    }  
  }
  
  // this is NO duplicate check, you must be sure this.
//  public void addBuddy(Buddy b) {
  public void add(Buddy b){
    Log.d(TAG, "buddy added: " + b.toString());
    mList.add(b);
    
    doBuddyAdd(b);
  }
  // this is NO duplicate check, you must be sure this.
//  public void addBuddies(List<Buddy> l) {
  public void add(List<Buddy> l) {
    for (Buddy b : l) {
      add(b);
    }
  }

//  public Buddy getBuddyByName(String userName) {
//    for (Buddy b : mList) {
//      if (userName.equals(b.getUserName())) {
//        return b;
//      }
//    }
//    
//    return null;
//  }

  /**
   * @param groupName
   * @return
   */
  public List<Buddy> getBuddiesByGroup(String groupName) {
    return null;
  }
  /**
   * Schedule a save of the blist.xml file. 
   * @throws MethodNotSupportedException 
   */
  public void ScheduleSave() throws MethodNotSupportedException{
    throw new MethodNotSupportedException("ScheduleSave");
  }
  public void regOnBuddyEventListener(OnBuddiesEvent l) {
    mBuddyListeners.add(l);
  }
  public void unRegOnBuddyEventListener(OnBuddiesEvent l) {
    mBuddyListeners.remove(l);
  }
  protected void doBuddyAdd(Buddy b) {
    for (OnBuddiesEvent l : mBuddyListeners) {
      l.onAdd(b);
    }
  }
  protected void  doBuddyRem(Buddy b) {
    for (OnBuddiesEvent l : mBuddyListeners) {
      l.onRem(b);
    }
  }
  
  /**
   *
   */
  public interface OnBuddiesEvent {
    /**
     * a budy has added.
     */
    public void onAdd(Buddy b);
    /**
     * guess by the name.
     */
    public void onRem(Buddy b);
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.