UserSet.java :  » Math » migen » uk » ac » lkl » migen » system » server » Java Open Source

Java Open Source » Math » migen 
migen » uk » ac » lkl » migen » system » server » UserSet.java
package uk.ac.lkl.migen.system.server;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import uk.ac.lkl.migen.system.MiGenConfiguration;

/**
 * A set of users in the MiGen system. 
 * 
 * Most of the time, a UserSet will comprise only one user. However, this
 * is not true for e.g. collaboration tasks. 
 * 
 * @author sergut
 *
 */
public class UserSet implements Iterable<User>, Comparable<UserSet> {
    /**
     * The list of users
     */
    private List<User> users;

    /**
     * A new user set. 
     * 
     * @param users a sequence of users
     * 
     * @throws IllegalArgumentException if the sequence of users is empty
     */
    public UserSet(User... users) {
  this(Arrays.asList(users));
    }

    /**
     * A new user set. 
     * 
     * @param users a list of users
     * 
     * @throws IllegalArgumentException if the list of users is empty
     */
    public UserSet(Collection<User> users) {
  if (users.size() == 0)
      throw new IllegalArgumentException("UserSet must consist of at least one user.");
  
  this.users = new ArrayList<User>(users);
    }

    /**
     * Returns an iterator for the users in this UserSet.
     * 
     * @return an iterator for the users in this UserSet.
     */
    public Iterator<User> iterator() {
  return Collections.unmodifiableList(users).iterator();
    }

    /**
     * Returns the user at the given index.
     * 
     * @param index the index for the user to be returned.
     * 
     * @return the user at the given index.
     */
    public User getUser(int index) {
  return users.get(index);
    }

    /**
     * Returns the number of users in this UserSet.
     * 
     * @return the number of users in this UserSet.
     */
    public int size() {
  return users.size();
    }

    /**
     * Returns true if this UserSet comprises only one user, false otherwise. 
     * 
     * @return true if this UserSet comprises only one user, false otherwise.
     */
    public boolean isSingleUser() {
  return (this.size() == 1);
    }

    /**
     * Returns a view-only list with the users in this UserSet.
     *  
     * @return a view-only list with the users in this UserSet.
     */
    public List<User> getUsers() {
  return Collections.unmodifiableList(users);
    }

    /**
     * Use getNames() instead (except for special cases). 
     * 
     * Returns a list with the names for all users in this UserSet. This method
     * always returns the true names (i.e. no anonimisation). Use with care.  
     * 
     * @return a list with the names for all users in this UserSet
     */
    public List<String> getNonAnonimisedNames() {
  List<String> result = new ArrayList<String>();
  for (User user : users) {
      result.add(user.getName());
  }
  return result;
    }
    
    /**
     * Returns a String with the names for all users in this UserSet separated
     * by the given string. 
     * 
     * @return a String with the names for all users in this UserSet separated
     * by the given string.
     */
    public String getNamesAsString(String separator) {
  return fromStringListToString(getNonAnonimisedNames(), separator);
    }
    
    /**
     * Returns a String with the names for all users in this UserSet separated
     * by a slash ("/"). 
     * 
     * @return a String with the names for all users in this UserSet separated
     * by a slash ("/").
     */
    public String getNamesAsString() {
  return getNamesAsString("/");
    }

    /**
     * Returns a String with the names for all users in this UserSet separated by a slash ("/"). 
     * 
     * The names, however, can be anonimised if the corresponding
     * configuration property is on.  
     * 
     * @return a String with the (maybe anonimised) names 
     */
    public String getNames() {
  if (MiGenConfiguration.isShowingOnlyInitials())
      return getInitialsAsString();
  else 
      return getNamesAsString();
    }

    /**
     * Returns a list with the initials for all users in this UserSet 
     * 
     * @return a list with the initials for all users in this UserSet
     */
    public List<String> getInitials() {
  List<String> result = new ArrayList<String>();
  for (User user : users)
      result.add(user.getInitials());
      
  return result;
    }
    
    /**
     * Returns a String with the initials for all users in this UserSet separated
     * by the given string. 
     * 
     * @return a String with the initials for all users in this UserSet separated
     * by the given string.
     */
    public String getInitialsAsString(String separator) {
  return fromStringListToString(getInitials(), separator);
    }
    
    /**
     * Returns a String with the initials for all users in this UserSet separated
     * by a slash ("/"). 
     * 
     * @return a String with the initials for all users in this UserSet separated
     * by a slash ("/").
     */
    public String getInitialsAsString() {
  return getInitialsAsString("/");
    }

    /**
     * Returns the concatenation of the strings in the given list separated
     * by the given separator. 
     * 
     * @param strings a list of strings
     * @param separator the separator
     * 
     * @return  the concatenation of the strings in the given list separated
     * by the given separator.
     */
    private String fromStringListToString(List<String> strings, String separator) {
  String result = "";
  for (Iterator<String> itr = strings.iterator(); itr.hasNext(); ) {
      result += itr.next();
      if (itr.hasNext())
    result += separator;
  }
  return result;
    }
    
    /**
     * Returns the concatenation of the strings in the given list separated by 
     * a slash "/".
     * 
     * @param strings a list of strings
     * @param separator the separator
     * 
     * @return  the concatenation of the strings in the given list separated
     * a slash "/".
     */
    public String fromStringListToString(List<String> strings) {
  return fromStringListToString(strings, "/");
    }

    /**
     * Adds a user to this UserSet.
     * 
     * If the user is already in the UserSet, does nothing.
     * 
     * @param newUser the user to be added
     */
    public void addUser(User newUser) {
  if (!users.contains(newUser))
      users.add(newUser);
    }

    @Override
    public String toString() {
  return users.toString();
    }

    @Override
    public boolean equals(Object object) {
  if (!(object instanceof UserSet))
      return false;
  
  UserSet other = (UserSet) object;
  return this.users.equals(other.users);
    }

    @Override
    public int hashCode() {
  return users.hashCode();
    }

    @Override
    public int compareTo(UserSet o) {
  return this.getNamesAsString().compareTo(o.getNamesAsString());
    }
}
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.