Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:44:21+00:00 2026-05-25T16:44:21+00:00

Context : Java using Guice 3.0 ‘Hi everybody, I’m trying to construct a decent

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:44:22+00:00Added an answer on May 25, 2026 at 4:44 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application configuration: Web application using java first method of creating JAX-WS 2.0 Web Services
I am trying to add multitenancy to a java application using the separate schema
I'm using Java for accessing Alfresco content server via it's web service API for
I want to replace the first context of web/style/clients.html with the java String.replaceFirst method
In my spring application context file, I have something like: <util:map id="someMap" map-class="java.util.HashMap" key-type="java.lang.String"
I'm building a java web media-scraping application for extracting content from a variety of
I've written some basic graphing software in Clojure/Java using drawLine() on the graphics context
I'm just getting acquainted with implementing REST web services in Java using JAX-RS and
I'm using Clojure, so this is in the context of Java regexes. Here is
I am writing Web app in java using Spring Web MVC framework. Somehow validation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.