01: /*
02: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.desktop.context;
06:
07: import java.util.Map;
08: import java.util.HashMap;
09:
10: public class Monitor {
11: private HashMap completedData = null;
12: private int totalNumberOfTasks = 0;
13: private boolean started = false;
14:
15: public Monitor() {
16: completedData = new HashMap();
17: }
18:
19: public synchronized boolean contains(Object o) {
20: return completedData.containsKey(o);
21: }
22:
23: public synchronized Object get(Object key) {
24: return completedData.get(key);
25: }
26:
27: public synchronized void put(Object name, Object value) {
28: completedData.put(name, value);
29: notifyIfComplete();
30: }
31:
32: public synchronized boolean isComplete() {
33: if (started && (completedData.size() >= totalNumberOfTasks)) {
34: return true;
35: }
36:
37: return false;
38: }
39:
40: public synchronized void notifyIfComplete() {
41: if (isComplete()) {
42: this .notifyAll();
43: }
44: }
45:
46: public synchronized void addTask() {
47: totalNumberOfTasks++;
48: }
49:
50: public synchronized void start() {
51: started = true;
52: notifyIfComplete();
53: }
54:
55: public synchronized Map getCompleted() {
56: return completedData;
57: }
58:
59: public boolean isDoneWaiting(int seconds) {
60: try {
61: synchronized (this ) {
62: if (!isComplete()) {
63: wait(seconds * 1000);
64: }
65: }
66: } catch (InterruptedException ie) {
67: return false;
68: }
69:
70: return isComplete();
71: }
72: }
|