convert system spi and api
This commit is contained in:
parent
f83019afed
commit
c278e1e943
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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() + "'";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<S, T> {
|
||||
|
||||
/**
|
||||
* Returns the source class of conversions performed by this executor.
|
||||
* @return the source class
|
||||
*/
|
||||
public Class<S> getSourceClass();
|
||||
|
||||
/**
|
||||
* Returns the target class of conversions performed by this executor.
|
||||
* @return the target class
|
||||
*/
|
||||
public Class<T> getTargetClass();
|
||||
|
||||
/**
|
||||
* Execute the conversion for the provided source object.
|
||||
* @param source the source object to convert
|
||||
*/
|
||||
public T execute(S source) throws ConversionExecutionException;
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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 <code>targetClass</code>
|
||||
* @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 <code>targetClass</code>, or <code>null</code> 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> T executeConversion(Object source, Class<T> targetClass) throws ConversionExecutorNotFoundException,
|
||||
ConversionException;
|
||||
|
||||
/**
|
||||
* Convert the source object to <code>targetClass</code> 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 <code>targetClass</code>, or <code>null</code> 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> T executeConversion(String converterId, Object source, Class<T> targetClass)
|
||||
throws ConversionExecutorNotFoundException, ConversionException;
|
||||
|
||||
/**
|
||||
* Get a ConversionExecutor capable of converting objects from <code>sourceClass</code> to <code>targetClass</code>.
|
||||
* 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 <S, T> ConversionExecutor<S, T> getConversionExecutor(Class<S> sourceClass, Class<T> targetClass)
|
||||
throws ConversionExecutorNotFoundException;
|
||||
|
||||
/**
|
||||
* Get a ConversionExecutor that uses a custom converter to capable convert objects from <code>sourceClass</code> to
|
||||
* <code>targetClass</code>. 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 <S, T> ConversionExecutor<S, T> getConversionExecutor(String converterId, Class<S> sourceClass,
|
||||
Class<T> targetClass) throws ConversionExecutorNotFoundException;
|
||||
|
||||
/**
|
||||
* Lookup a class by its well-known alias. For example, <code>long</code> for <code>java.lang.Long</code>
|
||||
* @param alias the class alias
|
||||
* @return the class, or <code>null</code> if no alias exists
|
||||
*/
|
||||
public Class<?> getClassForAlias(String alias);
|
||||
|
||||
}
|
||||
|
|
@ -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.
|
||||
* <p>
|
||||
* Implementations of this interface are thread-safe and can be shared. Converters are typically registered with and
|
||||
* accessed through a {@link ConversionService}.
|
||||
* </p>
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface Converter<S, T> {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
}
|
||||
|
|
@ -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<Number, Number> {
|
||||
|
||||
public <N extends Number> N convert(Number source, Class<N> targetClass) throws Exception {
|
||||
return NumberUtils.convertNumberToTargetClass(source, targetClass);
|
||||
}
|
||||
|
||||
public Number convertBack(Number target) throws Exception {
|
||||
return target;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, BigDecimal> {
|
||||
|
||||
public BigDecimal convert(String source) throws Exception {
|
||||
return new BigDecimal(source);
|
||||
}
|
||||
|
||||
public String convertBack(BigDecimal target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, BigInteger> {
|
||||
|
||||
public BigInteger convert(String source) throws Exception {
|
||||
return new BigInteger(source);
|
||||
}
|
||||
|
||||
public String convertBack(BigInteger target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Boolean> {
|
||||
|
||||
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 + "]");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Byte> {
|
||||
|
||||
public Byte convert(String source) throws Exception {
|
||||
return new Byte(source);
|
||||
}
|
||||
|
||||
public String convertBack(Byte target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Character> {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Double> {
|
||||
|
||||
public Double convert(String source) throws Exception {
|
||||
return Double.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Double target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Enum> {
|
||||
|
||||
public <E extends Enum> E convert(String source, Class<E> targetClass) throws Exception {
|
||||
return Enum.valueOf(targetClass, source);
|
||||
}
|
||||
|
||||
public String convertBack(Enum target) throws Exception {
|
||||
return target.name();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Float> {
|
||||
|
||||
public Float convert(String source) throws Exception {
|
||||
return Float.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Float target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Integer> {
|
||||
|
||||
public Integer convert(String source) throws Exception {
|
||||
return Integer.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Integer target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Locale> {
|
||||
|
||||
public Locale convert(String source) throws Exception {
|
||||
return StringUtils.parseLocaleString(source);
|
||||
}
|
||||
|
||||
public String convertBack(Locale target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Long> {
|
||||
|
||||
public Long convert(String source) throws Exception {
|
||||
return Long.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Long target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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<String, Short> {
|
||||
|
||||
public Short convert(String source) throws Exception {
|
||||
return Short.valueOf(source);
|
||||
}
|
||||
|
||||
public String convertBack(Short target) throws Exception {
|
||||
return target.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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.
|
||||
* <p>
|
||||
* Implementations of this interface are thread-safe and can be shared. Converters are typically registered with and
|
||||
* accessed through a {@link ConversionService}.
|
||||
* </p>
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface SuperConverter<S, T> {
|
||||
|
||||
/**
|
||||
* 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 extends T> AT convert(S source, Class<AT> 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;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
<p>
|
||||
User Converter API and default Converter implementations.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<html>
|
||||
<body>
|
||||
<p>
|
||||
Type conversion system SPI.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in New Issue