All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.l4.dust.jaxrs.internal.ConversionParamProvider Maven / Gradle / Ivy

package se.l4.dust.jaxrs.internal;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.ws.rs.ext.ParamConverter;
import javax.ws.rs.ext.ParamConverterProvider;

import se.l4.dust.api.conversion.NonGenericConversion;
import se.l4.dust.api.conversion.TypeConverter;

import com.google.common.primitives.Primitives;
import com.google.inject.Inject;

/**
 * A {@link ParamConverterProvider} that ties into {@link TypeConverter}.
 * 
 * @author Andreas Holstenson
 *
 */
public class ConversionParamProvider
	implements ParamConverterProvider
{
	private final TypeConverter converter;

	@Inject
	public ConversionParamProvider(TypeConverter converter)
	{
		this.converter = converter;
	}

	@Override
	public  ParamConverter getConverter(Class rawType, Type genericType, Annotation[] annotations)
	{
		if(rawType.isPrimitive()
			|| Primitives.isWrapperType(rawType) ||
			rawType == String.class ||
			! converter.canConvertBetween(rawType, String.class) ||
			! converter.canConvertBetween(String.class, rawType))
		{
			// Primitives and String are not handled, nor are types that we can't convert both ways
			return null;
		}
		
		NonGenericConversion toString = converter.getConversion(rawType, String.class);
		NonGenericConversion fromString = converter.getConversion(String.class, rawType);
		return new ParamConverterImpl(toString, fromString);
	}

	private static class ParamConverterImpl
		implements ParamConverter
	{
		private final NonGenericConversion toString;
		private final NonGenericConversion fromString;

		public ParamConverterImpl(NonGenericConversion toString, NonGenericConversion fromString)
		{
			this.toString = toString;
			this.fromString = fromString;
		}
		
		@Override
		public T fromString(String value)
		{
			return fromString.convert(value);
		}
		
		@Override
		public String toString(T value)
		{
			return toString.convert(value);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy