001: /*
002: * Copyright 2004-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.springframework.webflow.engine;
017:
018: import java.util.Iterator;
019: import java.util.LinkedList;
020: import java.util.List;
021:
022: import org.springframework.core.style.StylerUtils;
023: import org.springframework.webflow.core.collection.CollectionUtils;
024: import org.springframework.webflow.execution.RequestContext;
025:
026: /**
027: * A typed set of transitions for use internally by artifacts that can
028: * apply transition execution logic.
029: *
030: * @see TransitionableState#getTransitionSet()
031: * @see Flow#getGlobalTransitionSet()
032: *
033: * @author Keith Donald
034: */
035: public class TransitionSet {
036:
037: /**
038: * The set of transitions.
039: */
040: private List transitions = new LinkedList();
041:
042: /**
043: * Add a transition to this set.
044: * @param transition the transition to add
045: * @return true if this set's contents changed as a result of the add
046: * operation
047: */
048: public boolean add(Transition transition) {
049: if (contains(transition)) {
050: return false;
051: }
052: return transitions.add(transition);
053: }
054:
055: /**
056: * Add a collection of transition instances to this set.
057: * @param transitions the transitions to add
058: * @return true if this set's contents changed as a result of the add
059: * operation
060: */
061: public boolean addAll(Transition[] transitions) {
062: return CollectionUtils.addAllNoDuplicates(this .transitions,
063: transitions);
064: }
065:
066: /**
067: * Tests if this transition is in this set.
068: * @param transition the transition
069: * @return true if the transition is contained in this set, false otherwise
070: */
071: public boolean contains(Transition transition) {
072: return transitions.contains(transition);
073: }
074:
075: /**
076: * Remove the transition instance from this set.
077: * @param transition the transition to remove
078: * @return true if this list's contents changed as a result of the remove
079: * operation
080: */
081: public boolean remove(Transition transition) {
082: return transitions.remove(transition);
083: }
084:
085: /**
086: * Returns the size of this transition set.
087: * @return the exception handler set size
088: */
089: public int size() {
090: return transitions.size();
091: }
092:
093: /**
094: * Convert this set to a typed transition array.
095: * @return the transition set as a typed array
096: */
097: public Transition[] toArray() {
098: return (Transition[]) transitions
099: .toArray(new Transition[transitions.size()]);
100: }
101:
102: /**
103: * Returns a list of the supported transitional criteria used to match
104: * transitions in this state.
105: * @return the list of transitional criteria
106: */
107: public TransitionCriteria[] getTransitionCriterias() {
108: TransitionCriteria[] criterias = new TransitionCriteria[transitions
109: .size()];
110: int i = 0;
111: Iterator it = transitions.iterator();
112: while (it.hasNext()) {
113: criterias[i++] = ((Transition) it.next())
114: .getMatchingCriteria();
115: }
116: return criterias;
117: }
118:
119: /**
120: * Gets a transition for given flow execution request context. The first
121: * matching transition will be returned.
122: * @param context a flow execution context
123: * @return the transition, or null if no transition matches
124: */
125: public Transition getTransition(RequestContext context) {
126: Iterator it = transitions.iterator();
127: while (it.hasNext()) {
128: Transition transition = (Transition) it.next();
129: if (transition.matches(context)) {
130: return transition;
131: }
132: }
133: return null;
134: }
135:
136: /**
137: * Returns whether or not this list has a transition that will fire for
138: * given flow execution request context.
139: * @param context a flow execution context
140: */
141: public boolean hasMatchingTransition(RequestContext context) {
142: return getTransition(context) != null;
143: }
144:
145: public String toString() {
146: return StylerUtils.style(transitions);
147: }
148: }
|