KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > enhanced > Polymorphic


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: Polymorphic.java,v 1.7 2005/02/21 12:03:40 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.enhanced;
8
9
10 /**
11  * Abstract base class for all polymorphic RDF objects, especially enhanced node and enhanced graph.
12  *
13  * @author <a HREF="mailto:Jeremy.Carroll@hp.com">Jeremy Carroll</a> (original code)<br>
14  * <a HREF="mailto:Chris.Dollin@hp.com">Chris Dollin</a> (original code)<br>
15  * <a HREF="mailto:Ian.Dickinson@hp.com">Ian Dickinson</a> (tidying up and comments)
16  */

17 public abstract class Polymorphic {
18     
19     /** Each new polymorphic object is in a ring of views */
20     private Polymorphic ring;
21     
22     /**
23         initially we're in the singleton ring.
24      */

25     Polymorphic()
26         { this.ring = this; }
27
28     // External contract methods
29

30     /**
31      * Answer the personality object bound to this polymorphic instance
32      * @return The personality object
33      */

34     protected abstract Personality getPersonality();
35         
36     /**
37      * Answer true iff <i>this</i> is already acceptable to the given type t.
38      * Delegates to t to test if this class is an acceptable implementation.
39      * @param t A type to test
40      * @return True iff this object is already an acceptable implementation of t
41      */

42     protected boolean already(Class JavaDoc t)
43         { return t.isInstance( this ); }
44
45     /**
46         return _true_ iff this polymorphic object supports the specified interface.
47         Synonymous with "does the argument class have this as an instance".
48         Actually it shouldn't be. Review.
49     */

50     public boolean supports( Class JavaDoc t )
51         {
52         Polymorphic supporter = findExistingView( t );
53         return supporter != null || this.canSupport( t );
54         }
55         
56     /**
57      * Answer a polymorphic object that presents <i>this</i> in a way which satisfies type
58      * t.
59      * @param t A type
60      * @return A polymorphic instance, possibly but not necessarily this, that conforms to t.
61      */

62     protected final Polymorphic asInternal( Class JavaDoc t )
63         {
64         Polymorphic other = findExistingView( t );
65         return other == null ? this.convertTo( t ) : other;
66         }
67         
68     /**
69         find an existing view in the ring which is an instance of _t_ and
70         return it; otherwise return null. If _this_ is an instance, the
71         search takes care to find it first.
72     */

73     private Polymorphic findExistingView( Class JavaDoc 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     /**
85         answer true iff this enhanced node is still underpinned in the graph
86         by triples appropriate to its type.
87     */

88     public abstract boolean isValid();
89         
90     /**
91         subclasses must provide a method for converting _this_, if
92         possible, into an instance of _t_. It will only be called if _this_
93         doesn't already have (or be) a suitable ring-sibling.
94     */

95     protected abstract Polymorphic convertTo( Class JavaDoc t );
96     
97     /**
98         subclasses must provide a method for testing if _this_ can be
99         converted to an instance of _t_.
100     */

101     protected abstract boolean canSupport( Class JavaDoc t );
102     
103     /**
104         subclasses must override equals. Actually they may not have
105         to nowadays ... I have expunged the clever facet-identity test
106         (and indeed facets).
107     */

108     public abstract boolean equals( Object JavaDoc o );
109     
110     /**
111         add another view for this object. <code>other</code> must be freshly constructed.
112         To be called by subclasses when they have constructed a new view
113         for this object.
114         
115         This method is public ONLY so that it can be tested.
116         TODO find a better way to make it testable.
117     */

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 /*
132     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
133     All rights reserved.
134
135     Redistribution and use in source and binary forms, with or without
136     modification, are permitted provided that the following conditions
137     are met:
138
139     1. Redistributions of source code must retain the above copyright
140        notice, this list of conditions and the following disclaimer.
141
142     2. Redistributions in binary form must reproduce the above copyright
143        notice, this list of conditions and the following disclaimer in the
144        documentation and/or other materials provided with the distribution.
145
146     3. The name of the author may not be used to endorse or promote products
147        derived from this software without specific prior written permission.
148
149     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
150     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
151     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
152     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
153     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
154     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
155     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
156     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
157     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
158     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
159 */

160
Popular Tags