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

📄 group.java

📁 JAVA平台下优秀的CHART开源代码,可以实现类似EXCEL中的二维或三维的饼图/椎图功能.
💻 JAVA
字号:
/**
 * Copyright (C) 2003  Manfred Andres
 * 
 * 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 freecs.core;

import freecs.interfaces.*;
import java.util.Vector;
import java.util.Iterator;

public class Group implements IGroupState, IMessageDestination {
   private String       name;
   private String       theme;
   private int          state;
   private Vector       usr;
   private Vector       susers;
   private Vector       banList;

   public Group (String name, String theme) {
      this.name   = name;
      this.theme  = theme;
      this.state  = OPEN;
      this.usr  = new Vector ();
      this.susers = new Vector ();
      this.banList = new Vector ();
   }

   public Group (String name, String theme, int state) {
      this.name   = name;
      this.theme  = theme;
      this.state  = state;
      this.usr  = new Vector ();
      this.susers = new Vector ();
      this.banList = new Vector ();
   }

   /**
    * returns the name of this group
    * @return the name of this group
    */
   public String getName () {
      return name;
   }

	/**
	 * set the theme of this group
	 * @param t the theme to use for this group
	 */
   public void setTheme (String t) {
      	theme = t;
   }
   /**
    * returns the theme of this group
    * @return the theme of this group
    */
   public String getTheme () {
      return theme;
   }

   /**
    * adds this user to this group
    * @param u The user joining this Group
    */
   public boolean addUser (User u) { 
   		return (addUser (u, u)); 
   }
   
	public void addLoginUser (User u) {
		synchronized (usr) {
			usr.addElement(u);
		}
		u.setGroup(this);
	}
   /**
    * adds this user to this group using the right of another user
    * @param u The user to join to this group
    * @param ru The user to check for the rights for joining ths group
    * @return ture on success, false if joining is not allowed
    */
	public boolean addUser (User u, User ru) {
		synchronized (usr) {
			if (usr.contains (u)) return true;
			if (!usrMayJoin(ru)) return false;
			boolean isFirst = usr.isEmpty();
			usr.addElement (u);
			u.setGroup(this);
			if (isFirst && (state & ENTRANCE) == 0 && !name.toLowerCase().equals("exil")) {
				addToSusers (u);
			}
		}
		return true;
	}

   /**
    * removes this user from this group
    * @param u the user to remove from this group
    */
   public void removeUser (User u) {
   	  synchronized (usr) {
      	usr.remove (u);
		if (usr.size() <= 0 && (this.state & ENTRANCE) != ENTRANCE) {
		   GroupManager.mgr.removeGroup (this.name);
		}
   	  }
   	  synchronized (susers) {
      	if (susers.contains (u))
         	susers.remove (u);
   	  }
      u.unsetGroup ();
   }

   /**
    * adds user to su-user-list of this room
    * @param u The user to add to the su-user-list of this group
    */
	public void addToSusers (User u) {
		synchronized (susers) {
			if (susers.contains (u)) return;
			susers.addElement (u);
		}
	}

	/**
	 * removes a user from the su-user-list of this group
	 * @param u The user to remove from the su-user-list
	 */
	public void removeFromSusers (User u) {
		synchronized (susers) {
			while (susers.contains(u))
				susers.removeElement (u);
		}
	}

   /**
    * Interface IMessageDestination used for sending messages to this group
    * meaning all users of this group
    */

   /**
    * sends a message to all users of this group. If the group is moderated,
    * only the moderator will see the message. A moderator may send a users
    * message by typing /ack username theMessageOfTheUser. This will be done
    * by a template, causing the message to have a link which sends this 
    * acknowledgement.
    * @param mc the IContainer containing the content to send to all users of this group
    */
	public void sendMessage (IContainer mc) {
		if (usr.size () < 1)
			return;
		
		boolean senderIsModerator = false;
		if (mc instanceof MessageParser) {
			User sender = ((MessageParser) mc).getSender();
			senderIsModerator = sender.hasRight(IUserRights.ROLE_MODERATOR);
		}
		if (!senderIsModerator && 
			this.hasState(IGroupState.MODERATED)) {
			for (Iterator e = users (); e.hasNext ();) {
				User cu = (User) e.next ();
				if (!cu.hasRight(IUserRights.ROLE_MODERATOR))
					continue;
				cu.sendMessage (mc);
			}
			return;
		}
		sendMsg(mc);
	}
   
	private void sendMsg (IContainer mc) {
		for (Iterator e = users (); e.hasNext ();) {
			User cu = (User) e.next ();
			cu.sendMessage (mc);
		}
	}
	
	public void sendModeratorMessage (IContainer mc) {
		if (usr.size () < 1) 
			return;
		User sender = ((MessageParser) mc).getSender();
		for (Iterator e = users (); e.hasNext ();) {
			User cu = (User) e.next ();
			if (sender.equals(cu))
				continue;
			cu.sendMessage (mc);
		}
	}
	
   /**
	* sends a message to all users of this group
	* @param mc the IContainer containing the content to send to all users of this group
	*/
	public void sendModeraedMessage (IContainer mc) {
		if (usr.size () < 1) 
			return;
		sendMsg(mc);
   }

   /**
    * returns an Iterator containing all users of this group
    * @return an Iterator for looping over all groupmembers of this group
    */
   public Iterator users () {
      return usr.iterator ();
   }

	/**
	 * Check if the given user may join this group
	 * @param u The user to check the rights from
	 * @return true if the user is allowed to join, false if not
	 */
   public boolean usrMayJoin (User u) {
   		if (u==null) 
   			return false;
		if (!u.hasRight (IUserRights.MAY_JOIN_GROUP)) 
			return false;
		if (!hasState(OPEN) 
			&& !u.hasRight (IUserRights.MAY_JOIN_LOCKED_GROUP) 
			&& !susers.contains (u)) 
				return false;
		String uname = u.getName().toLowerCase();
		if (banList.contains(uname))
			return false;
		return true;
	}

	/**
	 * Set the group-ban-state for a user
	 * @param u The user to set the state for
	 * @param on If true, the user will be banned, if false the user will be unbanned
	 */
	public void setBanForUser (String u, boolean on) {
		String uname = u.toLowerCase();
		synchronized (banList) {
			if (on && !banList.contains (uname)) 
				banList.addElement (uname);
			else if (!on) 
				banList.removeElement (uname);
		}
	}
	
	public Vector bannedUsers () {
		return (Vector) banList.clone();
	} 

	/**
	 * Check if the given user is banned
	 * @param u The user to chech the ban-state for this room
	 * @return true if the user is banned, false if not
	 */
   public boolean usrIsBaned (String u) {
	  String uname = u.toLowerCase();
	  synchronized (banList) {
      	return banList.contains (uname);
	  }
   }

	/**
	 * Checks if a specific user is insied this room
	 * @param u The user to search for
	 * @return true if the user is within this group, false if not
	 */
   public boolean usrIsPresent (User u) {
      return usr.contains (u);
   }

	/**
	 * Checks if the given user is in the SU-list for this group
	 * @param u The user to check for
	 * @return true if the user is contained in the SU-list, false if not
	 */
   public boolean usrIsSu (User u) {
   	  synchronized (susers) {
      	return susers.contains (u);
   	  }
   }

	/**
	 * returns the number of superusers in this grou
	 * @return number of superusers in this group
	 */
	public int suUserCount () {
		return susers.size();
	}
	
	/**
	 * returns the number of users within this group
	 * @return the number of users within this group
	 */
	public int size() {
		return usr.size();
	}
	/**
	 * checks if a state is true for this group
	 * @param state the state which must be given
	 * @return true if the state is given, false if not
	 */
   public boolean hasState (int state) {
      return (this.state & state) == state;
   }

	/**
	 * enables the given state
	 * @param state the state to enable
	 */
   public void setState (int state) {
      this.state = this.state | (state - (this.state & state));
   }

	/**
	 * disables the given state
	 * @param state the state to disable
	 */
   public void unsetState (int state) {
      this.state = this.state - (this.state & state);
   }

	/**
	 * checks if the given group is equal to this group
	 * @return true if the two groups are equal
	 */
   public boolean equals (Object g) {
		if (g==null) return false;
      if (!(g instanceof Group)) return false;
      String n = ((Group) g).getName ();
      if (n == null) return false;
      return (n.equalsIgnoreCase (this.name));
   }
}

⌨️ 快捷键说明

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