From c278e1e94360c773c617e8e30945de42ca0999ef Mon Sep 17 00:00:00 2001 From: Keith Donald Date: Fri, 6 Mar 2009 20:08:33 +0000 Subject: [PATCH] convert system spi and api --- .../core/convert/ConversionException.java | 41 ++++++++ .../convert/ConversionExecutionException.java | 96 +++++++++++++++++++ .../core/convert/ConversionExecutor.java | 45 +++++++++ .../ConversionExecutorNotFoundException.java | 55 +++++++++++ .../core/convert/ConversionService.java | 88 +++++++++++++++++ .../core/convert/converter/Converter.java | 51 ++++++++++ .../convert/converter/NumberToNumber.java | 47 +++++++++ .../convert/converter/StringToBigDecimal.java | 35 +++++++ .../convert/converter/StringToBigInteger.java | 35 +++++++ .../convert/converter/StringToBoolean.java | 81 ++++++++++++++++ .../core/convert/converter/StringToByte.java | 33 +++++++ .../convert/converter/StringToCharacter.java | 36 +++++++ .../convert/converter/StringToDouble.java | 33 +++++++ .../core/convert/converter/StringToEnum.java | 34 +++++++ .../core/convert/converter/StringToFloat.java | 33 +++++++ .../convert/converter/StringToInteger.java | 33 +++++++ .../convert/converter/StringToLocale.java | 37 +++++++ .../core/convert/converter/StringToLong.java | 33 +++++++ .../core/convert/converter/StringToShort.java | 33 +++++++ .../convert/converter/SuperConverter.java | 54 +++++++++++ .../core/convert/converter/package.html | 7 ++ .../springframework/core/convert/package.html | 7 ++ 22 files changed, 947 insertions(+) create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutor.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutorNotFoundException.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/NumberToNumber.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToEnum.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/converter/package.html create mode 100644 org.springframework.core/src/main/java/org/springframework/core/convert/package.html diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java new file mode 100644 index 00000000000..5baa0079da9 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionException.java @@ -0,0 +1,41 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert; + +/** + * Base class for exceptions thrown by the convert system. + * + * @author Keith Donald + */ +public abstract class ConversionException extends RuntimeException { + + /** + * Creates a new conversion exception. + * @param message the exception message + * @param cause the cause + */ + public ConversionException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Creates a new conversion exception. + * @param message the exception message + */ + public ConversionException(String message) { + super(message); + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java new file mode 100644 index 00000000000..30a40a71497 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutionException.java @@ -0,0 +1,96 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert; + +import org.springframework.core.style.StylerUtils; + +/** + * Thrown when an attempt to execute a type conversion fails. + * + * @author Keith Donald + */ +public class ConversionExecutionException extends ConversionException { + + /** + * The value we tried to convert. Transient because we cannot guarantee that the value is Serializable. + */ + private transient Object value; + + /** + * The source type we tried to convert the value from. + */ + private Class sourceClass; + + /** + * The target type we tried to convert the value to. + */ + private Class targetClass; + + /** + * Creates a new conversion exception. + * @param value the value we tried to convert + * @param sourceClass the value's original type + * @param targetClass the value's target type + * @param cause the cause of the conversion failure + */ + public ConversionExecutionException(Object value, Class sourceClass, Class targetClass, Throwable cause) { + super(defaultMessage(value, sourceClass, targetClass, cause), cause); + this.value = value; + this.sourceClass = sourceClass; + this.targetClass = targetClass; + } + + /** + * Creates a new conversion exception. + * @param value the value we tried to convert + * @param sourceClass the value's original type + * @param targetClass the value's target type + * @param message a descriptive message of what went wrong. + */ + public ConversionExecutionException(Object value, Class sourceClass, Class targetClass, String message) { + super(message); + this.value = value; + this.sourceClass = sourceClass; + this.targetClass = targetClass; + } + + /** + * Returns the actual value we tried to convert, an instance of {@link #getSourceClass()}. + */ + public Object getValue() { + return value; + } + + /** + * Returns the source type we tried to convert the value from. + */ + public Class getSourceClass() { + return sourceClass; + } + + /** + * Returns the target type we tried to convert the value to. + */ + public Class getTargetClass() { + return targetClass; + } + + private static String defaultMessage(Object value, Class sourceClass, Class targetClass, Throwable cause) { + return "Unable to convert value " + StylerUtils.style(value) + " from type '" + sourceClass.getName() + + "' to type '" + targetClass.getName() + "'; reason = '" + cause.getMessage() + "'"; + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutor.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutor.java new file mode 100644 index 00000000000..5d8b5e65814 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutor.java @@ -0,0 +1,45 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert; + +/** + * A command object that is parameterized with the information necessary to perform a conversion of a source input to a + * target output. Encapsulates knowledge about how to convert source objects to a specific target type using a specific + * converter. + * + * @author Keith Donald + */ +public interface ConversionExecutor { + + /** + * Returns the source class of conversions performed by this executor. + * @return the source class + */ + public Class getSourceClass(); + + /** + * Returns the target class of conversions performed by this executor. + * @return the target class + */ + public Class getTargetClass(); + + /** + * Execute the conversion for the provided source object. + * @param source the source object to convert + */ + public T execute(S source) throws ConversionExecutionException; + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutorNotFoundException.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutorNotFoundException.java new file mode 100644 index 00000000000..82d485f3cf0 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionExecutorNotFoundException.java @@ -0,0 +1,55 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert; + +/** + * Thrown when a conversion executor could not be found in a conversion service. + * + * @see ConversionService#getConversionExecutor(Class, Class) + * @author Keith Donald + */ +public class ConversionExecutorNotFoundException extends ConversionException { + + private Class sourceClass; + + private Class targetClass; + + /** + * Creates a new conversion executor not found exception. + * @param sourceClass the source type requested to convert from + * @param targetClass the target type requested to convert to + * @param message a descriptive message + */ + public ConversionExecutorNotFoundException(Class sourceClass, Class targetClass, String message) { + super(message); + this.sourceClass = sourceClass; + this.targetClass = targetClass; + } + + /** + * Returns the source type requested to convert from. + */ + public Class getSourceClass() { + return sourceClass; + } + + /** + * Returns the target type requested to convert to. + */ + public Class getTargetClass() { + return targetClass; + } +} diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java new file mode 100644 index 00000000000..9735a60986c --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/ConversionService.java @@ -0,0 +1,88 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert; + +/** + * A service interface for type conversion. This is the entry point into the convert system. Call one of the + * {@link #executeConversion(Object, Class) executeConversion} operations to perform a thread-safe type conversion using + * this system. Call one of the {@link #getConversionExecutor(Class, Class) getConversionExecutor} operations to obtain + * a thread-safe {@link ConversionExecutor} command for later use. + * + * @author Keith Donald + */ +public interface ConversionService { + + /** + * Convert the source object to targetClass + * @param source the source to convert from (may be null) + * @param targetClass the target class to convert to + * @return the converted object, an instance of the targetClass, or null if a null source + * was provided + * @throws ConversionExecutorNotFoundException if no suitable conversion executor could be found to convert the + * source to an instance of targetClass + * @throws ConversionException if an exception occurred during the conversion process + */ + public T executeConversion(Object source, Class targetClass) throws ConversionExecutorNotFoundException, + ConversionException; + + /** + * Convert the source object to targetClass using a custom converter. + * @param converterId the id of the custom converter, which must be registered with this conversion service and + * capable of converting to the targetClass + * @param source the source to convert from (may be null) + * @param targetClass the target class to convert to + * @return the converted object, an instance of the targetClass, or null if a null source + * was provided + * @throws ConversionExecutorNotFoundException if no suitable conversion executor could be found to convert the + * source to an instance of targetClass + * @throws ConversionException if an exception occurred during the conversion process + */ + public T executeConversion(String converterId, Object source, Class targetClass) + throws ConversionExecutorNotFoundException, ConversionException; + + /** + * Get a ConversionExecutor capable of converting objects from sourceClass to targetClass. + * The returned ConversionExecutor is thread-safe and may safely be cached for later use by client code. + * @param sourceClass the source class to convert from (required) + * @param targetClass the target class to convert to (required) + * @return the executor that can execute instance type conversion, never null + * @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found + */ + public ConversionExecutor getConversionExecutor(Class sourceClass, Class targetClass) + throws ConversionExecutorNotFoundException; + + /** + * Get a ConversionExecutor that uses a custom converter to capable convert objects from sourceClass to + * targetClass. The returned ConversionExecutor is thread-safe and may safely be cached for use in + * client code. + * @param converterId the id of the custom converter, which must be registered with this conversion service and + * capable of converting from sourceClass to targetClass (required) + * @param sourceClass the source class to convert from (required) + * @param targetClass the target class to convert to (required) + * @return the executor that can execute instance type conversion, never null + * @throws ConversionExecutorNotFoundException when no suitable conversion executor could be found + */ + public ConversionExecutor getConversionExecutor(String converterId, Class sourceClass, + Class targetClass) throws ConversionExecutorNotFoundException; + + /** + * Lookup a class by its well-known alias. For example, long for java.lang.Long + * @param alias the class alias + * @return the class, or null if no alias exists + */ + public Class getClassForAlias(String alias); + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java new file mode 100644 index 00000000000..112cad0d161 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/Converter.java @@ -0,0 +1,51 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConversionService; + +/** + * A converter converts a source object of type S to a target type of type T and back. + *

+ * Implementations of this interface are thread-safe and can be shared. Converters are typically registered with and + * accessed through a {@link ConversionService}. + *

+ * @author Keith Donald + */ +public interface Converter { + + /** + * Convert the source S to target type T. + * @param source the source object to convert, which must be an instance of S + * @return the converted object, which must be an instance of T + * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion + * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type + * conversion error context + */ + public T convert(S source) throws Exception; + + /** + * Convert the target T back to source type S. + * @param target the target object to convert, which must be an instance of T + * @return the converted object, which must be an instance of S + * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion + * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type + * conversion error context + */ + public S convertBack(T target) throws Exception; + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/NumberToNumber.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/NumberToNumber.java new file mode 100644 index 00000000000..5a09ce973b7 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/NumberToNumber.java @@ -0,0 +1,47 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +import org.springframework.util.NumberUtils; + +/** + *Converts from any JDK-standard Number implementation to any other JDK-standard Number implementation. + * + * Support Number classes include byte, short, integer, float, double, long, big integer, big decimal. This class + * delegates to {@link NumberUtils#convertNumberToTargetClass(Number, Class)} to perform the conversion. + * + * @see java.lang.Byte + * @see java.lang.Short + * @see java.lang.Integer + * @see java.lang.Long + * @see java.math.BigInteger + * @see java.lang.Float + * @see java.lang.Double + * @see java.math.BigDecimal + * + * @author Keith Donald + */ +public class NumberToNumber implements SuperConverter { + + public N convert(Number source, Class targetClass) throws Exception { + return NumberUtils.convertNumberToTargetClass(source, targetClass); + } + + public Number convertBack(Number target) throws Exception { + return target; + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java new file mode 100644 index 00000000000..de4ea108a51 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigDecimal.java @@ -0,0 +1,35 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +import java.math.BigDecimal; + +/** + * Converts a String to a BigDecimal and back. + * + * @author Keith Donald + */ +public class StringToBigDecimal implements Converter { + + public BigDecimal convert(String source) throws Exception { + return new BigDecimal(source); + } + + public String convertBack(BigDecimal target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java new file mode 100644 index 00000000000..a6fe0515316 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBigInteger.java @@ -0,0 +1,35 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +import java.math.BigInteger; + +/** + * Converts a String to a BigInteger and back. + * + * @author Keith Donald + */ +public class StringToBigInteger implements Converter { + + public BigInteger convert(String source) throws Exception { + return new BigInteger(source); + } + + public String convertBack(BigInteger target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java new file mode 100644 index 00000000000..39574ed781b --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToBoolean.java @@ -0,0 +1,81 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts String to a Boolean and back. + * + * @author Keith Donald + */ +public class StringToBoolean implements Converter { + + private static final String VALUE_TRUE = "true"; + + private static final String VALUE_FALSE = "false"; + + private String trueString; + + private String falseString; + + /** + * Create a StringToBoolean converter. + */ + public StringToBoolean() { + } + + /** + * Create a StringToBoolean converter configured with specific values for true and false strings. + * @param trueString special true string to use + * @param falseString special false string to use + */ + public StringToBoolean(String trueString, String falseString) { + this.trueString = trueString; + this.falseString = falseString; + } + + public Boolean convert(String source) throws Exception { + if (trueString != null && source.equals(trueString)) { + return Boolean.TRUE; + } else if (falseString != null && source.equals(falseString)) { + return Boolean.FALSE; + } else if (trueString == null && source.equals(VALUE_TRUE)) { + return Boolean.TRUE; + } else if (falseString == null && source.equals(VALUE_FALSE)) { + return Boolean.FALSE; + } else { + throw new IllegalArgumentException("Invalid boolean value [" + source + "]"); + } + } + + public String convertBack(Boolean target) throws Exception { + if (Boolean.TRUE.equals(target)) { + if (trueString != null) { + return trueString; + } else { + return VALUE_TRUE; + } + } else if (Boolean.FALSE.equals(target)) { + if (falseString != null) { + return falseString; + } else { + return VALUE_FALSE; + } + } else { + throw new IllegalArgumentException("Invalid boolean value [" + target + "]"); + } + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java new file mode 100644 index 00000000000..3501b2f7176 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToByte.java @@ -0,0 +1,33 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts a String to a Byte and back. + * + * @author Keith Donald + */ +public class StringToByte implements Converter { + + public Byte convert(String source) throws Exception { + return new Byte(source); + } + + public String convertBack(Byte target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java new file mode 100644 index 00000000000..afed56b390b --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToCharacter.java @@ -0,0 +1,36 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts a String to a Character and back. + * + * @author Keith Donald + */ +public class StringToCharacter implements Converter { + + public Character convert(String source) throws Exception { + if (source.length() > 1) { + throw new IllegalArgumentException("To be a Character the String '" + source + "' must have a length of 1"); + } + return new Character(source.charAt(0)); + } + + public String convertBack(Character target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java new file mode 100644 index 00000000000..bee007c9c1f --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToDouble.java @@ -0,0 +1,33 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts a String to a Double and back. + * + * @author Keith Donald + */ +public class StringToDouble implements Converter { + + public Double convert(String source) throws Exception { + return Double.valueOf(source); + } + + public String convertBack(Double target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToEnum.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToEnum.java new file mode 100644 index 00000000000..e4b32444532 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToEnum.java @@ -0,0 +1,34 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts from a String to Enum and back. + * + * @author Scott Andrews + */ +@SuppressWarnings("unchecked") +public class StringToEnum implements SuperConverter { + + public E convert(String source, Class targetClass) throws Exception { + return Enum.valueOf(targetClass, source); + } + + public String convertBack(Enum target) throws Exception { + return target.name(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java new file mode 100644 index 00000000000..965848471f9 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToFloat.java @@ -0,0 +1,33 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts a String to an Short using {@link Short#valueOf(String)}. + * + * @author Keith Donald + */ +public class StringToFloat implements Converter { + + public Float convert(String source) throws Exception { + return Float.valueOf(source); + } + + public String convertBack(Float target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java new file mode 100644 index 00000000000..22fe1f3c4b0 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToInteger.java @@ -0,0 +1,33 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts a String to an Integer using {@link Integer#valueOf(String)}. + * + * @author Keith Donald + */ +public class StringToInteger implements Converter { + + public Integer convert(String source) throws Exception { + return Integer.valueOf(source); + } + + public String convertBack(Integer target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java new file mode 100644 index 00000000000..566d3017a36 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLocale.java @@ -0,0 +1,37 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +import java.util.Locale; + +import org.springframework.util.StringUtils; + +/** + * Converts a String to a Locale using {@link StringUtils#parseLocaleString(String)}. + * + * @author Keith Donald + */ +public class StringToLocale implements Converter { + + public Locale convert(String source) throws Exception { + return StringUtils.parseLocaleString(source); + } + + public String convertBack(Locale target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java new file mode 100644 index 00000000000..1b6c28c3040 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToLong.java @@ -0,0 +1,33 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts a String to an Long using {@link Long#valueOf(String)}. + * + * @author Keith Donald + */ +public class StringToLong implements Converter { + + public Long convert(String source) throws Exception { + return Long.valueOf(source); + } + + public String convertBack(Long target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java new file mode 100644 index 00000000000..7e3a4d59af2 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/StringToShort.java @@ -0,0 +1,33 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +/** + * Converts a String to an Short using {@link Short#valueOf(String)}. + * + * @author Keith Donald + */ +public class StringToShort implements Converter { + + public Short convert(String source) throws Exception { + return Short.valueOf(source); + } + + public String convertBack(Short target) throws Exception { + return target.toString(); + } + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java new file mode 100644 index 00000000000..0bd39420208 --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/SuperConverter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2004-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.core.convert.converter; + +import org.springframework.core.convert.ConversionException; +import org.springframework.core.convert.ConversionService; + +/** + * A super converter converts a source object of type S to a target type of type AT and back, where AT is equal to or a + * subclass of T. + *

+ * Implementations of this interface are thread-safe and can be shared. Converters are typically registered with and + * accessed through a {@link ConversionService}. + *

+ * @author Keith Donald + */ +public interface SuperConverter { + + /** + * Convert the source S to an instance of AT. + * @param source the source object to convert, which must be an instance of S + * @param actualTargetClass the actual target class to convert to (AT), which must be equal to or a specialization + * of T. + * @return the converted object, which must be an instance of AT + * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion + * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type + * conversion error context + */ + public AT convert(S source, Class actualTargetClass) throws Exception; + + /** + * Convert the target T to an instance of S. + * @param target the target object to convert, which must be an instance of T. + * @return the converted object, which must be an instance of S. + * @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion + * system will handle wrapping the failure in a {@link ConversionException} that provides a consistent type + * conversion error context + */ + public S convertBack(T target) throws Exception; + +} \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/converter/package.html b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/package.html new file mode 100644 index 00000000000..59e9fba652b --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/converter/package.html @@ -0,0 +1,7 @@ + + +

+User Converter API and default Converter implementations. +

+ + \ No newline at end of file diff --git a/org.springframework.core/src/main/java/org/springframework/core/convert/package.html b/org.springframework.core/src/main/java/org/springframework/core/convert/package.html new file mode 100644 index 00000000000..c2fb2e8425b --- /dev/null +++ b/org.springframework.core/src/main/java/org/springframework/core/convert/package.html @@ -0,0 +1,7 @@ + + +

+Type conversion system SPI. +

+ + \ No newline at end of file