-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
144 lines (123 loc) · 3.22 KB
/
Copy pathUser.java
File metadata and controls
144 lines (123 loc) · 3.22 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
public class User extends UserManager implements Subject
{
private String group; //Can only belong to one.
private static HashMap<String, User> users = new HashMap<String, User>();
public static HashMap<String, User> getUsers() {
return new HashMap<>(users);
}
private ArrayList<Observer> followers;
private ArrayList<String> following;
private ArrayList<String> tweets;
private NewsFeed nf;
private long creationTime;
private long lastUpdateTime;
public User(String id, String group)
{
if(!users.containsKey(id))
{
this.id = id;
followers = new ArrayList<Observer>();
following = new ArrayList<String>();
tweets = new ArrayList<String>();
this.group = group;
nf = new NewsFeed();
this.register(nf);
users.put(id, this);
}
else
{
System.out.println("ID already exists!"); //Filler, will have dialog pop up instead
}
this.creationTime = System.currentTimeMillis();
}
public NewsFeed getFeed()
{
return nf;
}
public static boolean exists(String uid)
{
return users.containsKey(uid);
}
public void follow(String id)
{
users.get(id).register(nf);
following.add(id);
for(String s : users.get(id).getTweets())
{
nf.update(s, users.get(id));
}
}
public void joinGroup(String id)
{
group = id;
}
public String getGroup()
{
return group;
}
public ArrayList<String> getTweets()
{
return tweets;
}
public void tweet(String msg)
{
tweets.add(msg);
notifyObs();
for (Observer follower : followers) {
if (follower instanceof User) {
((User) follower).lastUpdateTime = this.lastUpdateTime;
}
}
}
@Override
public void register(Observer obj)
{
if(obj == null)
{
System.out.println("Null observer");
}
if(!followers.contains(obj))
{
followers.add(obj);
}
}
@Override
public void notifyObs()
{
for(Observer obs : followers)
{
obs.update(tweets.get(tweets.size()-1), this);
}
}
public boolean isFollowing(String id)
{
return following.contains(id);
}
@Override
public Object getUpdate(Observer obj) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
@Override
public void add(UserManager um) {
System.out.println("Can't add to leaf.");
}
@Override
public ArrayList<UserManager> getMembers() {
return null;
}
public ArrayList<String> getFollowing()
{
return following;
}
@Override
public void accept(InfoVisitor v)
{
v.visit(this);
}
public long getLastUpdateTime() {
return lastUpdateTime;
}
}