Michael,
The magic is new in JDK 1.5 generics. It's a generic method the <T>
means that at compile time it should deduce anywhere it sees T (T can be
anything you want) in the method based on the actual classes used.
So in the below example we use T as the return type so the compiler can
pick this up from your assignment statement Integer i =
task.getProperty(...) and then use Integer where ever it sees T.
public <T> T getProperty(QName name) {
return (T)properties.get(name);
}
This just makes your code a lot easier to read as you don't have to cast
in your code
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf section 5 generic
methods
Paul
Michael Michaud wrote:
Paul Austin a écrit :
I would like to extend the Task class to support properties. Properties
can be set by plug-ins on the task. An example of a property would be
the SRID for the whole task. This will allow us to support extended
metadata on tasks without having to add new methods each time or
subclassing.
Seems to be a good idea to me.
I propose to use QName's as the key for the keys for the properties.
This would allow different namespaces for different applications.
The change would include the following.
private void Map<QName,Object> properties = new HashMap<QName,Object>();
public void setProperty(QName name, Object value) {
properties.put(name, value);
}
/**
* This will auto cast so you can just type things like
* int value = getProperty(MY_KEY);
*/
public <T> T getProperty(QName name) {
return (T)properties.get(name);
}
Hey, looks like magic code.
I cannot understand how the type T can be used in an object method if it
has not been declared in the class definition.
Can really the jvm know how to cast properties.get(KEY) while the object
associated with this key can be any Object value?
Michaël
public Map<QName,Object> getProperties() {
}
We'd also need to have it setup so this would be saved to the project file.
Any suggestions/comments?
Paul
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel