-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRealTimeFeedObserver.java
More file actions
50 lines (41 loc) · 1.37 KB
/
RealTimeFeedObserver.java
File metadata and controls
50 lines (41 loc) · 1.37 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
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Real-time feed observer implementation
* Design Pattern: Observer Pattern
*/
public class RealTimeFeedObserver implements FeedObserver {
private String userId;
private BlockingQueue<Post> feedQueue;
private volatile boolean active;
public RealTimeFeedObserver(String userId) {
this.userId = userId;
this.feedQueue = new LinkedBlockingQueue<>();
this.active = true;
}
@Override
public void onNewPost(Post post, String followerId) {
if (active && userId.equals(followerId)) {
feedQueue.offer(post);
System.out.println("[REALTIME] User " + userId + " received new post from " + post.getUserId() + ": " + post.getContent());
}
}
@Override
public void onPostDeleted(String postId, String followerId) {
if (active && userId.equals(followerId)) {
System.out.println("[REALTIME] User " + userId + " notified: Post " + postId + " was deleted");
}
}
public Post getNextPost() throws InterruptedException {
return feedQueue.take();
}
public Post pollPost() {
return feedQueue.poll();
}
public void stop() {
this.active = false;
}
public boolean isActive() {
return active;
}
}