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

📄 user.java

📁 本程序可在局域网内实现聊天、传送文件功能
💻 JAVA
字号:
package org.tuna.net.csuim;

import java.util.*;

public class User
{
	private ArrayList<UserProp> property;
	private class UserProp
	{
		public String name;
		public String value;
		public UserProp(String n, String v)
		{ name = n; value = v; } 
	}
	
	
	public User(String str) throws UserCreateException
	{
		property = new ArrayList<UserProp>();
		parse(str);
	}
	
	public User(String ID, boolean on)
	{
		property = new ArrayList<UserProp>();
		add("ID", ID);
		add("online", on + "");
		add("onPort", "33355");
	}
	
	
	// -- Public Methods --		
	public void add(String str)
	{
		UserProp[] up = parseString(str);
		if ( up != null )
			for(int i = 0; i < up.length; i++)
			   add(up[i].name, up[i].value);
	}
	
	public void add(String name, String value)
	{
		if ( !isProp(name) )
		   property.add(new UserProp(name, value));
	}
	
	public int countProperties()
	{
		return property.size();
	}
	
	public String[] getAllNames()
	{
		if ( property.size() == 0 )
	     return null;
	  else{
	  	String[] names = new String[property.size()];
	  	for(int i = 0; i < property.size(); i++)
	  	   names[i] = property.get(i).name;
	  	return names;
	  }
	}
	
	public void remove(String name)
	{
		if ( isProp(name) ){
			int i = index(name);
			property.remove(i);
		}
	}
		
	public String toString()
	{
		if ( property.size() == 0 )
		   return null;
		else{
			String r = "";
			for( int i = 0; i < property.size(); i++ )
			   r += ("<" + property.get(i).name + ">" + property.get(i).value);
			return r;
		}
	}
	
	public String toString(String[] names)
	{
		if (names == null) return null;
		else if (names.length == 0) return null;
		else{
			String str = "";
			for(String n : names){
				if ( isProp(n) )
				   str += ("<" + property.get(index(n)).name + ">" + property.get(index(n)).value);
			}
			return str;
		}
  }
	
	public void update(User u)
	{
		update(u.toString());
		add(u.toString());
	}
	
	public void update(String name, String value)
	{
		if ( isProp(name) ){
			int i = index(name);
			property.get(i).value = value;
		}
	}
	
	public void update(String str)
	{
		UserProp[] up = parseString(str);
		if ( up != null )
			for(int i = 0; i < up.length; i++)
			   update(up[i].name, up[i].value);
	}
	
	public String value(String name)
	{
		if ( !isProp(name) )
		   return null;
		else
		   return property.get(index(name)).value;
	}
	
	
	//--------  Private Method  ----------
	private void parse(String str) throws UserCreateException
	{
		if (str == null) 
		   return;			
		ArrayList<String> temp = new ArrayList<String>();
		StringTokenizer stk = new StringTokenizer(str, "<");		
		if (str.charAt(0) != '<')
		   stk.nextToken();
		while(stk.hasMoreTokens()){
			temp.add(stk.nextToken());
		}		
		for(int i = 0; i < temp.size(); i++){
			int pos = 0;
			String tmp = temp.get(i);
			for( ; pos < tmp.length(); pos++ )
			   if ( tmp.charAt(pos) == '>' )
			      break;
			if ( pos == 0 || pos == tmp.length() - 1 )
			   continue;
			property.add(new UserProp(tmp.substring(0, pos), tmp.substring(pos + 1)));
		}
		if ( property.size() == 0 )
		   throw new UserCreateException("String parse error!");
		//强制添加ID和online
		if ( !isProp("ID") )
		   add("ID", "Tuna");
		if ( !isProp("online") )
		   add("online", "false");
		if ( !isProp("onPort") )
		   add("onPort", "33355");
	}
	
	private UserProp[] parseString(String str)
	{
		if (str == null) return null;
			
		ArrayList<String> temp = new ArrayList<String>();
		StringTokenizer stk = new StringTokenizer(str, "<");		
		if (str.charAt(0) != '<')
		   stk.nextToken();
		while(stk.hasMoreTokens()){
			temp.add(stk.nextToken());
		}
		
		ArrayList<UserProp> usr = new ArrayList<UserProp>();
		for(int i = 0; i < temp.size(); i++){
			int pos = 0;
			String tmp = temp.get(i);
			for( ; pos < tmp.length(); pos++ )
			   if ( tmp.charAt(pos) == '>' )
			      break;
			if ( pos == 0 || pos == tmp.length() - 1 )
			   continue;
			usr.add(new UserProp(tmp.substring(0, pos), tmp.substring(pos + 1)));
		}
		if (usr.size() == 0) return null;
		else{
			UserProp[] up = new UserProp[usr.size()];
			return usr.toArray(up);
		}
	}
	
	private int index(String n)
	{
		int i;
		for( i = 0; i < property.size(); i++ )
		   if ( property.get(i).name.equals(n) )
		      return i;
		return -1;
	}
	
	private boolean isProp(String name)
	{
		int i;
		if ( (i = index(name)) == -1 )
		   return false;
		else 
		   return true;
	}
	
	
	// ----------  For Test  -------------
	public static void main(String[] args)
	{
		
	}
}

⌨️ 快捷键说明

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