-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatRoom.java
More file actions
90 lines (80 loc) · 3.05 KB
/
ChatRoom.java
File metadata and controls
90 lines (80 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class ChatRoom {
//name of room
String name;
//list of users
List<Integer> users = Collections.synchronizedList(new ArrayList<Integer>());
ConcurrentLinkedDeque<String> history = new ConcurrentLinkedDeque<String>();
//comparison method: checks if o is a chatroom, if not, return false, otherwise, return whether or not the name is equal
@Override
public boolean equals(Object o){
if(o instanceof ChatRoom){
ChatRoom object = (ChatRoom) o;
return object.name.equals(this.name);
} else {
return false;
}
}
//constructor
public ChatRoom(String name){
this.name = name;
}
//synchronized editors for the list of users
public synchronized void add(int i){
users.add(i);
}
public synchronized void remove(int i){
users.remove(users.indexOf(i));
}
//synchronized getter to return the list of users as an int array
public synchronized int[] users(){
int[] list = new int[users.size()];
for (int i = 0; i<users.size(); i++) {
list[i] = users.get(i);
}
return list;
}
//synchronized getter for name
public synchronized String name(){
return name;
}
//synchronized editor for history queue
public synchronized void historyAdd(String message){
//add message to tail of deque
history.add(message);
if(history.size()==20){
//if size is larger than 20, remove first element of deque to make room
history.removeFirst();
}
}
//synchronized method to return the history of the room
public synchronized String historyGet(){
//declare
String date;
String type;
String addedMsg;
String fullHistory = "type:history,messages:";
String[] messages = history.toArray(new String[0]);
for(String s : messages){
//get the type and the date since those are the ones that are always importantand always in the same spot
date = s.substring(s.lastIndexOf(",timestamp:")+11);
fullHistory += date + " :: ";
//the message always starts with "type:" so we just skip to index 5
type = s.substring(5,s.indexOf(','));
//if the message is of type text, get the message from text, otherwise, get it from message
if(type.equals("text")){
fullHistory += "[" + this.name +"] " + s.substring(s.indexOf(",nickname:")+10,s.lastIndexOf(",userID:"))+": ";
addedMsg = s.substring(s.indexOf(",text:")+6,s.lastIndexOf(",timestamp:"));
} else {
addedMsg = s.substring(s.indexOf(",message:")+9,s.lastIndexOf(",timestamp:"));
}
fullHistory += addedMsg + "\n";
}
return fullHistory;
}
//synchronized method to return the size of the history
public synchronized int historySize(){
return history.size();
}
}