- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
public class AuthorizedUserObservervable implements Runnable {
private long sleepTime = 1000;
private HashMap<AuthorizedUserObserver, HashMap<AuthorizedUser, Integer>> observers = new HashMap<AuthorizedUserObserver, HashMap<AuthorizedUser, Integer>>();
private void setSleepTime(long sleepTime) {
this.sleepTime = sleepTime;
}
public void add(AuthorizedUserObserver auo) {
HashMap<AuthorizedUser, Integer> userCache = new HashMap<AuthorizedUser, Integer>();
for (AuthorizedUser user : auo.getUsers()) {
userCache.put(user, null);
}
observers.put(auo, userCache);
}
public void beginObservation() {
new Thread(this).start();
}
@Override
public void run() {
while (true) {
try {
for (Map.Entry<AuthorizedUserObserver, HashMap<AuthorizedUser, Integer>> observer : observers.entrySet()) {
for (Map.Entry<AuthorizedUser, Integer> user : observer.getValue().entrySet()) {
int newPostsCount = user.getKey().getNewPostsCount();
if (user.getValue() == null || newPostsCount != user.getValue()) {
HashMap<AuthorizedUser, Integer> userCache = new HashMap<AuthorizedUser, Integer>();
userCache.put(user.getKey(), newPostsCount);
observers.put(observer.getKey(), userCache);
observer.getKey().notify(user.getKey(), newPostsCount);
}
}
}
Thread.sleep(sleepTime);
} catch (InterruptedException ex) {
Logger.getLogger(UserObservervable.class.getName()).log(Level.SEVERE, null, ex);
} catch (AuthenticationException e) {
Thread.currentThread().stop(e);
}
}
}
}
guest 15.11.2011 23:30 # 0
tir 16.11.2011 07:42 # 0