-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocialMediaNetwork.java
More file actions
266 lines (232 loc) · 8.7 KB
/
SocialMediaNetwork.java
File metadata and controls
266 lines (232 loc) · 8.7 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
/**
* Main Social Media Network System
* Design Patterns: Observer, Factory, Repository, Strategy
* Thread-safe and scalable implementation
*/
public class SocialMediaNetwork implements FeedSubject {
private final SocialMediaRepository repository;
private final FeedStrategy feedStrategy;
private final Map<String, List<FeedObserver>> observers; // userId -> List of observers
private final ExecutorService executorService;
private final ReadWriteLock lock;
public SocialMediaNetwork() {
this.repository = new SocialMediaRepository();
this.feedStrategy = new RecentFeedStrategy(10);
this.observers = new ConcurrentHashMap<>();
this.executorService = Executors.newFixedThreadPool(10);
this.lock = new ReentrantReadWriteLock();
}
// Create a new user
public void createUser(String userId, String name) {
lock.writeLock().lock();
try {
if (repository.userExists(userId)) {
throw new IllegalArgumentException("User already exists: " + userId);
}
User user = UserFactory.createUser(userId, name);
repository.addUser(user);
observers.putIfAbsent(userId, new CopyOnWriteArrayList<>());
} finally {
lock.writeLock().unlock();
}
}
// Upload a post
public String uploadPost(String userId, String content) {
lock.readLock().lock();
try {
if (!repository.userExists(userId)) {
throw new IllegalArgumentException("User " + userId + " does not exist");
}
} finally {
lock.readLock().unlock();
}
Post post = PostFactory.createPost(userId, content);
lock.writeLock().lock();
try {
if (repository.postExists(post.getPostId())) {
throw new IllegalArgumentException("Post already posted");
}
repository.addPost(post);
} finally {
lock.writeLock().unlock();
}
// Notify observers asynchronously
notifyObserversAsync(post, userId);
return post.getPostId();
}
// Delete a post
public boolean deletePost(String userId, String postId) {
lock.writeLock().lock();
try {
Post post = repository.getPost(postId);
if (post == null) {
return false;
}
if (!post.getUserId().equals(userId)) {
throw new IllegalArgumentException("User can only delete their own posts");
}
repository.removePost(postId);
// Notify observers asynchronously
notifyPostDeletedAsync(postId, userId);
return true;
} finally {
lock.writeLock().unlock();
}
}
// Follow a user
public void followUser(String userId, String followUserId) {
lock.readLock().lock();
try {
if (!repository.userExists(userId)) {
throw new IllegalArgumentException("User does not exist: " + userId);
}
if (!repository.userExists(followUserId)) {
throw new IllegalArgumentException("User to follow does not exist: " + followUserId);
}
if (userId.equals(followUserId)) {
throw new IllegalArgumentException("User cannot follow themselves");
}
} finally {
lock.readLock().unlock();
}
User user = repository.getUser(userId);
if (user != null) {
user.follow(followUserId);
}
}
// Unfollow a user
public void unfollowUser(String userId, String unfollowUserId) {
lock.readLock().lock();
try {
if (!repository.userExists(userId)) {
throw new IllegalArgumentException("User does not exist: " + userId);
}
} finally {
lock.readLock().unlock();
}
User user = repository.getUser(userId);
if (user != null) {
user.unfollow(unfollowUserId);
}
}
// Get feed - recent 10 posts from user's account and followings' accounts
public List<Post> getFeed(String userId) {
lock.readLock().lock();
try {
if (!repository.userExists(userId)) {
throw new IllegalArgumentException("User does not exist: " + userId);
}
return feedStrategy.generateFeed(userId, repository);
} finally {
lock.readLock().unlock();
}
}
// Get user's posts
public List<Post> getUserPosts(String userId) {
lock.readLock().lock();
try {
if (!repository.userExists(userId)) {
throw new IllegalArgumentException("User does not exist: " + userId);
}
List<String> postIds = repository.getUserPostIds(userId);
return postIds.stream()
.map(repository::getPost)
.filter(Objects::nonNull)
.sorted((p1, p2) -> p2.getTimestamp().compareTo(p1.getTimestamp()))
.collect(Collectors.toList());
} finally {
lock.readLock().unlock();
}
}
// Get user info
public User getUser(String userId) {
lock.readLock().lock();
try {
return repository.getUser(userId);
} finally {
lock.readLock().unlock();
}
}
// Observer Pattern Implementation
@Override
public void registerObserver(FeedObserver observer, String userId) {
observers.computeIfAbsent(userId, k -> new CopyOnWriteArrayList<>()).add(observer);
}
@Override
public void unregisterObserver(FeedObserver observer, String userId) {
List<FeedObserver> userObservers = observers.get(userId);
if (userObservers != null) {
userObservers.remove(observer);
}
}
@Override
public void notifyObservers(Post post, String authorId) {
User author = repository.getUser(authorId);
if (author == null) return;
// Get all followers of the author
Set<String> followers = new HashSet<>();
for (User user : repository.getAllUsers().values()) {
if (user.isFollowing(authorId)) {
followers.add(user.getUserId());
}
}
// Also notify the author themselves
followers.add(authorId);
// Notify all observers
for (String followerId : followers) {
List<FeedObserver> userObservers = observers.get(followerId);
if (userObservers != null) {
for (FeedObserver observer : userObservers) {
observer.onNewPost(post, followerId);
}
}
}
}
@Override
public void notifyPostDeleted(String postId, String authorId) {
User author = repository.getUser(authorId);
if (author == null) return;
// Get all followers of the author
Set<String> followers = new HashSet<>();
for (User user : repository.getAllUsers().values()) {
if (user.isFollowing(authorId)) {
followers.add(user.getUserId());
}
}
// Also notify the author themselves
followers.add(authorId);
// Notify all observers
for (String followerId : followers) {
List<FeedObserver> userObservers = observers.get(followerId);
if (userObservers != null) {
for (FeedObserver observer : userObservers) {
observer.onPostDeleted(postId, followerId);
}
}
}
}
// Async notification methods
private void notifyObserversAsync(Post post, String authorId) {
executorService.submit(() -> notifyObservers(post, authorId));
}
private void notifyPostDeletedAsync(String postId, String authorId) {
executorService.submit(() -> notifyPostDeleted(postId, authorId));
}
// Shutdown executor service
public void shutdown() {
executorService.shutdown();
try {
if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} catch (InterruptedException e) {
executorService.shutdownNow();
Thread.currentThread().interrupt();
}
}
}