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 6188111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:10:28+00:00 2026-05-24T02:10:28+00:00

I have a conception problem on my ConverterManager. ConverterManager is an object that convert

  • 0

I have a conception problem on my ConverterManager. ConverterManager is an object that convert a type to an other type. As you can see below, I got an error when I’m tyring to create “convert” function.

public class StringIntegerConverter implements Converter<String, Integer> {
    @Override
    public Integer convert(String from) {
        //...
        return Integer.valueOf(from);
    }
}


public class ConverterManager {

    private static Map<Key,Converter<?,?>> converterRegistry;
    {
        converterRegistry = new HashMap<Key, Converter<?,?>>();
        converterRegistry.put(new Key(String.class, Integer.class), new StringIntegerConverter()); 
    }   

    public <T> T convert(Object source, Class<T> toType) 
    {
         //**ERROR HERE : cause "source" is an Object**
         return converterRegistry.get(new Key(source.getClass(),toType)).convert(source);
    }

}
  • Is there a way to solve this problem ? (I don’t want to change my StringIntegerConverter to accepet converting from Object)

Thank you for reading, I hope someone will help me 😉

  • 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-24T02:10:28+00:00Added an answer on May 24, 2026 at 2:10 am

    Basically you’ll have to perform an unsafe conversion – at execution time, the JVM can’t check that you’ve really got a Converter<String, Integer>. That leaves the problem of persuading it to accept the input. You can do that via a “proxying” converter class which implements Converter<Object, ?>:

    class ConverterConverter<T, U> implements Converter<Object, U>
    {
        private final Converter<T, U> original;
    
        ConverterConverter(Converter<T, U> original)
        {
            this.original = original;
        }
    
        public U convert(Object input)
        {
            return original.convert((T) input);
        }
    }
    
    class ConverterManager {
    
        private Map<Key,Converter<Object,?>> converterRegistry;
    
        ConverterManager()
        {
            converterRegistry = new HashMap<Key, Converter<Object,?>>();
            converterRegistry.put(new Key(String.class, Integer.class),
                                  new ConverterConverter<String, Integer>
                                      (new StringIntegerConverter())); 
        }   
    
        public <T> T convert(Object source, Class<T> toType) 
        {
            Key key = new Key(source.getClass(), toType);
            Converter<Object, ?> converter = converterRegistry.get(key);
            return (T) converter.convert(source);
        }
    }
    

    This is messy and it feels like there should be a better way, but it’s all I’ve got at the moment…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My problem is I have an existing table that I can not modify that
I have an app that loads an URL. My problem is that it shows
This is a common problem for me. I have a solution that contains a
I have a problem with a Socket connection closing too fast. I was told
My problem involves checking if I have a valid database connection before reading from
I'm hearing that is better to have one connection open upon app start up
I have a .NET 3.5 website that is uploading files to a SQL Server
I have a website running a basic ASP.NET application that is mostly used from
I have a connection class that uses NSURLConnection to connect to the server. While
I have connection leak to DB in my code. The funny thing is that

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.