This is also a good pattern IMHO:
public <K> IConverter<K> getConverter(Class<K> type) {
if (Integer.class.isAssignableFrom(type)) {
return new IntegerConverter<K>();
} else {
return super.getConverter(type);
}
}
class IntegerConverter<J> implements IConverter<J> {
@SuppressWarnings("unchecked")
public J convertToObject(String value, Locale locale) {
return (J) new Integer(value);
}
public String convertToString(Integer value, Locale locale) {
return null;
}
}
Except that IntegerConverter class must not be public (I prefer it as an
inner or anonymous class.)
regards
On Tue, Apr 15, 2008 at 4:25 PM, Bruno Borges <[EMAIL PROTECTED]>
wrote:
> This is what you could do:
>
> import java.util.Locale;
>
> public class Test {
> public <L> IConverter<L> getConverter(Class<L> type) {
> return null;
> }
> }
>
> class InnerTest extends Test {
> public <K> IConverter<K> getConverter(Class<K> type) {
> if (Integer.class.isAssignableFrom(type)) {
> return (IConverter<K>) new IConverter<Integer>() {
> public Integer convertToObject(String value, Locale locale) {
> return null;
> }
> public String convertToString(Integer value, Locale locale) {
> return null;
> }
> };
> } else {
> return super.getConverter(type);
> }
> }
>
> public void foo() {
> IConverter<Integer> converter = getConverter(Integer.class);
> }
> }
>
> interface IConverter<X> {
> public X convertToObject(String value, Locale locale);
> public String convertToString(Integer value, Locale locale);
> }
>
> But, you are obligated to use a hard cast inside of getConverter either
> way.
>
> Regards
>
>
> On Tue, Apr 15, 2008 at 4:04 PM, Igor Vaynberg <[EMAIL PROTECTED]>
> wrote:
>
> > Type mismatch: cannot convert from new IConverter<Integer>(){} to
> > IConverter<Z>
> >
> > i can hard cast to IConverter<Z> myself, but then there is very little
> > point to having it generified
> >
> > -igor
> >
> >
> > On Tue, Apr 15, 2008 at 11:59 AM, Bruno Borges <[EMAIL PROTECTED]>
> > wrote:
> > > What is the compile error message?
> > >
> > > On Tue, Apr 15, 2008 at 3:10 PM, Igor Vaynberg <
> > [EMAIL PROTECTED]>
> > >
> > >
> > > wrote:
> > >
> > > > maybe just IConverter<?> getConverter(Class<?> cl)
> > > >
> > > > -igor
> > > >
> > > >
> > > > On Tue, Apr 15, 2008 at 11:05 AM, Igor Vaynberg <
> > [EMAIL PROTECTED]>
> > > > wrote:
> > > > > example below doesnt compile, so im not exactly sure how to
> > override
> > > > > generified getconverter() properly ... do we need to change our
> > > > > generics decl somehow?
> > > > >
> > > > > -igor
> > > > >
> > > > > @Override
> > > > > public <Z> IConverter<Z> getConverter(Class<Z> type)
> > > > > {
> > > > > if (Integer.class.isAssignableFrom(type))
> > > > > {
> > > > > return new IConverter<Integer>()
> > > > > {
> > > > > private static final long
> > > > serialVersionUID = 1L;
> > > > >
> > > > > public Integer
> > convertToObject(String
> > > > value, Locale locale)
> > > > > {
> > > > > return null;
> > > > > }
> > > > >
> > > > > public String
> > convertToString(Integer
> > > > value, Locale locale)
> > > > > {
> > > > > return null;
> > > > > }
> > > > >
> > > > > };
> > > > > }
> > > > > else
> > > > > {
> > > > > return super.getConverter(type);
> > > > > }
> > > > > }
> > > > >
> > > >
> > >
> > >
> > >
> > > --
> > > Bruno Borges
> > > blog.brunoborges.com.br
> > > +55 1185657739
> > >
> > > "The glory of great men should always be
> > > measured by the means they have used to
> > > acquire it."
> > > - Francois de La Rochefoucauld
> > >
> >
>
>
>
> --
> Bruno Borges
> blog.brunoborges.com.br
> +55 1185657739
>
> "The glory of great men should always be
> measured by the means they have used to
> acquire it."
> - Francois de La Rochefoucauld
>
--
Bruno Borges
blog.brunoborges.com.br
+55 1185657739
"The glory of great men should always be
measured by the means they have used to
acquire it."
- Francois de La Rochefoucauld