import java.util.*;
public class UserList {
private Hashtable list=new Hashtable();
private Hashtable active=new Hashtable();
UserList(){
}
protected synchronized boolean add(String user){
if(exists(user))
return false;;
list.put(user, new MessageQueue());
touch(user);
return true;
}
protected synchronized void touch(String user){
if(exists(user))
active.put(user, new Long(System.currentTimeMillis()));
}
protected synchronized Enumeration list(){
return list.keys();
}
protected synchronized boolean drop(String user){
if(!exists(user))
return false;
list.remove(user);
active.remove(user);
return true;
}
protected boolean exists(String user){
return list.containsKey(user);
}
protected void addMessage(Message message){
Enumeration allUsers=list.keys();
while(allUsers.hasMoreElements())
addUserMessage((String)allUsers.nextElement(),message);
}
protected void addUserMessage(String user,Message message){
((MessageQueue)list.get(user)).add(message);
}
protected void resetUser(String user){
((MessageQueue)list.get(user)).reset();
}
protected Enumeration getMessage(String user){
touch(user);
return ((MessageQueue)list.get(user)).listMessages();
}
protected synchronized Enumeration inactive(int timeout){
Vector inactive=new Vector();
long now=System.currentTimeMillis();
long when=now-timeout;
Enumeration users=list.keys();
while(users.hasMoreElements()){
String user=(String) users.nextElement();
if(((Long)active.get(user)).longValue()<when){
inactive.addElement(user);
}
}
return inactive.elements();
}
}
编译时:UserList.java :使用了未经检查或不安全的操作;请使用 -Xlint:unchecked 重新编译