001 package edu.harvard.deas.hyperenc; 002 003 import java.util.ArrayList; 004 import java.util.Iterator; 005 import java.util.List; 006 import java.util.NoSuchElementException; 007 008 import javax.mail.Address; 009 010 011 /** 012 * A contact list backed by a standard {@link java.util.List}. 013 */ 014 public class BasicContactList extends ContactList { 015 private List<Contact> lst; 016 017 private Contact myOwner; 018 019 /** 020 * Creates a new, empty BasicContactList. 021 * @param owner 022 * the owner of this list (usually the Contact corresponding to the 023 * user of the hyper-encryption application) 024 */ 025 public BasicContactList(Contact owner) { 026 lst = new ArrayList<Contact>(); 027 myOwner = owner; 028 } 029 030 @Override 031 public void addContact(Contact c) { 032 lst.add(c); 033 } 034 035 @Override 036 public Contact get(int index) { 037 return lst.get(index); 038 } 039 040 @Override 041 public Contact getContact(Address a) { 042 if (a.equals(myOwner.getEmail())) 043 return myOwner; 044 for (Contact c : lst) 045 if (a.equals(c.getEmail())) 046 return c; 047 throw new NoSuchElementException("No contact with address " + a); 048 } 049 050 @Override 051 public void removeContact(Contact c) { 052 lst.remove(c); 053 } 054 055 @Override 056 public int size() { 057 return lst.size(); 058 } 059 060 @Override 061 public Iterator<Contact> iterator() { 062 return lst.iterator(); 063 } 064 065 }