KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > webflow > engine > support > TransitionCriteriaChain


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.engine.support;
17
18 import java.util.Arrays JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.springframework.core.style.ToStringCreator;
24 import org.springframework.webflow.engine.AnnotatedAction;
25 import org.springframework.webflow.engine.TransitionCriteria;
26 import org.springframework.webflow.engine.WildcardTransitionCriteria;
27 import org.springframework.webflow.execution.RequestContext;
28
29 /**
30  * An ordered chain of <code>TransitionCriteria</code>. Iterates over each element
31  * in the chain, continues until one returns false or the list is exhausted. So
32  * in effect it will do a logical AND between the contained criteria.
33  *
34  * @author Keith Donald
35  */

36 public class TransitionCriteriaChain implements TransitionCriteria {
37
38     /**
39      * The ordered chain of TransitionCriteria objects.
40      */

41     private List JavaDoc criteriaChain = new LinkedList JavaDoc();
42
43     /**
44      * Creates an initially empty transition criteria chain.
45      * @see #add(TransitionCriteria)
46      */

47     public TransitionCriteriaChain() {
48     }
49
50     /**
51      * Creates a transition criteria chain with the specified criteria.
52      * @param criteria the criteria
53      */

54     public TransitionCriteriaChain(TransitionCriteria[] criteria) {
55         criteriaChain.addAll(Arrays.asList(criteria));
56     }
57
58     /**
59      * Add given criteria object to the end of the chain.
60      * @param criteria the criteria
61      * @return this object, so multiple criteria can be added in a single
62      * statement
63      */

64     public TransitionCriteriaChain add(TransitionCriteria criteria) {
65         this.criteriaChain.add(criteria);
66         return this;
67     }
68
69     public boolean test(RequestContext context) {
70         Iterator JavaDoc it = criteriaChain.iterator();
71         while (it.hasNext()) {
72             TransitionCriteria criteria = (TransitionCriteria)it.next();
73             if (!criteria.test(context)) {
74                 return false;
75             }
76         }
77         return true;
78     }
79
80     public String JavaDoc toString() {
81         return new ToStringCreator(this).append("criteriaChain", criteriaChain).toString();
82     }
83
84     // static helpers
85

86     /**
87      * Create a transition criteria chain chaining given list of actions.
88      * @param actions the actions (and their execution properties) to chain together
89      */

90     public static TransitionCriteria criteriaChainFor(AnnotatedAction[] actions) {
91         if (actions == null || actions.length == 0) {
92             return WildcardTransitionCriteria.INSTANCE;
93         }
94         TransitionCriteriaChain chain = new TransitionCriteriaChain();
95         for (int i = 0; i < actions.length; i++) {
96             chain.add(new ActionTransitionCriteria(actions[i]));
97         }
98         return chain;
99     }
100 }
Popular Tags