1 16 package org.springframework.binding.convert; 17 18 import java.io.Serializable ; 19 20 import org.springframework.core.style.ToStringCreator; 21 import org.springframework.util.Assert; 22 23 31 public class ConversionExecutor implements Serializable { 32 33 36 private final Class sourceClass; 37 38 41 private final Class targetClass; 42 43 46 private final Converter converter; 47 48 53 public ConversionExecutor(Class sourceClass, Class targetClass, Converter converter) { 54 Assert.notNull(sourceClass, "The source class is required"); 55 Assert.notNull(targetClass, "The target class is required"); 56 Assert.notNull(converter, "The converter is required"); 57 this.sourceClass = sourceClass; 58 this.targetClass = targetClass; 59 this.converter = converter; 60 } 61 62 66 public Class getSourceClass() { 67 return sourceClass; 68 } 69 70 74 public Class getTargetClass() { 75 return targetClass; 76 } 77 78 82 public Object execute(Object source) throws ConversionException { 83 return execute(source, null); 84 } 85 86 92 public Object execute(Object source, ConversionContext context) throws ConversionException { 93 if (source != null) { 94 Assert.isInstanceOf(sourceClass, source, "Not of source type: "); 95 } 96 return converter.convert(source, targetClass, context); 97 } 98 99 public boolean equals(Object o) { 100 if (!(o instanceof ConversionExecutor)) { 101 return false; 102 } 103 ConversionExecutor other = (ConversionExecutor)o; 104 return sourceClass.equals(other.sourceClass) && targetClass.equals(other.targetClass); 105 } 106 107 public int hashCode() { 108 return sourceClass.hashCode() + targetClass.hashCode(); 109 } 110 111 public String toString() { 112 return new ToStringCreator(this).append("sourceClass", sourceClass).append("targetClass", targetClass) 113 .toString(); 114 } 115 } | Popular Tags |