1 6 7 package com.hp.hpl.jena.enhanced; 8 9 10 17 public abstract class Polymorphic { 18 19 20 private Polymorphic ring; 21 22 25 Polymorphic() 26 { this.ring = this; } 27 28 30 34 protected abstract Personality getPersonality(); 35 36 42 protected boolean already(Class t) 43 { return t.isInstance( this ); } 44 45 50 public boolean supports( Class t ) 51 { 52 Polymorphic supporter = findExistingView( t ); 53 return supporter != null || this.canSupport( t ); 54 } 55 56 62 protected final Polymorphic asInternal( Class t ) 63 { 64 Polymorphic other = findExistingView( t ); 65 return other == null ? this.convertTo( t ) : other; 66 } 67 68 73 private Polymorphic findExistingView( Class t ) 74 { 75 Polymorphic r = this; 76 for (;;) 77 { 78 if (t.isInstance( r ) && r.isValid()) return r; 79 r = r.ring; 80 if (r == this) return null; 81 } 82 } 83 84 88 public abstract boolean isValid(); 89 90 95 protected abstract Polymorphic convertTo( Class t ); 96 97 101 protected abstract boolean canSupport( Class t ); 102 103 108 public abstract boolean equals( Object o ); 109 110 118 public void addView( Polymorphic other ) 119 { 120 if (other.ring == other) 121 { 122 other.ring = this.ring; 123 this.ring = other; 124 } 125 else 126 throw new AlreadyLinkedViewException( other ); 127 } 128 129 } 130 131 160 | Popular Tags |