1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.proxy;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import lotus.domino.Database;
29 import lotus.domino.Document;
30 import lotus.domino.DocumentCollection;
31 import lotus.domino.Item;
32 import lotus.domino.Session;
33 import lotus.domino.View;
34 import lotus.domino.ViewEntry;
35 import lotus.domino.ViewEntryCollection;
36 import de.bea.domingo.queue.Queue;
37
38 /***
39 * Queue of Notes objects to be recycled.
40 *
41 * <p>Notes objects are queued with a priority defined by their class in this
42 * decreasing order:<p/>
43 * <p><code>Session</code>, <code>Database</code>, <code>View</code>,
44 * <code>Collection</code>, <code>Document</code>, <code>Entry</code>,
45 * <code>Item</code>, <code><i>others</i></code></p>
46 *
47 * @author <a href="mailto:kriede@users.sourceforge.net">Kurt Riede</a>
48 */
49 public final class NotesRecycleQueue implements Queue {
50
51 /*** Reference to a session to be recycled. */
52 private Session session = null;
53
54 /*** List of references to databases to be recycled. */
55 private List databaseQueue = new ArrayList();
56
57 /*** List of references to views to be recycled. */
58 private List viewQueue = new ArrayList();
59
60 /*** List of references to collection to be recycled. */
61 private List collectionQueue = new ArrayList();
62
63 /*** List of references to documents to be recycled. */
64 private List documentQueue = new ArrayList();
65
66 /*** List of references to view entries to be recycled. */
67 private List entryQueue = new ArrayList();
68
69 /*** List of references to items to be recycled. */
70 private List itemQueue = new ArrayList();
71
72 /*** List of references to other objects to be recycled. */
73 private List otherQueue = new ArrayList();
74
75 /***
76 * Constructor.
77 */
78 public NotesRecycleQueue() {
79 super();
80 }
81
82 /***
83 * Returns the number of objects in the queue.
84 *
85 * @return number of objects in the queue
86 */
87 public synchronized int size() {
88 return databaseQueue.size() + viewQueue.size() + collectionQueue.size() + documentQueue.size()
89 + entryQueue.size() + itemQueue.size() + otherQueue.size() + (session != null ? 1 : 0);
90 }
91
92 /***
93 * Checks is the queue is empty or not.
94 *
95 * @return <code>true</code> if the queue is empty, else <code>false</code>
96 */
97 public synchronized boolean isEmpty() {
98 return size() == 0;
99 }
100
101 /***
102 * Enqueues an object to the queue.
103 *
104 * @param obj the object to enqueue
105 */
106 public synchronized void enqueue(final Object obj) {
107 if (obj instanceof Session) {
108 session = (Session) obj;
109 } else if (obj instanceof Database) {
110 databaseQueue.add(obj);
111 } else if (obj instanceof View) {
112 viewQueue.add(obj);
113 } else if (obj instanceof DocumentCollection || obj instanceof ViewEntryCollection) {
114 collectionQueue.add(obj);
115 } else if (obj instanceof Document) {
116 documentQueue.add(obj);
117 } else if (obj instanceof ViewEntry) {
118 entryQueue.add(obj);
119 } else if (obj instanceof Item) {
120 itemQueue.add(obj);
121 } else {
122 otherQueue.add(obj);
123 }
124 }
125
126 /***
127 * Dequeues an object from the queue.
128 *
129 * @return next object from the queue
130 * or <code>null</code> if the queue is empty
131 */
132 public synchronized Object dequeue() {
133 if (!otherQueue.isEmpty()) {
134 return otherQueue.remove(otherQueue.size() - 1);
135 } else if (!itemQueue.isEmpty()) {
136 return itemQueue.remove(itemQueue.size() - 1);
137 } else if (!entryQueue.isEmpty()) {
138 return entryQueue.remove(entryQueue.size() - 1);
139 } else if (!documentQueue.isEmpty()) {
140 return documentQueue.remove(documentQueue.size() - 1);
141 } else if (!itemQueue.isEmpty()) {
142 return itemQueue.remove(itemQueue.size() - 1);
143 } else if (!collectionQueue.isEmpty()) {
144 return collectionQueue.remove(collectionQueue.size() - 1);
145 } else if (!viewQueue.isEmpty()) {
146 return viewQueue.remove(viewQueue.size() - 1);
147 } else if (!databaseQueue.isEmpty()) {
148 return databaseQueue.remove(databaseQueue.size() - 1);
149 } else if (session != null) {
150 final Object obj = session;
151 session = null;
152 return obj;
153 } else {
154 return null;
155 }
156 }
157 }