/** Answer an new enhanced node object that presents <i>this</i> in a way which satisfies type <code>t</code>. The new object is linked into this object's sibling ring. If the node cannot be converted, throw an UnsupportedPolymorphismException. */ @Override protected <X extends RDFNode> X convertTo( Class<X> t ) { EnhGraph eg = getGraph(); if (eg == null) throw new UnsupportedPolymorphismException( this, false, t ); Implementation imp = getPersonality().getImplementation( t ); if (imp == null) throw new UnsupportedPolymorphismException( this, true, t ); EnhNode result = imp.wrap( asNode(), eg ); this.addView( result ); return t.cast( result ); }
@Override public EnhNode wrap( Node n, EnhGraph g ) { return new EnhNode( n, g ); }
/** answer true iff this enhanced node can support the class <code>t</code>, ie it is already a value <code>t</code> or it can be reimplemented as a <code>t</code> via the graph's personality's implementation. If this node has no graph, answer false. */ @Override protected <X extends RDFNode> boolean canSupport( Class<X> t ) { if (alreadyHasView( t )) return true; if (getGraph() == null) return false; Implementation imp = getPersonality().getImplementation( t ); return imp == null ? false : imp.canWrap( asNode(), getGraph() ); }
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) {} }
/** Test that an attempt to polymorph an enhanced node into a class that isn't supported by the enhanced graph generates an UnsupportedPolymorphism exception. */ public void testNullPointerTrap() { EnhGraph eg = new EnhGraph( Factory.createGraphMem(), new Personality<RDFNode>() ); Node n = NodeCreateUtils.create( "eh:something" ); EnhNode en = new EnhNode( n, eg ); try { en.as( Property.class ); fail( "oops" ); } catch (UnsupportedPolymorphismException e) { assertEquals( en, e.getBadNode() ); assertTrue( "exception should have cuplprit graph", eg == ((EnhNode)e.getBadNode()).getGraph() ); assertSame( "exception should have culprit class", Property.class, e.getBadClass() ); } }
/** * 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 ) ); }
/** * Answer the personality object bound to this enhanced node, which we obtain from * the associated enhanced graph. * * @return The personality object */ @Override protected Personality<RDFNode> getPersonality() { return getGraph().getPersonality(); }
@Override public boolean test( T x ) { Node n = (x instanceof Triple) ? ((Triple) x).getSubject() : ((x instanceof EnhNode) ? ((EnhNode) x).asNode() : (Node) x); try { getNodeAs( n, m_asKey ); } catch (Exception ignore) { return false; } return true; } }
public void testSimple() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeFactory.createURI( "spoo:bar" ); EnhNode eNode = new EnhNode( NodeFactory.createURI( "spoo:bar" ), eg ); EnhNode eBlank = new EnhNode( NodeFactory.createBlankNode(), eg ); assertTrue( "URI node can be an Example", eNode.supports( Example.class ) ); assertFalse( "Blank node cannot be an Example", eBlank.supports( Example.class ) ); }
/** * 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 a facet of this node, where that facet is denoted by the * given type. * * @param t A type denoting the desired facet of the underlying node * @return An enhanced nodet that corresponds to t; this may be <i>this</i> * Java object, or a different object. */ public <X extends RDFNode> X viewAs( Class<X> t ) { return asInternal( t ); }
/** API-level method for polymorphic testing */ public <X extends RDFNode> boolean canAs( Class<X> t ) { return canSupport( t ); }
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) {} }
/** Test that an attempt to polymorph an enhanced node into a class that isn't supported by the enhanced graph generates an UnsupportedPolymorphism exception. */ public void testNullPointerTrap() { EnhGraph eg = new EnhGraph( Factory.createGraphMem(), new Personality<RDFNode>() ); Node n = NodeCreateUtils.create( "eh:something" ); EnhNode en = new EnhNode( n, eg ); try { en.as( Property.class ); fail( "oops" ); } catch (UnsupportedPolymorphismException e) { assertEquals( en, e.getBadNode() ); assertTrue( "exception should have cuplprit graph", eg == ((EnhNode)e.getBadNode()).getGraph() ); assertSame( "exception should have culprit class", Property.class, e.getBadClass() ); } }
/** * 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 ) ); }
/** * Answer the personality object bound to this enhanced node, which we obtain from * the associated enhanced graph. * * @return The personality object */ @Override protected Personality<RDFNode> getPersonality() { return getGraph().getPersonality(); }
@Override public boolean test( T x ) { Node n = (x instanceof Triple) ? ((Triple) x).getSubject() : ((x instanceof EnhNode) ? ((EnhNode) x).asNode() : (Node) x); try { getNodeAs( n, m_asKey ); } catch (Exception ignore) { return false; } return true; } }
public void testSimple() { Graph g = Factory.createGraphMem(); Personality<RDFNode> ours = BuiltinPersonalities.model.copy().add( Example.class, Example.factory ); EnhGraph eg = new EnhGraph( g, ours ); Node n = NodeFactory.createURI( "spoo:bar" ); EnhNode eNode = new EnhNode( NodeFactory.createURI( "spoo:bar" ), eg ); EnhNode eBlank = new EnhNode( NodeFactory.createBlankNode(), eg ); assertTrue( "URI node can be an Example", eNode.supports( Example.class ) ); assertFalse( "Blank node cannot be an Example", eBlank.supports( Example.class ) ); }