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.binding.convert;
017:
018: import org.springframework.core.style.ToStringCreator;
019: import org.springframework.util.Assert;
020:
021: /**
022: * A command object that is parameterized with the information necessary to
023: * perform a conversion of a source input to a target output.
024: * <p>
025: * Specifically, encapsulates knowledge about how to convert source objects to a
026: * specific target type using a specific converter.
027: *
028: * @author Keith Donald
029: */
030: public class ConversionExecutor {
031:
032: /**
033: * The source value type this executor will attempt to convert from.
034: */
035: private final Class sourceClass;
036:
037: /**
038: * The target value type this executor will attempt to convert to.
039: */
040: private final Class targetClass;
041:
042: /**
043: * The converter that will perform the conversion.
044: */
045: private final Converter converter;
046:
047: /**
048: * Creates a conversion executor.
049: * @param sourceClass the source type that the converter will convert from
050: * @param targetClass the target type that the converter will convert to
051: * @param converter the converter that will perform the conversion
052: */
053: public ConversionExecutor(Class sourceClass, Class targetClass,
054: Converter converter) {
055: Assert.notNull(sourceClass, "The source class is required");
056: Assert.notNull(targetClass, "The target class is required");
057: Assert.notNull(converter, "The converter is required");
058: this .sourceClass = sourceClass;
059: this .targetClass = targetClass;
060: this .converter = converter;
061: }
062:
063: /**
064: * Returns the source class of conversions performed by this executor.
065: * @return the source class
066: */
067: public Class getSourceClass() {
068: return sourceClass;
069: }
070:
071: /**
072: * Returns the target class of conversions performed by this executor.
073: * @return the target class
074: */
075: public Class getTargetClass() {
076: return targetClass;
077: }
078:
079: /**
080: * Returns the converter that will perform the conversion.
081: * @return the converter
082: */
083: public Converter getConverter() {
084: return converter;
085: }
086:
087: /**
088: * Execute the conversion for the provided source object.
089: * @param source the source object to convert
090: */
091: public Object execute(Object source) throws ConversionException {
092: return execute(source, null);
093: }
094:
095: /**
096: * Execute the conversion for the provided source object.
097: * @param source the source object to convert
098: * @param context the conversion context, useful for influencing the
099: * behavior of the converter
100: */
101: public Object execute(Object source, ConversionContext context)
102: throws ConversionException {
103: if (getTargetClass().isInstance(source)) {
104: // source is already assignment compatible with target class
105: return source;
106: }
107: if (source != null && !getSourceClass().isInstance(source)) {
108: throw new ConversionException(getSourceClass(), source,
109: getTargetClass(), "Source object '" + source
110: + "' is expected to be an instance of "
111: + getSourceClass());
112: }
113: return converter.convert(source, targetClass, context);
114: }
115:
116: public boolean equals(Object o) {
117: if (!(o instanceof ConversionExecutor)) {
118: return false;
119: }
120: ConversionExecutor other = (ConversionExecutor) o;
121: return sourceClass.equals(other.sourceClass)
122: && targetClass.equals(other.targetClass);
123: }
124:
125: public int hashCode() {
126: return sourceClass.hashCode() + targetClass.hashCode();
127: }
128:
129: public String toString() {
130: return new ToStringCreator(this ).append("sourceClass",
131: sourceClass).append("targetClass", targetClass)
132: .toString();
133: }
134: }
|