- Revision
- 401
- Author
- mauro
- Date
- 2007-11-20 03:34:01 -0600 (Tue, 20 Nov 2007)
Log Message
Extracted ValueConverterFinder from DelegatingTypeConverter.
Modified Paths
Added Paths
Diff
Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultValueConverterFinder.java (0 => 401)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultValueConverterFinder.java (rev 0) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DefaultValueConverterFinder.java 2007-11-20 09:34:01 UTC (rev 401) @@ -0,0 +1,56 @@ +/***************************************************************************** + * Copyright (C) 2005,2006 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + * Original code by: Michael Ward * + *****************************************************************************/ +package org.codehaus.waffle.bind; + +import java.util.HashMap; +import java.util.Map; + +/** + * Default implementation of <code>ValueConverterFinder</code> which caches + * converters found per type. + * + * @author Michael Ward + * @author Mauro Talevi + */ +public class DefaultValueConverterFinder implements ValueConverterFinder { + + private final Map<Class<?>, ValueConverter> cache = new HashMap<Class<?>, ValueConverter>(); + private final ValueConverter[] converters; + + public DefaultValueConverterFinder() { + this.converters = new ValueConverter[0]; + } + + public DefaultValueConverterFinder(ValueConverter... converters) { + if (converters == null) { + this.converters = new ValueConverter[0]; + } else { + this.converters = converters; + } + } + + public ValueConverter findConverter(Class<?> type) { + if (cache.containsKey(type)) { // cache hit + return cache.get(type); + } + + for (ValueConverter converter : converters) { + if (converter.accept(type)) { + cache.put(type, converter); + return converter; + } + } + + cache.put(type, null); // cache the null + return null; + } + +}
Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DelegatingTypeConverter.java (400 => 401)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DelegatingTypeConverter.java 2007-11-17 10:34:16 UTC (rev 400) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/DelegatingTypeConverter.java 2007-11-20 09:34:01 UTC (rev 401) @@ -10,13 +10,12 @@ *****************************************************************************/ package org.codehaus.waffle.bind; -import ognl.OgnlOps; -import ognl.TypeConverter; - import java.lang.reflect.Member; -import java.util.HashMap; import java.util.Map; +import ognl.OgnlOps; +import ognl.TypeConverter; + /** * An implementation of Ognl's <code>TypeConverter</code> which handles Java 5 enums and will delegate * custom <code>ValueConverter</code>'s registered per application. @@ -26,19 +25,14 @@ */ public class DelegatingTypeConverter implements TypeConverter { private static final String EMPTY = ""; - private final ValueConverter[] valueConverters; - private final Map<Class<?>, ValueConverter> cache = new HashMap<Class<?>, ValueConverter>(); + private final ValueConverterFinder valueConverterFinder; public DelegatingTypeConverter() { - this.valueConverters = new ValueConverter[0]; + this.valueConverterFinder = new DefaultValueConverterFinder(); } public DelegatingTypeConverter(ValueConverter... valueConverters) { - if (valueConverters == null) { - this.valueConverters = new ValueConverter[0]; - } else { - this.valueConverters = valueConverters; - } + this.valueConverterFinder = new DefaultValueConverterFinder(valueConverters); } /** @@ -84,7 +78,7 @@ return Enum.valueOf(toType, value); } - ValueConverter converter = findConverter(toType); + ValueConverter converter = valueConverterFinder.findConverter(toType); if (converter != null) { return converter.convertValue(propertyName, value, toType); @@ -93,20 +87,4 @@ return OgnlOps.convertValue(value, toType); } - private ValueConverter findConverter(Class<?> toType) { - if (cache.containsKey(toType)) { // cache hit - return cache.get(toType); - } - - for (ValueConverter converter : valueConverters) { - if (converter.accept(toType)) { - cache.put(toType, converter); - return converter; - } - } - - cache.put(toType, null); // cache the null - return null; - } - }
Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverterFinder.java (0 => 401)
--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverterFinder.java (rev 0) +++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverterFinder.java 2007-11-20 09:34:01 UTC (rev 401) @@ -0,0 +1,22 @@ +/***************************************************************************** + * Copyright (C) 2005,2006 Michael Ward * + * All rights reserved. * + * ------------------------------------------------------------------------- * + * The software in this package is published under the terms of the BSD * + * style license a copy of which has been included with this distribution in * + * the LICENSE.txt file. * + * * + * Original code by: Mauro Talevi * + *****************************************************************************/ +package org.codehaus.waffle.bind; + +/** + * Finder interface for <code>ValueConverter</code>s registered per application. + * + * @author Mauro Talevi + */ +public interface ValueConverterFinder { + + ValueConverter findConverter(Class<?> type); + +}
To unsubscribe from this list please visit:
