[Biojava-dev] another 1.5 generics question
Matthew Pocock
matthew_pocock at yahoo.co.uk
Wed Apr 21 06:14:52 EDT 2004
I have had trouble with some cases of nesting templated declarations
like this, particularly with anonymous classes. It is a particular
instance of Graph that is parameterised by concrete types in place of A
and B. Node and Edge do not have an instance of Graph available, so they
can not infer the run-time type of A and B. Try making the Node and
Edge interfaces non-static. Alternatively, it may be possible to declare
Node and Edge as being parameterised by AA and BB, and swapping the
types appropreately in their deffinitions.
Matthew
Michael Heuer wrote:
>All,
>
>Sorry to abuse the list with another 1.5 generics question.
>
>Given,
>
>import java.util.Map;
>import java.util.Set;
>
>/**
> * A graph.
> */
>public interface Graph<A, B>
>{
>
> /**
> * Return a read-only set view of the nodes in this graph.
> */
> Set<Node<A>> nodes();
>
> /**
> * Return a read-only set view of the edges in this graph.
> */
> Set<Edge<B>> edges();
>
> /**
> * Return a map of type <code><Node, T></code> with
> * the nodes in this graph as keys.
> */
> <T> Map<Node, T> nodeMap();
>
> /**
> * Return a map of type <code><Edge, T></code> with
> * the edges in this graph as keys.
> */
> <T> Map<Edge, T> edgeMap();
>
> /**
> * Create a node in this graph for the specified element
> * (optional operation).
> */
> Node<A> createNode(A a)
> throws UnsupportedOperationException;
>
> /**
> * Create an edge in this graph for the specified element
> * connecting the specified set of nodes (optional operation).
> */
> Edge<B> createEdge(B b, Set<Node<A>> nodes)
> throws UnsupportedOperationException;
>
>
> /**
> * Node in a graph.
> */
> interface Node<A>
> {
>
> /**
> * Return the element at this node.
> */
> A get();
>
> /**
> * Set the element at this node to <code>a</code>
> * (optional operation).
> */
> void set(A a)
> throws UnsupportedOperationException;
>
> /**
> * Return a read-only set view of the edges in this graph connected
> * to this node.
> */
> Set<Edge<B>> edges();
> }
>
>
> /**
> * Edge in a graph.
> */
> interface Edge<B>
> {
>
> /**
> * Return the element at this edge.
> */
> B get();
>
> /**
> * Set the element at this edge to <code>b</code>
> * (optional operation).
> */
> void set(B b)
> throws UnsupportedOperationException;
>
> /**
> * Return a read-only set view of the nodes in this graph connected
> * by this edge.
> */
> Set<Node<A>> nodes();
> }
>}
>
>I get a complaint about the Set<Edge<B>> edges() method on
>Node<A> and the Set<Node<A>> nodes() method on Edge<B>:
>
> non-static class A cannot be referenced from a static context
>
>I'm a bit confused, because I thought that everything is static in an
>interface definition, and I thought that A, B in this example are just
>type placeholders for the element classes.
>
> michael
>
>_______________________________________________
>biojava-dev mailing list
>biojava-dev at biojava.org
>http://biojava.org/mailman/listinfo/biojava-dev
>
>
>
More information about the biojava-dev
mailing list