Results 1 to 10 of 10
Thread: Generics Problem
- 12-08-2011, 09:16 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Generics Problem
I'm trying to set/get values to/from a Property<T> class.
I can set a Property's value like this: myNode.getProperty( "My_Prop" ).setValue( 0.25 ).
However, I cannot retrieve a Property's value without having to cast.
I want to be able to do this: Double dbl = myNode.getProperty( "My_Prop" ).getValue().
For some reason I cannot figure out, getValue() is returning an Object instead of a Double.
Any guidance is appreciated.
Java Code:public class Property<T> { public Property( String id, T initialValue ) { this.id = id; value = initialValue; } public T getValue() { return value; } public void setValue( T newValue ) { value = newValue; } public String getID() { return id; } private T value; private String id; }Java Code:import java.util.*; public abstract class Node<T> { public Node() { properties = new ArrayList<Property>(); inputs = new ArrayList<Node>(); } public abstract T process(); public Property getProperty( String id ) { for ( Property prop : properties ) { if ( prop.getID().equals( id ) ) return prop; } return null; } public ArrayList<Property> properties; public ArrayList<Node> inputs; }Java Code:import java.awt.image.*; import javax.imageio.*; import java.io.File; public class ImageRead extends Node<BufferedImage> { public ImageRead() { super(); Property<String> imageFile = new Property<String>( "Image File", "bitmap.jpg" ); properties.add( imageFile ); } public BufferedImage process() { return null; } }Java Code:public class Tester { public static void main( String[] args ) { ImageRead read = new ImageRead(); read.getProperty( "Image File" ).setValue( "testImage.jpg" ); //works String str = read.getProperty( "Image File" ).getValue(); //doesn't work String str2 = (String) read.getProperty( "Image File" ).getValue(); //works with cast } }Last edited by robbie.26; 12-08-2011 at 09:20 PM.
- 12-08-2011, 09:30 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Generics Problem
Where do you define the generic type for the Property objects contained in the Node class?
- 12-08-2011, 09:36 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Re: Generics Problem
Originally I defined all the Property objects in Node as Property<?> , but that bombarded me with exceptions like this:
Java Code:C:\Users\Robbie\Desktop\Reaction\Reaction.java:6: error: method setValue in class Property<T> cannot be applied to given types; read.getProperty( "Image File" ).setValue( "testImage.jpg" ); ^ required: CAP#1 found: String reason: actual argument String cannot be converted to CAP#1 by method invocation conversion where T is a type-variable: T extends Object declared in class Property where CAP#1 is a fresh type-variable: CAP#1 extends Object from capture of ?
- 12-08-2011, 10:11 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Re: Generics Problem
A wildcard (?) should extend something.
One way to get the result you are after would be to define Node to have 2 generic types, the second of which is assigned to Property.
Java Code:public abstract class Node<T,V>{ ... properties = new ArrayList<Property<V>>(); ... public Property<V> getProperty() .... }
- 12-08-2011, 11:09 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
- 12-08-2011, 11:32 PM #6
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
- 12-09-2011, 12:29 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Re: Generics Problem
When I define a sub-class of Node like ImageRead, I would need to pass in a second type:
Wouldn't this limit the properties ArrayList of an ImageRead object to contain only Properties of type String?Java Code:public class ImageRead extends Node<BufferedImage, String> { ... }
- 12-09-2011, 12:44 AM #8
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
- 12-09-2011, 01:03 AM #9
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Re: Generics Problem
Ok. Well I need a Node to have an ArrayList of any type of Property. How would I do that?
- 12-09-2011, 02:17 AM #10
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,609
- Rep Power
- 5
Similar Threads
-
Generics problem in JSP?
By makpandian in forum New To JavaReplies: 6Last Post: 07-25-2011, 01:19 PM -
Problem with Generics and BinaryTree
By conderol in forum New To JavaReplies: 2Last Post: 04-28-2011, 08:09 AM -
Generics Problem (Bound Mismatch)
By castiel in forum New To JavaReplies: 2Last Post: 02-16-2011, 12:05 AM -
Generics problem
By ankur.trapasiya in forum New To JavaReplies: 2Last Post: 01-22-2011, 05:09 PM -
Generics
By sireesha in forum New To JavaReplies: 2Last Post: 01-10-2008, 11:08 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks