The Artima Developer Community
Sponsored Link

Java Answers Forum
Generics & Inheritance

3 replies on 1 page. Most recent reply: Aug 11, 2009 4:23 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Lee Rhodes

Posts: 1
Nickname: rhoba
Registered: Jun, 2009

Generics & Inheritance Posted: Jun 28, 2009 10:50 PM
Reply to this message Reply
Advertisement
I have an application that performs various analysis algorithms on graphs of nodes and edges G(N,E). The attributes of the nodes and edges vary with the application and form an inheritance hierarchy based on the type of graph and nature of the attributes. For example the root of the Node hierarchy could represent the most general Non-directed Cyclic Graphs (NcgNode). A sub-class of NcgNode might represent directed cyclic graphs (DcgNode), followed by DagNode, etc. The algorithms that can be applied to DAGs are different from those of NCGs, but not visa-versa. A key behavior of the root of the tree is to add and retrieve adjacent nodes of the graph. The question is how to do this without creating an "unchecked" exception?

A terse version of the code might look like this:

public class NcgNode {
    private List<NcgNode> nodeList_ = null;
    private List<? extends NcgNode> nodeListSrc_ = null;
    private List<? super NcgNode> nodeListSink_ = null;
 
    public <N extends NcgNode> void addNode(N node) {
        if (nodeList_ == null) {
            nodeList_ = new ArrayList<NcgNode>(); 
            nodeListSrc_  = nodeList_;
            nodeListSink_ = nodeList_;
        }
        nodeListSink_.add(node);
    }
 
    @SuppressWarnings("unchecked") //Any way to avoid this?
    public <N extends NcgNode> N getNode(int n) {
        if ((nodeList_ == null) || (n >= nodeList_.size()))
            return null;
        //causes unchecked warning:
        return (N)nodeListSrc_.get(n);
    }
}
 
class DcgNode extends NcgNode {
    //enables DCG algorithms, etc
}
 
class DagNode extends DcgNode {
    //enables DAG algorithms, etc.
}


Is there a better way to design this?


Alok Ranjan Meher

Posts: 2
Nickname: aloka
Registered: Jul, 2008

Re: Generics & Inheritance Posted: Aug 6, 2009 5:02 AM
Reply to this message Reply
modify your method as such

public NcgNode getNode(int n) {
if ((nodeList_ == null) || (n >= nodeList_.size())) {
return null;
}

return (NcgNode) nodeListSrc_.get(n);
}

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Generics & Inheritance Posted: Aug 7, 2009 1:19 AM
Reply to this message Reply
In short: It's just a warning that you can acknowledge and supress.

(In long:) I don't think that there's a simple way to get rid of it. The get method just returns an Object. Checking the cast is done at runtime and, due (I think) to the way that Java does type erasure, the type of the Object cannot be guaranteed at compile time - hence the warning.

This problem, and some possible solutions, are discussed below but it seems to be generally agreed that the solutions are more complex than the problem (and a lot more complex than adding a "supress warnings" label).

http://stackoverflow.com/questions/262367/type-safety-unchecked-cast
http://stackoverflow.com/questions/509076/how-do-i-address-unchecked-cast-warnings/509230#509230

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Generics & Inheritance Posted: Aug 11, 2009 4:23 AM
Reply to this message Reply
> The
> question is how to do this without creating an "unchecked"
> exception?

Not sure about Net Beans and IntelliJ (IDEA) but in Eclipse you can configure it to Ignore Unchecked Generics - in your preferences. If the Warning is being spat out during your Ant/Maven Build, I'm sure there should be ways or flags to ignore this too.

Flat View: This topic has 3 replies on 1 page
Topic: JSP Struts Previous Topic   Next Topic Topic: Diamond printing

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use