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

📄 modelsorter.java

📁 类似于MSN
💻 JAVA
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.models;

import java.io.UnsupportedEncodingException;
import java.util.Comparator;


/**
 * <pre>
 * 用于对群组排序,群组和好友组是不同的,因为其有一个父子结构,所以排序的策略是:
 * 1. 如果都是群,则先按照类型排,固定群排在上面,然后相同类型的按照名称排
 * 2. 如果一个是群,一个是成员,如果这个成员属于这个群,那肯定成员排在后面,如果
 *    这个成员不属于这个群,则和这个成员的父群比较
 * 3. 如果两个都是成员且两个都在一个群中,按照好友那么排;如果两个在不同的群中,
 *    比较他们的父群
 * 
 * 好友排序的方法,原则是上线的在最前面,离开的在后面,离线或隐身的在最后,如果某个好友有消息
 * 来了,那么把他当作上线状态对待,其次按照昵称排序。
 * </pre>
 * @author 马若劼
 */
public class ModelSorter implements Comparator {
	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object o1, Object o2) {
		if(o1 instanceof ClusterModel && o2 instanceof ClusterModel) {
			/* 如果两者都是群组 */
			ClusterModel c1 = (ClusterModel)o1;
			ClusterModel c2 = (ClusterModel)o2;
			return compareClusterModel(c1, c2);
		} else if(o1 instanceof ClusterModel || o2 instanceof ClusterModel) {
			/* 有一个是ClusterModel,另一个是FriendModel */
			ClusterModel c1 = null;
			ClusterModel c2 = null;
			if(o1 instanceof ClusterModel) {
				c1 = (ClusterModel)o1;
				c2 = (ClusterModel)((FriendModel)o2).getProperty("parent");
			} else {
				c1 = (ClusterModel)((FriendModel)o1).getProperty("parent");
				c2 = (ClusterModel)o2;
			}
			return (c1 == c2) ? -1 : compareClusterModel(c1, c2);
		} else {
			/* 两个都是FriendModel */
			return compareFriendModel((FriendModel)o1, (FriendModel)o2);
		}
	}
	
	// 比较两个FriendModel
	private int compareFriendModel(FriendModel f1, FriendModel f2) {
		// 得到好友状态
		String s1 = (String)f1.getProperty("status");
		String s2 = (String)f2.getProperty("status");
		// 得到显示名
		String _n1 = (String)f1.getProperty("name");
		String _n2 = (String)f2.getProperty("name");
		// 比较
        if(s1.equalsIgnoreCase(s2))
        	return compareChineseString(_n1, _n2);
        else if(s1.equalsIgnoreCase("online"))
        	return -1;
        else if(s2.equalsIgnoreCase("online"))
        	return 1;
        else if(s1.equalsIgnoreCase("away"))
        	return -1;
        else if(s2.equalsIgnoreCase("away"))
        	return 1;
        else
        	return compareChineseString(_n1, _n2);
	}
	
	// 比较两个ClusterModel
	private int compareClusterModel(ClusterModel c1, ClusterModel c2) {
		if(c1.isPermanent()) {
			/* 如果群1是固定群 */
			if(c2.isPermanent()) // 如果群2也是固定群,比较名字
				return compareChineseString((String)c1.getProperty("name"), (String)c2.getProperty("name"));
			else // 如果群2不是固定群,群1排在前面
				return -1;
		} else {
			/* 如果c1不是固定群 */
			if(c2.isPermanent()) // 如果群2是固定群,群2排在前面
				return 1;
			else // 如果c2也不是固定群,比较名字
				return compareChineseString((String)c1.getProperty("name"), (String)c2.getProperty("name"));
		}
	}
	
	// 比较中文字符串,比较后的结果按照拼音方式排序
	private int compareChineseString(String _n1, String _n2) {
		String n1 = null, n2 = null;
        try {
            n1 = new String(_n1.getBytes("GBK"), "ISO-8859-1");
            n2 = new String(_n2.getBytes("GBK"), "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            n1 = _n1;
            n2 = _n2;
        }
        return n1.compareTo(n2);
	}
}

⌨️ 快捷键说明

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