KickJava   Java API By Example, From Geeks To Geeks.

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


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

6
7 package com.hp.hpl.jena.enhanced;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.rdf.model.*;
11
12 /**
13  * <p>
14  * A specialisation of Polymorphic that models an extended node in a an extended graph. An extended node
15  * wraps a normal node, and adds additional convenience access or user affordances, though the state
16  * remains in the graph itself.
17  * </p>
18  * @author <a HREF="mailto:Jeremy.Carroll@hp.com">Jeremy Carroll</a> (original code)<br>
19  * <a HREF="mailto:Chris.Dollin@hp.com">Chris Dollin</a> (original code)<br>
20  * <a HREF="mailto:Ian.Dickinson@hp.com">Ian Dickinson</a> (refactoring and commentage)
21 */

22 public class EnhNode extends Polymorphic implements FrontsNode
23 {
24     
25     /** The graph node that this enhanced node is wrapping */
26     final protected Node node;
27     
28     /** The enhanced graph containing this node */
29     final protected EnhGraph enhGraph;
30     
31     public EnhNode( Node n, EnhGraph g ) {
32         super();
33         node=n;
34         enhGraph = g;
35    }
36
37     // External interface
38

39     /**
40      * Answer the graph node that this enhanced node wraps
41      * @return A plain node
42      */

43     public Node asNode() {
44         return node;
45     }
46     
47     /**
48      * Answer the graph containing this node
49      * @return An enhanced graph
50      */

51     public EnhGraph getGraph() {
52         return enhGraph;
53     }
54     
55     /**
56      * Answer a facet of this node, where that facet is denoted by the
57      * given type.
58      *
59      * @param t A type denoting the desired facet of the underlying node
60      * @return An enhanced nodet that corresponds to t; this may be <i>this</i>
61      * Java object, or a different object.
62      */

63     public EnhNode viewAs( Class JavaDoc t ) {
64         return (EnhNode) asInternal( t );
65     }
66     
67     /** allow subclasses to implement RDFNode & its subinterface */
68     public RDFNode as( Class JavaDoc t )
69         { return (RDFNode) viewAs( t ); }
70       
71     /**
72         API-level method for polymorphic testing
73     */

74     public boolean canAs( Class JavaDoc t )
75         { return canSupport( t ); }
76         
77     /**
78      * The hash code of an enhanced node is defined to be the same as the underlying node.
79      * @return The hashcode as an int
80      */

81     final public int hashCode() {
82         return node.hashCode();
83     }
84        
85     /**
86      * An enhanced node is equal to another enhanced node n iff the underlying
87      * nodes are equal. We generalise to allow the other object to be any class
88      * implementing HasNode, because we allow other implemementations of
89      * Resource than EnhNodes, at least in principle.
90      * This is deemed to be a complete and correct interpretation of enhanced node
91      * equality, which is why this method has been marked final.
92      *
93      * @param o An object to test for equality with this node
94      * @return True if o is equal to this node.
95      */

96     final public boolean equals( Object JavaDoc o )
97         { return o instanceof FrontsNode && node.equals(((FrontsNode) o).asNode()); }
98     
99     public boolean isValid()
100         { return true; }
101         
102     /**
103      * Answer an enhanced node object that presents <i>this</i> in a way which satisfies type
104      * t.
105      * @param t A type
106      * @return A polymorphic instance that conforms to t.
107      */

108     protected Polymorphic convertTo( Class JavaDoc t )
109         {
110         EnhGraph eg = getGraph();
111         Implementation imp = getPersonality().getImplementation( t );
112         if (imp == null) throw new UnsupportedPolymorphismException( eg, t );
113         Polymorphic result = imp.wrap( asNode(), eg );
114         this.addView( result );
115         return result;
116         }
117     
118     /**
119         answer true iff this enhanced node can support the class _t_, ie, it can be
120         converted to an instance of t.
121         @param t the (interface) class being tested
122         @return true iff the implementation can wrap this enhanced node
123     */

124     protected boolean canSupport( Class JavaDoc t )
125         {
126         return getPersonality().getImplementation( t ).canWrap( asNode(), getGraph() );
127         }
128
129     /**
130      * Answer the personality object bound to this enhanced node, which we obtain from
131      * the associated enhanced graph.
132      *
133      * @return The personality object
134      */

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

170
Popular Tags