56 changed files with 313 additions and 922 deletions
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
/* |
||||
* 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; |
||||
|
||||
public interface ConverterFactory<S, R> { |
||||
<T extends R> Converter<S, T> getConverter(Class<T> targetType); |
||||
} |
||||
@ -1,17 +0,0 @@
@@ -1,17 +0,0 @@
|
||||
package org.springframework.core.convert.converter; |
||||
|
||||
import org.springframework.core.convert.service.DefaultConversionService; |
||||
|
||||
/** |
||||
* Simply calls {@link Object#toString()} to convert any object to a string. |
||||
* Used by the {@link DefaultConversionService} as a fallback if there are no other explicit to string converters registered. |
||||
* @author Keith Donald |
||||
*/ |
||||
public class ObjectToString implements SuperConverter<Object, String> { |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
public <RT extends String> RT convert(Object source, Class<RT> targetClass) { |
||||
return (RT) source.toString(); |
||||
} |
||||
|
||||
} |
||||
@ -1,71 +0,0 @@
@@ -1,71 +0,0 @@
|
||||
/* |
||||
* 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.Assert; |
||||
|
||||
/** |
||||
* Converts String to a Boolean. The trueString and falseStrings are configurable. |
||||
* |
||||
* @see #StringToBoolean(String, String) |
||||
* @author Keith Donald |
||||
*/ |
||||
public class StringToBoolean implements TwoWayConverter<String, Boolean> { |
||||
|
||||
private String trueString; |
||||
|
||||
private String falseString; |
||||
|
||||
/** |
||||
* Create a StringToBoolean converter with the default 'true' and 'false' strings. |
||||
*/ |
||||
public StringToBoolean() { |
||||
this("true", "false"); |
||||
} |
||||
|
||||
/** |
||||
* Create a StringToBoolean converter configured with specific values for true and false strings. |
||||
* @param trueString special true string to use (required) |
||||
* @param falseString special false string to use (required) |
||||
*/ |
||||
public StringToBoolean(String trueString, String falseString) { |
||||
Assert.hasText(trueString, "The true string is required"); |
||||
Assert.hasText(falseString, "The false string is required"); |
||||
this.trueString = trueString; |
||||
this.falseString = falseString; |
||||
} |
||||
|
||||
public Boolean convert(String source) { |
||||
if (source.equals(trueString)) { |
||||
return Boolean.TRUE; |
||||
} else if (source.equals(falseString)) { |
||||
return Boolean.FALSE; |
||||
} else { |
||||
throw new IllegalArgumentException("Invalid boolean string '" + source + "'; expected '" + trueString + "' or '" + falseString + "'"); |
||||
} |
||||
} |
||||
|
||||
public String convertBack(Boolean target) { |
||||
if (Boolean.TRUE.equals(target)) { |
||||
return trueString; |
||||
} else if (Boolean.FALSE.equals(target)) { |
||||
return falseString; |
||||
} else { |
||||
throw new IllegalArgumentException("Invalid boolean value " + target); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,35 +0,0 @@
@@ -1,35 +0,0 @@
|
||||
/* |
||||
* 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 Enum using {@link Enum#valueOf(Class, String)}. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 3.0 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public class StringToEnum implements SuperTwoWayConverter<String, Enum> { |
||||
|
||||
public <RT extends Enum> RT convert(String source, Class<RT> targetClass) { |
||||
return (RT) Enum.valueOf(targetClass, source); |
||||
} |
||||
|
||||
public <RS extends String> RS convertBack(Enum target, Class<RS> sourceClass) { |
||||
return (RS) target.name(); |
||||
} |
||||
|
||||
} |
||||
@ -1,42 +0,0 @@
@@ -1,42 +0,0 @@
|
||||
/* |
||||
* 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.ConvertException; |
||||
import org.springframework.core.convert.ConversionService; |
||||
|
||||
/** |
||||
* A super converter converts a source object of type S to a target of class hierarchy 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 of type S to an instance of type RT. |
||||
* @param source the source object to convert, whose class must be equal to or a subclass of S |
||||
* @param targetClass the requested target class to convert to (RT), which must be equal to T or extend from T |
||||
* @return the converted object, which must be an instance of RT |
||||
* @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion |
||||
* system will handle wrapping the failure in a {@link ConvertException} that provides a consistent type |
||||
* conversion error context |
||||
*/ |
||||
public <RT extends T> RT convert(S source, Class<RT> targetClass) throws Exception; |
||||
|
||||
} |
||||
@ -1,42 +0,0 @@
@@ -1,42 +0,0 @@
|
||||
/* |
||||
* 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.ConvertException; |
||||
import org.springframework.core.convert.ConversionService; |
||||
|
||||
/** |
||||
* A super converter that can also convert a target object of type T to a source of class hierarchy S. |
||||
* <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 SuperTwoWayConverter<S, T> extends SuperConverter<S, T> { |
||||
|
||||
/** |
||||
* Convert the target of type T to an instance of RS. |
||||
* @param target the target object to convert, whose class must be equal to or a subclass of T |
||||
* @param sourceClass the requested source class to convert to, which must be equal to S or extend from S |
||||
* @return the converted object, which must be an instance of RS |
||||
* @throws Exception an exception occurred performing the conversion; may be any checked exception, the conversion |
||||
* system will handle wrapping the failure in a {@link ConvertException} that provides a consistent type |
||||
* conversion error context |
||||
*/ |
||||
public <RS extends S> RS convertBack(T target, Class<RS> sourceClass) throws Exception; |
||||
|
||||
} |
||||
@ -1,25 +0,0 @@
@@ -1,25 +0,0 @@
|
||||
package org.springframework.core.convert.converter; |
||||
|
||||
import org.springframework.core.convert.ConversionService; |
||||
import org.springframework.core.convert.ConvertException; |
||||
|
||||
/** |
||||
* A converter that can also convert a target object of type T to a source of class S. |
||||
* <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 TwoWayConverter<S, T> extends Converter<S, T> { |
||||
|
||||
/** |
||||
* Convert the target of type 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 ConvertException} that provides a consistent type |
||||
* conversion error context |
||||
*/ |
||||
public S convertBack(T target) throws Exception; |
||||
} |
||||
@ -1,7 +1,7 @@
@@ -1,7 +1,7 @@
|
||||
<html> |
||||
<body> |
||||
<p> |
||||
User Converter API and default Converter implementations. |
||||
SPI to implement Converters. |
||||
</p> |
||||
</body> |
||||
</html> |
||||
@ -1,7 +1,7 @@
@@ -1,7 +1,7 @@
|
||||
<html> |
||||
<body> |
||||
<p> |
||||
Type conversion system SPI. |
||||
Type conversion system API. |
||||
</p> |
||||
</body> |
||||
</html> |
||||
@ -1,38 +0,0 @@
@@ -1,38 +0,0 @@
|
||||
/* |
||||
* 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.service; |
||||
|
||||
import org.springframework.core.convert.converter.SuperConverter; |
||||
import org.springframework.core.convert.converter.SuperTwoWayConverter; |
||||
|
||||
/** |
||||
* A super converter that reverses another super converter. |
||||
* @author Keith Donald |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class ReverseSuperConverter implements SuperConverter { |
||||
|
||||
private SuperTwoWayConverter converter; |
||||
|
||||
public ReverseSuperConverter(SuperTwoWayConverter converter) { |
||||
this.converter = converter; |
||||
} |
||||
|
||||
public Object convert(Object source, Class targetClass) throws Exception { |
||||
return converter.convertBack(source, targetClass); |
||||
} |
||||
|
||||
} |
||||
@ -1,73 +0,0 @@
@@ -1,73 +0,0 @@
|
||||
/* |
||||
* 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.service; |
||||
|
||||
import org.springframework.core.convert.ConversionExecutionException; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.core.convert.converter.SuperConverter; |
||||
import org.springframework.core.style.ToStringCreator; |
||||
|
||||
/** |
||||
* Default conversion executor implementation for super converters. |
||||
* @author Keith Donald |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class StaticSuperConversionExecutor implements ConversionExecutor { |
||||
|
||||
private final TypeDescriptor sourceType; |
||||
|
||||
private final TypeDescriptor targetType; |
||||
|
||||
private final SuperConverter converter; |
||||
|
||||
public StaticSuperConversionExecutor(TypeDescriptor sourceType, TypeDescriptor targetType, SuperConverter converter) { |
||||
this.sourceType = sourceType; |
||||
this.targetType = targetType; |
||||
this.converter = converter; |
||||
} |
||||
|
||||
public Object execute(Object source) throws ConversionExecutionException { |
||||
if (source == null) { |
||||
return null; |
||||
} |
||||
if (!sourceType.isInstance(source)) { |
||||
throw new ConversionExecutionException(source, sourceType.getType(), targetType, "Source object " |
||||
+ source + " to convert is expected to be an instance of [" + sourceType.getName() + "]"); |
||||
} |
||||
try { |
||||
return converter.convert(source, targetType.getType()); |
||||
} catch (Exception e) { |
||||
throw new ConversionExecutionException(source, sourceType.getType(), targetType, e); |
||||
} |
||||
} |
||||
|
||||
public boolean equals(Object o) { |
||||
if (!(o instanceof StaticSuperConversionExecutor)) { |
||||
return false; |
||||
} |
||||
StaticSuperConversionExecutor other = (StaticSuperConversionExecutor) o; |
||||
return sourceType.equals(other.sourceType) && targetType.equals(other.targetType); |
||||
} |
||||
|
||||
public int hashCode() { |
||||
return sourceType.hashCode() + targetType.hashCode(); |
||||
} |
||||
|
||||
public String toString() { |
||||
return new ToStringCreator(this).append("sourceClass", sourceType).append("targetClass", targetType) |
||||
.toString(); |
||||
} |
||||
} |
||||
@ -1,58 +0,0 @@
@@ -1,58 +0,0 @@
|
||||
/* |
||||
* 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.service; |
||||
|
||||
import org.springframework.core.convert.converter.Converter; |
||||
import org.springframework.core.convert.converter.ConverterInfo; |
||||
import org.springframework.core.convert.converter.SuperConverter; |
||||
import org.springframework.core.convert.converter.SuperTwoWayConverter; |
||||
|
||||
/** |
||||
* Adapts a {@link SuperTwoWayConverter} to the {@link Converter} interface in a type safe way. This adapter is useful |
||||
* for applying more general {@link SuperConverter} logic to a specific source/target class pair. |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
class SuperTwoWayConverterConverter implements Converter, ConverterInfo { |
||||
|
||||
private SuperTwoWayConverter superConverter; |
||||
|
||||
private Class sourceType; |
||||
|
||||
private Class targetType; |
||||
|
||||
public SuperTwoWayConverterConverter(SuperTwoWayConverter superConverter, Class sourceType, Class targetType) { |
||||
this.superConverter = superConverter; |
||||
this.sourceType = sourceType; |
||||
this.targetType = targetType; |
||||
} |
||||
|
||||
public Class getSourceType() { |
||||
return sourceType; |
||||
} |
||||
|
||||
public Class getTargetType() { |
||||
return targetType; |
||||
} |
||||
|
||||
public Object convert(Object source) throws Exception { |
||||
return superConverter.convert(source, targetType); |
||||
} |
||||
|
||||
public Object convertBack(Object target) throws Exception { |
||||
return superConverter.convertBack(target, sourceType); |
||||
} |
||||
|
||||
} |
||||
@ -1,7 +0,0 @@
@@ -1,7 +0,0 @@
|
||||
<html> |
||||
<body> |
||||
<p> |
||||
ConversionService implementation. |
||||
</p> |
||||
</body> |
||||
</html> |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
/* |
||||
* 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.support; |
||||
|
||||
import org.springframework.core.convert.converter.Converter; |
||||
import org.springframework.util.NumberUtils; |
||||
|
||||
/** |
||||
* Converts from any JDK-standard Number implementation to a Character. |
||||
* |
||||
* @see java.lang.Character |
||||
* @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 |
||||
* @see NumberUtils |
||||
* |
||||
* @author Keith Donald |
||||
*/ |
||||
public class NumberToCharacter implements Converter<Number, Character> { |
||||
public Character convert(Number source) { |
||||
return Character.valueOf((char) source.shortValue()); |
||||
} |
||||
} |
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.converter.Converter; |
||||
|
||||
/** |
||||
* Simply calls {@link Object#toString()} to convert any object to a string. |
||||
* Used by the {@link DefaultConversionService} as a fallback if there are no other explicit to string converters registered. |
||||
* @author Keith Donald |
||||
*/ |
||||
public class ObjectToString implements Converter<Object, String> { |
||||
public String convert(Object source) { |
||||
return source.toString(); |
||||
} |
||||
} |
||||
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
package org.springframework.core.convert.support; |
||||
|
||||
import org.springframework.core.convert.converter.Converter; |
||||
import org.springframework.core.convert.converter.ConverterFactory; |
||||
|
||||
@SuppressWarnings("unchecked") |
||||
public class StringToEnumFactory implements ConverterFactory<String, Enum> { |
||||
|
||||
public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) { |
||||
return new StringToEnum(targetType); |
||||
} |
||||
|
||||
class StringToEnum<T extends Enum> implements Converter<String, T> { |
||||
|
||||
private Class<T> enumType; |
||||
|
||||
public StringToEnum(Class<T> enumType) { |
||||
this.enumType = enumType; |
||||
} |
||||
|
||||
public T convert(String source) throws Exception { |
||||
return Enum.valueOf(enumType, source); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
<html> |
||||
<body> |
||||
<p> |
||||
TypeConverter system implementation. |
||||
</p> |
||||
</body> |
||||
</html> |
||||
@ -1,9 +1,11 @@
@@ -1,9 +1,11 @@
|
||||
package org.springframework.core.convert.service; |
||||
package org.springframework.core.convert.support; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
import org.junit.Test; |
||||
import org.springframework.core.convert.TypeDescriptor; |
||||
import org.springframework.core.convert.support.ArrayToArray; |
||||
import org.springframework.core.convert.support.DefaultConversionService; |
||||
|
||||
public class ArrayToArrayTests { |
||||
|
||||
Loading…
Reference in new issue