PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ChatApp/ContactList.java

https://bitbucket.org/blitz2145/cop4331-chat-application
Java | 146 lines | 78 code | 12 blank | 56 comment | 9 complexity | f92fe80527f9aaa65064e769a6f1517a MD5 | raw file
  1package ChatApp;
  2import java.io.*;
  3import java.net.InetAddress;
  4import java.util.*;
  5
  6/**
  7 * This class manages a list of contacts
  8 */
  9public class ContactList {
 10	
 11	/**
 12	 * Constructs a new contact list by reading data from "ContactList.dat".
 13	 * If the file is not found, an empty list will be created
 14	 */
 15	//suppress unchecked cast exception when deserializing contact list
 16	@SuppressWarnings("unchecked")
 17	public ContactList() {
 18		try {
 19			ObjectInputStream in = new ObjectInputStream(new FileInputStream("ContactList.dat"));
 20			list = (ArrayList<Contact>)in.readObject();
 21			in.close();
 22			Contact tmp = new Contact("test2");
 23			tmp.setIpAddress(InetAddress.getByName("192.168.1.136"));
 24			//list.add(tmp);
 25			
 26		} catch (FileNotFoundException e) {
 27			// TODO Auto-generated catch block
 28			list = new ArrayList<Contact>();
 29			System.out.print("Contact List file not found...");
 30			//e.printStackTrace();
 31		} catch (IOException e) {
 32			// TODO Auto-generated catch block
 33			e.printStackTrace();
 34		} catch (ClassNotFoundException e) {
 35			// TODO Auto-generated catch block
 36			e.printStackTrace();
 37		}
 38	}
 39	
 40	/**
 41	 * Selects a contact from the list
 42	 * @param index the index of the contact to be selected
 43	 * If (index < 0 || index > list.size() -1) the contact selected will be null
 44	 */
 45	public void setSelected(int index) {
 46		if(index < 0 || index > list.size() -1) {
 47			selectedContact = null;
 48		}
 49		selectedContact = list.get(index);
 50	}
 51	
 52	/**
 53	 * Accessor for the selected Contact
 54	 * @return the selected Contact
 55	 */
 56	public Contact getSelected() {
 57		return selectedContact;
 58	}
 59	
 60	/**
 61	 * Adds a contact to the list
 62	 * @param contact the contact to be added
 63	 * @return true
 64	 * @precondition contact != null
 65	 */
 66	public boolean add(Contact contact) {
 67		assert contact != null : "violated precondition contact != null";
 68		return list.add(contact);
 69	}
 70	
 71	/**
 72	 * Removes a contact from the list
 73	 * @param contact the contact to be removed
 74	 * @precondition contact != null
 75	 */
 76	public void remove(Contact contact) {
 77		assert contact != null : "violated precondition contact != null";
 78		list.remove(contact);
 79	}
 80	
 81	/**
 82	 * Given a username, this method searches the list of contacts for this
 83	 * username and returns the index of the contact in the list if found.
 84	 * It returns -1 if the username is not found.
 85	 * 
 86	 * @param
 87	 * 		username the username to find in the contacts list
 88	 * @returns (index >= 0) if the username is found in the contact list
 89	 * 			-1			 if the username is not found in the contact list
 90	 */
 91	public int find(String username) {
 92		int i = 0;
 93		for(Contact contact : list) {
 94			if(username.equals(contact.getUsername())){
 95				return i;
 96			}
 97			i++;
 98		}
 99		return -1;
100	}
101	
102	/**
103	 * Creates a string array of the contacts' usernames
104	 * @return the usernames
105	 */
106	public String[] getNames() {
107		if(list == null) {
108			return null;
109		}
110		String[] names = new String[list.size()];
111		int i = 0;
112		for(Contact contact : list) {
113			names[i] = contact.getUsername();
114			i++;
115		}
116		return names;
117	}
118	
119	/**
120	 * Saves the contact list to file
121	 */
122	public void saveContacts() {
123		try {
124			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("ContactList.dat"));
125			out.writeObject(list);
126			out.close();
127		} catch (FileNotFoundException e) {
128			// TODO Auto-generated catch block
129			e.printStackTrace();
130		} catch (IOException e) {
131			// TODO Auto-generated catch block
132			e.printStackTrace();
133		}
134	}
135	
136	/**
137	 * 
138	 * @return the number of contacts in the list
139	 */
140	public int size() {
141		return list.size();
142	}
143	
144	private ArrayList<Contact> list;
145	private Contact selectedContact;
146}