Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Iterator;
- import java.util.HashMap;
- /**
- * A simple model of a mail server. The server is able to receive
- * mail items for storage, and deliver them to clients on demand.
- *
- * @author David J. Barnes and Michael Kölling
- * @version 2011.07.31
- */
- public class MailServer
- {
- // Storage for the arbitrary number of mail items to be stored
- // on the server.
- private HashMap<String, ArrayList<MailItem>> mailServer;
- /**
- * Construct a mail server.
- */
- public MailServer()
- {
- mailServer = new HashMap<String, ArrayList<MailItem>>();
- }
- /**
- * Create a mailbox for a single user
- * @param the name of the new user
- * @return 1 if the creation succeeded
- * 0 if a mailbox with that same name already exists
- */
- public int createMailbox(String user)
- {
- if (mailServer.containsKey(user)) {
- return 0;
- }
- else {
- ArrayList<MailItem> mailBox = new ArrayList<MailItem>();
- mailServer.put(user, mailBox);
- return 1;
- }
- }
- /**
- * Create mailboxes for users
- * @param an array containing the names of the new users
- * @return the number of successfully created mailboxes
- * this number will be less than the number of
- * specified users if some mailboxes already existed
- */
- public int createMailbox(String[] users)
- {
- int oldSize = mailServer.size();
- for (String u : users) {
- ArrayList<MailItem> mailBox = new ArrayList<MailItem>();
- mailServer.put(u, mailBox);
- }
- return mailServer.size() - oldSize;
- }
- /**
- * Retrieves the mailbox for a user
- *
- * @param who the owner of the requested mailbox
- * @return the mailbox belonging to who,
- * null if it does not exist
- */
- private ArrayList getMailbox(String who)
- {
- return mailServer.get(who);
- }
- /**
- * Return how many mail items are waiting for a user.
- * @param who The user to check for.
- * @return How many items are waiting.
- */
- public int howManyMailItems(String who)
- {
- return getMailbox(who).size();
- }
- /**
- * Return the next mail item for a user or null if there
- * are none.
- * @param who The user requesting their next item.
- * @return The user's next item.
- */
- public MailItem getNextMailItem(String who)
- {
- Iterator<MailItem> it = getMailbox(who).iterator();
- while(it.hasNext())
- {
- MailItem item = it.next();
- it.remove();
- return item;
- }
- return null;
- }
- private boolean ignore(MailItem item)
- {
- return (item.getFrom().equals("") || item.getTo().equals(""));
- }
- /**
- * Add the given mail item to the message list.
- * @param item The mail item to be stored on the server.
- */
- public void post(MailItem item)
- {
- if (!ignore(item))
- {
- for (String rec : item.getTo()) {
- if (mailServer.containsKey(rec)) {
- getMailbox(rec).add(0, item);
- }
- }
- }
- }
- public int howManyMessages()
- {
- int nMsg = 0;
- for (ArrayList<MailItem> mailBox : mailServer.values()) {
- nMsg = nMsg + mailBox.size();
- }
- return nMsg;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement