Results 1 to 10 of 10
  1. #1
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default 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.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Generics Problem

    Where do you define the generic type for the Property objects contained in the Node class?

  3. #3
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default 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 ?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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()
        ....
    }

  5. #5
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default Re: Generics Problem

    Quote Originally Posted by doWhile View Post
    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()
        ....
    }
    What if I need a Node to hold a collection of different types of Properties?

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Generics Problem

    Quote Originally Posted by robbie.26 View Post
    What if I need a Node to hold a collection of different types of Properties?
    What if? I gave you a solution - add 2 generic types....did you try to implement that solution into your code?

  7. #7
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default Re: Generics Problem

    Quote Originally Posted by doWhile View Post
    What if? I gave you a solution - add 2 generic types....did you try to implement that solution into your code?
    When I define a sub-class of Node like ImageRead, I would need to pass in a second type:

    Java Code:
    public class ImageRead extends Node<BufferedImage, String> { ... }
    Wouldn't this limit the properties ArrayList of an ImageRead object to contain only Properties of type String?

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Generics Problem

    Quote Originally Posted by robbie.26 View Post
    Wouldn't this limit the properties ArrayList of an ImageRead object to contain only Properties of type String?
    Yes, which is exactly what you are doing in the constructor of ImageRead.

  9. #9
    robbie.26 is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default Re: Generics Problem

    Ok. Well I need a Node to have an ArrayList of any type of Property. How would I do that?

  10. #10
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Generics Problem

    Quote Originally Posted by robbie.26 View Post
    Ok. Well I need a Node to have an ArrayList of any type of Property. How would I do that?
    I just told you - specify Node as having 2 generic types, one for the object contained in Node and one for the Parameter type. I recommend you try, and post the code if you have problems.

Similar Threads

  1. Generics problem in JSP?
    By makpandian in forum New To Java
    Replies: 6
    Last Post: 07-25-2011, 01:19 PM
  2. Problem with Generics and BinaryTree
    By conderol in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 08:09 AM
  3. Generics Problem (Bound Mismatch)
    By castiel in forum New To Java
    Replies: 2
    Last Post: 02-16-2011, 12:05 AM
  4. Generics problem
    By ankur.trapasiya in forum New To Java
    Replies: 2
    Last Post: 01-22-2011, 05:09 PM
  5. Generics
    By sireesha in forum New To Java
    Replies: 2
    Last Post: 01-10-2008, 11:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •