KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > execution > repository > continuation > FlowExecutionContinuationGroup


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.webflow.execution.repository.continuation;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.Map JavaDoc;
22
23 /**
24  * A group of flow execution continuations. Simple typed data structure backed
25  * by a map and linked list. Supports expelling the oldest continuation once a
26  * maximum group size is met.
27  *
28  * @author Keith Donald
29  */

30 class FlowExecutionContinuationGroup implements Serializable JavaDoc {
31
32     /**
33      * A map of continuations; the key is the continuation id, and the value is
34      * the {@link FlowExecutionContinuation} object.
35      */

36     private Map JavaDoc continuations = new HashMap JavaDoc();
37
38     /**
39      * An ordered list of continuation ids. Each continuation id represents an
40      * pointer to a continuation in the map. The first element is the oldest
41      * continuation and the last is the youngest.
42      */

43     private LinkedList JavaDoc continuationIds = new LinkedList JavaDoc();
44
45     /**
46      * The maximum number of continuations allowed in this group.
47      */

48     private int maxContinuations = -1;
49
50     /**
51      * Creates a new flow execution continuation group.
52      * @param maxContinuations the maximum number of continuations that can be
53      * stored in this group, -1 for unlimited
54      */

55     public FlowExecutionContinuationGroup(int maxContinuations) {
56         this.maxContinuations = maxContinuations;
57     }
58     
59     /**
60      * Returns the count of continuations in this group.
61      */

62     public int getContinuationCount() {
63         return continuationIds.size();
64     }
65
66     /**
67      * Returns the continuation with the provided <code>id</code>, or
68      * <code>null</code> if no such continuation exists with that id.
69      * @param id the continuation id
70      * @return the continuation
71      * @throws ContinuationNotFoundException if the id does not match a
72      * continuation in this group
73      */

74     public FlowExecutionContinuation get(Serializable JavaDoc id) throws ContinuationNotFoundException {
75         FlowExecutionContinuation continuation = (FlowExecutionContinuation)continuations.get(id);
76         if (continuation == null) {
77             throw new ContinuationNotFoundException(id);
78         }
79         return continuation;
80     }
81
82     /**
83      * Add a flow execution continuation with given id to this group.
84      * @param continuationId the continuation id
85      * @param continuation the continuation
86      */

87     public void add(Serializable JavaDoc continuationId, FlowExecutionContinuation continuation) {
88         continuations.put(continuationId, continuation);
89         continuationIds.add(continuationId);
90         // remove the oldest continuation if them maximium number of
91
// continuations has been exceeded
92
if (maxExceeded()) {
93             removeOldestContinuation();
94         }
95     }
96
97     /**
98      * Has the maximum number of allowed continuations in this group been exceeded?
99      */

100     private boolean maxExceeded() {
101         return maxContinuations > 0 && continuationIds.size() > maxContinuations;
102     }
103
104     /**
105      * Remove the olders continuation from this group.
106      */

107     private void removeOldestContinuation() {
108         continuations.remove(continuationIds.removeFirst());
109     }
110 }
Popular Tags