net.sf.jsefa.common.converter.provider.SimpleTypeConverterProvider Maven / Gradle / Ivy
/*
* Copyright 2007 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 net.sf.jsefa.common.converter.provider;
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import net.sf.jsefa.common.converter.ConversionException;
import net.sf.jsefa.common.converter.SimpleTypeConverter;
import net.sf.jsefa.common.converter.SimpleTypeConverterConfiguration;
import net.sf.jsefa.common.util.ReflectionUtil;
/**
* Provider for {@link SimpleTypeConverter}.
*
* Each SimpleTypeConverter
must have a static factory method create
, which is
* either parameterless or has exactly one parameter of type {@link SimpleTypeConverterConfiguration}.
*
* It is thread-safe.
*
* @author Norman Lahme-Huetig
*
*/
public final class SimpleTypeConverterProvider {
private final ConcurrentMap, Class> converterTypeMap;
/**
* Constructs a SimpleTypeConverterProvider
.
*/
public SimpleTypeConverterProvider() {
this.converterTypeMap = new ConcurrentHashMap, Class>();
}
private SimpleTypeConverterProvider(SimpleTypeConverterProvider other) {
this.converterTypeMap = new ConcurrentHashMap, Class>(
other.converterTypeMap);
}
/**
* Creates a copy of this SimpleTypeConverterProvider
.
*
* @return a copy of this SimpleTypeConverterProvider
*/
public SimpleTypeConverterProvider createCopy() {
return new SimpleTypeConverterProvider(this);
}
/**
* Returns true if and only if this provider has a SimpleTypeConverter
for the given object
* type.
*
* @param objectType the type of the object a converter is needed for
* @return true if this provider has a SimpleTypeConverter
for the given type; false otherwise.
*/
public boolean hasConverterFor(Class objectType) {
return getConverterType(objectType) != null;
}
/**
* Returns a SimpleTypeConverter
for the given object type and format.
*
* @param objectType the type of the object a converter is needed for
* @param format the format the converter must be initialized with
* @return the converter.
*/
public SimpleTypeConverter getForObjectType(Class objectType, String[] format) {
if (!hasConverterFor(objectType)) {
return null;
}
Class converterType = getConverterType(objectType);
return getForConverterType(converterType, objectType, format);
}
/**
* Returns a SimpleTypeConverter
for the given object type and format.
*
* @param objectType the type of the object a converter is needed for
* @param format the format the converter must be initialized with
* @param itemTypeConverter the item type converter
* @return the converter.
*/
public SimpleTypeConverter getForObjectType(Class objectType, String[] format,
SimpleTypeConverter itemTypeConverter) {
if (!hasConverterFor(objectType)) {
return null;
}
Class converterType = getConverterType(objectType);
return getForConverterType(converterType, objectType, format, itemTypeConverter);
}
/**
* Returns an instance of the given SimpleTypeConverter
type initialized with the given format.
*
* @param converterType the SimpleTypeConverter
type
* @param objectType the type of the object a converter is needed for
* @param format the format to initialize the converter with
* @return the converter
*/
public SimpleTypeConverter getForConverterType(Class converterType,
Class objectType, String[] format) {
return getForConverterType(converterType, objectType, format, null);
}
/**
* Returns an instance of the given SimpleTypeConverter
type initialized with the given format.
*
* @param converterType the SimpleTypeConverter
type
* @param objectType the type of the object a converter is needed for
* @param format the format to initialize the converter with
* @param itemTypeConverter the item type converter
* @return the converter
*/
public SimpleTypeConverter getForConverterType(Class converterType,
Class objectType, String[] format, SimpleTypeConverter itemTypeConverter) {
try {
Method factoryMethod = ReflectionUtil.getMethod(converterType, "create",
SimpleTypeConverterConfiguration.class);
if (factoryMethod != null) {
return (SimpleTypeConverter) ReflectionUtil.callMethod(null, factoryMethod,
SimpleTypeConverterConfiguration.create(objectType, format, itemTypeConverter));
}
if (itemTypeConverter == null) {
factoryMethod = ReflectionUtil.getMethod(converterType, "create");
if (factoryMethod != null) {
return (SimpleTypeConverter) ReflectionUtil.callMethod(null, factoryMethod, (Object[]) null);
}
}
// no static create method found, lets create a new instance
return ReflectionUtil.createInstance(converterType);
} catch (Exception e) {
throw new ConversionException("Could not create a SimpleTypeConverter for class " + converterType, e);
}
}
/**
* Registers the given SimpleTypeConverter
type as being responsible for values of the given
* object type.
*
* @param objectType the object type
* @param converterType the SimpleTypeConverter
type
*/
public void registerConverterType(Class objectType, Class converterType) {
this.converterTypeMap.put(objectType, converterType);
}
private Class getConverterType(Class objectType) {
return ReflectionUtil.getNearest(objectType, this.converterTypeMap);
}
}