Added some finishing touches to the Message classes. Integrated some event support in input handling.
141 lines
3.9 KiB
Java
141 lines
3.9 KiB
Java
package elements;
|
|
|
|
|
|
import elements.exceptions.ReadErrorListException;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* Created by Guillermo Serrahima on 4/21/16.
|
|
*/
|
|
public class Chat {
|
|
private static final String DATE_TAG = "Date=";
|
|
private static final String WINNER_TAG = "Winner=";
|
|
|
|
public static final String MODE_NORMAL = "mode_normal";
|
|
public static final String MODE_RANDOM_HOUR = "mode_random_hour";
|
|
public static final String MODE_COLORS = "mode_colors";
|
|
public static final String MODE_FAR_WEST = "mode_far_west";
|
|
|
|
private String name;
|
|
private Map<String, ArrayList<ChatListItem>> lists;
|
|
|
|
public Chat() {
|
|
lists = new HashMap<String, ArrayList<ChatListItem>>();
|
|
name = null;
|
|
}
|
|
|
|
public Chat(String n) {
|
|
lists = new HashMap<String, ArrayList<ChatListItem>>();
|
|
name = n;
|
|
}
|
|
|
|
public boolean exists(String list) {
|
|
return lists.containsKey(list);
|
|
}
|
|
|
|
public void add(String listName, ArrayList<ChatListItem> list) {
|
|
lists.put(listName, list);
|
|
}
|
|
|
|
public void add(String list) {
|
|
lists.put(list, new ArrayList<ChatListItem>());
|
|
}
|
|
|
|
/*
|
|
public void winPole(String user, int date) throws PoleBlockedException {
|
|
if(!users.containsKey(user)) add(user);
|
|
if(!availablePole()) {
|
|
if(poleWinner != null) throw new PoleBlockedException(poleWinner);
|
|
else throw new PoleBlockedException();
|
|
}
|
|
users.replace(user, users.get(user)+1);
|
|
|
|
|
|
|
|
poleWinner = user;
|
|
}
|
|
*/
|
|
|
|
public String printList(String list) {
|
|
ArrayList<ChatListItem> l = lists.get(list);
|
|
String s = list + ":\n\n";
|
|
|
|
for(int i = 0; i < l.size(); i++) {
|
|
s += l.get(i) + "\n";
|
|
}
|
|
|
|
return s.trim();
|
|
}
|
|
|
|
public String orderAlphabetically(String list) {
|
|
ArrayList<ChatListItem> toOrder = lists.get(list);
|
|
|
|
toOrder.sort(new ChatListItemComparator());
|
|
|
|
return "Lista ordenada.\n" + printList(list);
|
|
}
|
|
|
|
public ArrayList<List<String>> saveLists() {
|
|
ArrayList<List<String>> listDocs = new ArrayList<List<String>>();
|
|
List<String> lines;
|
|
|
|
Iterator it = lists.entrySet().iterator();
|
|
|
|
if(name != null) {
|
|
lines = new ArrayList<String>();
|
|
|
|
lines.add("#Chat name:");
|
|
lines.add("#" + name);
|
|
|
|
listDocs.add(lines);
|
|
}
|
|
|
|
while(it.hasNext()) {
|
|
lines = new ArrayList<String>();
|
|
|
|
Map.Entry<String, ArrayList<ChatListItem>> entry = (Map.Entry<String, ArrayList<ChatListItem>>) it.next();
|
|
lines.add("#List name:");
|
|
lines.add("#" + entry.getKey());
|
|
|
|
ArrayList<ChatListItem> list = entry.getValue();
|
|
|
|
for(int i = 0; i < list.size(); i++) {
|
|
lines.add(list.get(i).saveItem());
|
|
}
|
|
|
|
listDocs.add(lines);
|
|
}
|
|
|
|
return listDocs;
|
|
}
|
|
|
|
public void loadList(String listName, List<String> lines) throws ReadErrorListException {
|
|
ArrayList<ChatListItem> list = new ArrayList<ChatListItem>();
|
|
|
|
for(int i = 0; i < lines.size(); i++) {
|
|
//Comprueba si la línea es un comentario.
|
|
if(lines.get(i).startsWith("#") || lines.get(i).length() < 1) {
|
|
//do nothing
|
|
}
|
|
//Comprueba si la línea es un comentario. (Windows)
|
|
else if(lines.get(i).startsWith("\uFEFF#")) {
|
|
//do nothing
|
|
}
|
|
else {
|
|
System.out.println("Reading " + lines.get(i));
|
|
String[] reading = lines.get(i).split(";");
|
|
list.add(new ChatListItem(reading[0], Integer.parseInt(reading[1])));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class ChatListItemComparator implements Comparator<ChatListItem> {
|
|
|
|
@Override
|
|
public int compare(ChatListItem entr1, ChatListItem entr2) {
|
|
return entr1.compareTo(entr2);
|
|
}
|
|
}
|