/** * Answer an enhanced node that wraps the given node and conforms to the given * interface type. * * @param n A node (assumed to be in this graph) * @param interf A type denoting the enhanced facet desired * @return An enhanced node */ public <X extends RDFNode> X getNodeAs( Node n, Class<X> interf ) { // We use a cache to avoid reconstructing the same Node too many times. EnhNode eh = (EnhNode) enhNodes.getIfPresent( n ); if ( eh == null ) { // not in the cache, so build a new one X constructed = personality.newInstance(interf, n, this) ; enhNodes.put(n, constructed) ; return constructed ; } else return eh.viewAs( interf ); }
/** * Answer an enhanced node that wraps the given node and conforms to the given * interface type. * * @param n A node (assumed to be in this graph) * @param interf A type denoting the enhanced facet desired * @return An enhanced node */ public <X extends RDFNode> X getNodeAs( Node n, Class<X> interf ) { // We use a cache to avoid reconstructing the same Node too many times. EnhNode eh = (EnhNode) enhNodes.getIfPresent( n ); if ( eh == null ) { // not in the cache, so build a new one X constructed = personality.newInstance(interf, n, this) ; enhNodes.put(n, constructed) ; return constructed ; } else return eh.viewAs( interf ); }
/** * View n as intf. This is supported iff rslt. */ private static <X extends RDFNode> void miniAsSupports(String title, TestNode n, Class<X> intf, boolean rslt ) { assertTrue(title +":sanity",n instanceof Polymorphic<?>); // It is always possible to view any node with any interface. TestNode as1 = (TestNode)((EnhNode)n).viewAs(intf); TestNode as2 = (TestNode)((EnhNode)n).viewAs(intf); // caching should ensure we get the same result both times. assertTrue( title + ":idempotency", as1==as2 ); // Whether the interface is actually useable depends on the underlying // graph. This factoid is the rslt parameter. assertEquals( title +":support",rslt,((EnhNode) as1).supports( intf ) ); }
/** * View n as intf. This is supported iff rslt. */ private static <X extends RDFNode> void miniAsSupports(String title, TestNode n, Class<X> intf, boolean rslt ) { assertTrue(title +":sanity",n instanceof Polymorphic<?>); // It is always possible to view any node with any interface. TestNode as1 = (TestNode)((EnhNode)n).viewAs(intf); TestNode as2 = (TestNode)((EnhNode)n).viewAs(intf); // caching should ensure we get the same result both times. assertTrue( title + ":idempotency", as1==as2 ); // Whether the interface is actually useable depends on the underlying // graph. This factoid is the rslt parameter. assertEquals( title +":support",rslt,((EnhNode) as1).supports( intf ) ); }
public void testAlreadyLinkedViewException() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeCreateUtils.create( "spoo:bar" ); EnhNode eNode = new Example( n, eg ); EnhNode multiplexed = new Example( n, eg ); multiplexed.as( Property.class ); eNode.viewAs( Example.class ); try { eNode.addView( multiplexed ); fail( "should raise an AlreadyLinkedViewException " ); } catch (AlreadyLinkedViewException e) {} }
public void testAlreadyLinkedViewException() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeCreateUtils.create( "spoo:bar" ); EnhNode eNode = new Example( n, eg ); EnhNode multiplexed = new Example( n, eg ); multiplexed.as( Property.class ); eNode.viewAs( Example.class ); try { eNode.addView( multiplexed ); fail( "should raise an AlreadyLinkedViewException " ); } catch (AlreadyLinkedViewException e) {} }