Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Generic methods: how to express explicit type parameters?

Thread view: 
z-man - 01 Oct 2006 18:13 GMT
Hi all

I'm struggling to solve this puzzle: I'm porting some C# code to Java
regarding some invocations to a couple of generic methods.

The problem is that the generic types of such generic methods cannot be
inferred by the compiler as they only refer to the returning value
(getter method: getEntry) and to some local variables inside the
respective code bodies (both getter and setter methods: getEntry and
setEntry).

So, it seems that an explicit type parameter invocation is needed: C#
supports it (see below), but I couldn't find an equivalent replacement
in Java till now.

Any idea?

Many thanks.

// C# version -------------------------------------
public string Name
{
get{return GetEntry<string,MyStringType>("name");}
set{SetEntry<string,MyStringType>("name",value);}
}

protected T GetEntry<T,TBase>(
string key
)
where TBase : MyBaseType<T>
{
try{return (T)((TBase)entries[key]).Value;}
catch{return default(T);}
}

protected void SetEntry<T,TBase>(
string key,
T value
)
where TBase : MyBaseType<T>, new()
{
if(!entries.ContainsKey(key))
 entries[key] = new TBase();

((TBase)entries[key]).Value = value;
}

// Java version -----------------------------------
public String getName()
{
// Doesn't work! What's the equivalent syntax?
return getEntry<String,MyStringType>("name");
}

public void setName(
String value
)
{
// Doesn't work! What's the equivalent syntax?
setEntry<String,MyStringType>("name",value);
}

protected T <T,TBase extends MyBaseType<T>> getEntry(
String key
)
{
try{return (T)((TBase)entries.get(key))).getValue();}
catch{return default(T);}
}

protected void <T,TBase extends MyBaseType<T>> setEntry(
String key,
T value
)
{
if(!entries.containsKey(key))
  entries.set(key,new TBase());

((TBase)entries.get(key))).setValue(value);
}
Piotr Kobzda - 01 Oct 2006 21:31 GMT
> // Java version -----------------------------------
> public String getName()
> {
>  // Doesn't work! What's the equivalent syntax?
>  return getEntry<String,MyStringType>("name");

 return this.<String,MyStringType>getEntry("name");

> }
>
[quoted text clipped - 4 lines]
>  // Doesn't work! What's the equivalent syntax?
>  setEntry<String,MyStringType>("name",value);

 return this.<String,MyStringType>setEntry("name",value);

> }
>
[quoted text clipped - 16 lines]
>  ((TBase)entries.get(key))).setValue(value);
> }

But all this won't work together in Java, because of erasure...

I think you need something like this:

public class YourCSharpPort {

    public String getName() {
        return getEntry("name", MyStringType.class);
    }

    public void setName(String value) {
        setEntry("name", MyStringType.class, value);
    }

    Map<String, Object> entries;

    protected <T, TBase extends MyBaseType<T>>
            T getEntry(
                    String key, Class<TBase> typeOfTBase) {
        TBase holder = typeOfTBase.cast(entries.get(key));
        return holder != null ? holder.getValue() : null;
    }

    protected <T, TBase extends MyBaseType<T>>
            void setEntry(
                    String key, Class<TBase> typeOfTBase,
                    T value) {
        TBase holder = typeOfTBase.cast(entries.get(key));
        if (holder == null && !entries.containsKey(key))
            entries.put(key, holder = newInstanceOf(typeOfTBase));
        holder.setValue(value);
    }

    protected <T> T newInstanceOf(Class<T> typeOfT) {
        try {
            return typeOfT.newInstance();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}

HTH,
piotr
z-man - 01 Oct 2006 23:54 GMT
>> // Java version -----------------------------------
>> public String getName()
[quoted text clipped - 83 lines]
> HTH,
> piotr

Many thanks Piotr, I'll get a try of it!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.