我需要某种Converter-Mapper并且没有任何好主意,如何使用特殊的转换器轻松附加枚举。我尝试了以下方法:
//ConverterInterface:
public interface PropertyConverter<T>
{
public String convertObjectToString( T object );
public T convertStringToObject( String string );
}
//Concrete Converter
public class FooConverter implements PropertyConverter<Foo>
{
@Override
public String convertObjectToString( Foo object )
{
throw new UnsupportedOperationException( "Not implemented yet." );
}
@Override
public Foo convertStringToObject( String string )
{
throw new UnsupportedOperationException( "Not implemented yet." );
}
}
//Dataclass
public class Foo
{
}
同样适用于Boo,这里是enum,我想将Converter附加到特定类型:
public enum PropEnum
{
BOO(new BooConverter()),
FOO(new FooConverter());
PropertyConverter<?> converter;
private PropEnum( PropertyConverter<?> converter )
{
this.converter = converter;
}
public PropertyConverter<?> getConverter()
{
return converter;
}
}
但由于我的PropertyConverter使用了通配符,因此我只使用对象字符串和字符串到对象方法而不是具体类型,如Foo To String和String To Foo,当我像下面这样使用它时:
有没有办法从转换器实现中接收具体类型?