001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: EnhNode.java,v 1.19 2008/01/02 12:09:12 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.enhanced;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.rdf.model.*;
011:
012: /**
013: * <p>
014: * A specialisation of Polymorphic that models an extended node in a an extended graph. An extended node
015: * wraps a normal node, and adds additional convenience access or user affordances, though the state
016: * remains in the graph itself.
017: * </p>
018: * @author <a href="mailto:Jeremy.Carroll@hp.com">Jeremy Carroll</a> (original code)<br>
019: * <a href="mailto:Chris.Dollin@hp.com">Chris Dollin</a> (original code)<br>
020: * <a href="mailto:Ian.Dickinson@hp.com">Ian Dickinson</a> (refactoring and commentage)
021: */
022: public class EnhNode extends Polymorphic implements FrontsNode {
023:
024: /** The graph node that this enhanced node is wrapping */
025: final protected Node node;
026:
027: /** The enhanced graph containing this node */
028: final protected EnhGraph enhGraph;
029:
030: public EnhNode(Node n, EnhGraph g) {
031: super ();
032: node = n;
033: enhGraph = g;
034: }
035:
036: // External interface
037:
038: /**
039: * Answer the graph node that this enhanced node wraps
040: * @return A plain node
041: */
042: public Node asNode() {
043: return node;
044: }
045:
046: /**
047: * Answer the graph containing this node
048: * @return An enhanced graph
049: */
050: public EnhGraph getGraph() {
051: return enhGraph;
052: }
053:
054: /**
055: An enhanced node is Anon[ymous] iff its underlying node is Blank.
056: */
057: public final boolean isAnon() {
058: return node.isBlank();
059: }
060:
061: /**
062: An enhanced node is Literal iff its underlying node is too.
063: */
064: public final boolean isLiteral() {
065: return node.isLiteral();
066: }
067:
068: /**
069: An enhanced node is a URI resource iff its underlying node is too.
070: */
071: public final boolean isURIResource() {
072: return node.isURI();
073: }
074:
075: /**
076: An enhanced node is a resource if it's node is a URI node or a blank node.
077: */
078: public final boolean isResource() {
079: return node.isURI() || node.isBlank();
080: }
081:
082: /**
083: * Answer a facet of this node, where that facet is denoted by the
084: * given type.
085: *
086: * @param t A type denoting the desired facet of the underlying node
087: * @return An enhanced nodet that corresponds to t; this may be <i>this</i>
088: * Java object, or a different object.
089: */
090: public EnhNode viewAs(Class t) {
091: return (EnhNode) asInternal(t);
092: }
093:
094: /** allow subclasses to implement RDFNode & its subinterface */
095: public RDFNode as(Class t) {
096: return (RDFNode) viewAs(t);
097: }
098:
099: /**
100: API-level method for polymorphic testing
101: */
102: public boolean canAs(Class t) {
103: return canSupport(t);
104: }
105:
106: /**
107: * The hash code of an enhanced node is defined to be the same as the underlying node.
108: * @return The hashcode as an int
109: */
110: final public int hashCode() {
111: return node.hashCode();
112: }
113:
114: /**
115: * An enhanced node is equal to another enhanced node n iff the underlying
116: * nodes are equal. We generalise to allow the other object to be any class
117: * implementing asNode, because we allow other implemementations of
118: * Resource than EnhNodes, at least in principle.
119: * This is deemed to be a complete and correct interpretation of enhanced node
120: * equality, which is why this method has been marked final.
121: *
122: * @param o An object to test for equality with this node
123: * @return True if o is equal to this node.
124: */
125: final public boolean equals(Object o) {
126: return o instanceof FrontsNode
127: && node.equals(((FrontsNode) o).asNode());
128: }
129:
130: public boolean isValid() {
131: return true;
132: }
133:
134: /**
135: Answer an new enhanced node object that presents <i>this</i> in a way
136: which satisfies type <code>t</code>. The new object is linked into this
137: object's sibling ring. If the node cannot be converted, throw an
138: UnsupportedPolymorphismException.
139: */
140: protected Polymorphic convertTo(Class t) {
141: EnhGraph eg = getGraph();
142: if (eg == null)
143: throw new UnsupportedPolymorphismException(this , t);
144: Implementation imp = getPersonality().getImplementation(t);
145: if (imp == null)
146: throw new UnsupportedPolymorphismException(this , t);
147: Polymorphic result = imp.wrap(asNode(), eg);
148: this .addView(result);
149: return result;
150: }
151:
152: /**
153: answer true iff this enhanced node can support the class <code>t</code>,
154: ie it is already a value <code>t</code> or it can be reimplemented
155: as a <code>t</code> via the graph's personality's implementation.
156: If this node has no graph, answer false.
157: */
158: protected boolean canSupport(Class t) {
159: if (alreadyHasView(t))
160: return true;
161: if (getGraph() == null)
162: return false;
163: Implementation imp = getPersonality().getImplementation(t);
164: return imp == null ? false : imp.canWrap(asNode(), getGraph());
165: }
166:
167: /**
168: * Answer the personality object bound to this enhanced node, which we obtain from
169: * the associated enhanced graph.
170: *
171: * @return The personality object
172: */
173: protected Personality getPersonality() {
174: return ((GraphPersonality) getGraph().getPersonality())
175: .nodePersonality();
176: }
177:
178: }
179:
180: /*
181: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
182: All rights reserved.
183:
184: Redistribution and use in source and binary forms, with or without
185: modification, are permitted provided that the following conditions
186: are met:
187:
188: 1. Redistributions of source code must retain the above copyright
189: notice, this list of conditions and the following disclaimer.
190:
191: 2. Redistributions in binary form must reproduce the above copyright
192: notice, this list of conditions and the following disclaimer in the
193: documentation and/or other materials provided with the distribution.
194:
195: 3. The name of the author may not be used to endorse or promote products
196: derived from this software without specific prior written permission.
197:
198: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
199: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
200: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
201: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
203: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
204: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
205: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
206: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
207: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
208: */
|