开发者

Conception problem on generic implementation

Context : Java using Guice 3.0


'Hi everybody, I'm trying to construct a decent converter library for my web application.

Converter allow me to convert from an unknown type to another type.

To convert type, I'm using a converter registry with Guice MultiBinder like this one (following @Ivan Sopov's implementation)

An example might be more explicit :

**NOTICE : this is not a working example**
//"obj" is an object with unknowed type, and we want to convert it to another type)

Converter c = converterRegistry.getConverter(obj.getClass(),type.getClass());
Object t = c.convert(obj); //obj is now converted !

I can have many kind of Converter : StringToInteger, StringToDouble, etc ; for those simple one I have no problem.

public interface Converter<F,T>{
    public T convert(F from);
}

public class StringToInteger implements Converter<String,Integer>{
   public Integer convert(String from){
       return Integer.开发者_如何转开发valueOf(from);
   }
}

Problems come when I have to implement Converter that needs some more parameters (specific information to convert a type).

For example : DateToString converter

In this case I would have the current user Locale to format the Date into String correctly.

Currently I'm badly solving the problem using another parameter to my Converter interface like that :

public interface Converter<F,T>{
    public T convert(F from, ConverterData data);
}

public class ConverterData{
     private Locale locale;
     //... many other object that can be usefull for other converter...
}

public class DateToString implements Converter<Date,String>{
   public String convert(Date from, ConverterData data){
       //using data.getLocale() to convert the Date to String and return it
   }
}

Now, I'm searching a better solution to improve the code. I thought about some possible solution using Guice but I'm not totally convinced...

Here is one of them :

public class DateToString implements Converter<Date,String>{

   private Provider<Locale> providerLocale;

   @Inject
   public DateToString(Provider<Locale> providerLocale){
       this.providerLocale = providerLocale;
   }

   public String convert(Date from){
       //using providerLocale.get() to convert the Date
   }
}

Here, I'm using a Provider cause a converter could be in a @Singleton...

(maybe I'm wrong in using a Provider ?)

- How should I do this ?

Well, I tried to explain my problem at best. I hope someone will take time to read that :)

I'm waiting for your help, Thanks in advance !

PS : Hard to find a title for this post, any suggestion are welcome!


It looks like you want a Converter that is configured by the locale state of the current request.

You are on the right track with the Provider. The reason you give sounds off to me. I'll explain.

Because you will be using your converterRegistry to get the Converter it looks like you'll be using the same object for each request.

A provider is factory that uses guice to supply values. Because you want to configure the DataToString converter with per-request state this allows you to configure the locale for each call to the convert method on the reused object.

Next, you need to create a guice provider class or method of the Local. If you already know how to do that you are done.

To more fully integrate guice I suggest you use Guice-servlet to plug it into your requests: http://code.google.com/p/google-guice/wiki/Servlets

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜