/** * 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(); }
/** * 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(); }
/** 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() ); }
/** 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() ); }
/** 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 ); }
/** 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 ); }
public void testAsThrowsPolymorphismExceptionIfNoModel() { Resource r = ResourceFactory.createResource(); try { r.as( Example.class ); fail( "should throw UnsupportedPolymorphismException" ); } catch (UnsupportedPolymorphismException e) { assertTrue( e.getBadNode() instanceof EnhNode ); assertEquals( null, ((EnhNode)e.getBadNode()).getGraph() ); assertEquals( Example.class, e.getBadClass() ); } }
public void testAsThrowsPolymorphismExceptionIfNoModel() { Resource r = ResourceFactory.createResource(); try { r.as( Example.class ); fail( "should throw UnsupportedPolymorphismException" ); } catch (UnsupportedPolymorphismException e) { assertTrue( e.getBadNode() instanceof EnhNode ); assertEquals( null, ((EnhNode)e.getBadNode()).getGraph() ); assertEquals( Example.class, e.getBadClass() ); } }
/** 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() ); } }
/** 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() ); } }