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.io.Serializable;
019:
020: import org.springframework.core.style.ToStringCreator;
021: import org.springframework.util.Assert;
022: import org.springframework.webflow.execution.RequestContext;
023: import org.springframework.webflow.execution.ScopeType;
024:
025: /**
026: * A value object that defines a specification for a flow variable. Encapsulates
027: * information about the variable and the behavior necessary to create a new
028: * variable instance in a flow execution scope.
029: *
030: * @author Keith Donald
031: */
032: public abstract class FlowVariable extends AnnotatedObject implements
033: Serializable {
034:
035: /**
036: * The variable name.
037: */
038: private String name;
039:
040: /**
041: * The variable scope.
042: */
043: private ScopeType scope;
044:
045: /**
046: * Creates a new flow variable.
047: * @param name the variable name
048: * @param scope the variable scope type
049: */
050: public FlowVariable(String name, ScopeType scope) {
051: Assert.hasText(name, "The variable name is required");
052: Assert.notNull(scope, "The variable scope type is required");
053: this .name = name;
054: this .scope = scope;
055: }
056:
057: /**
058: * Returns the name of this variable.
059: */
060: public String getName() {
061: return name;
062: }
063:
064: /**
065: * Returns the scope of this variable.
066: */
067: public ScopeType getScope() {
068: return scope;
069: }
070:
071: // name and scope based equality
072:
073: public boolean equals(Object o) {
074: if (!(o instanceof FlowVariable)) {
075: return false;
076: }
077: FlowVariable other = (FlowVariable) o;
078: return name.equals(other.name) && scope.equals(other.scope);
079: }
080:
081: public int hashCode() {
082: return name.hashCode() + scope.hashCode();
083: }
084:
085: /**
086: * Creates a new instance of this flow variable in the configured scope.
087: * @param context the flow execution request context
088: */
089: public final void create(RequestContext context) {
090: scope.getScope(context).put(name, createVariableValue(context));
091: }
092:
093: /**
094: * Hook method that needs to be implemented by subclasses to calculate the
095: * value of this flow variable based on the information available in the
096: * request context.
097: * @param context the flow execution request context
098: * @return the flow variable value
099: */
100: protected abstract Object createVariableValue(RequestContext context);
101:
102: public String toString() {
103: return new ToStringCreator(this ).append("name", name).append(
104: "scope", scope).toString();
105: }
106: }
|