KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > ontology > daml > impl > DAMLListImpl


1 /*****************************************************************************
2  * Source code information
3  * -----------------------
4  * Original author Ian Dickinson, HP Labs Bristol
5  * Author email Ian.Dickinson@hp.com
6  * Package Jena
7  * Created 4 Jan 2001
8  * Filename $RCSfile: DAMLListImpl.java,v $
9  * Revision $Revision: 1.13 $
10  * Release status Preview-release $State: Exp $
11  *
12  * Last modified on $Date: 2005/02/21 12:05:23 $
13  * by $Author: andy_seaborne $
14  *
15  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
16  * (see footer for full conditions)
17  *****************************************************************************/

18
19 // Package
20
///////////////
21
package com.hp.hpl.jena.ontology.daml.impl;
22
23
24 // Imports
25
///////////////
26
import com.hp.hpl.jena.rdf.model.*;
27 import com.hp.hpl.jena.rdf.model.impl.RDFListImpl;
28 import com.hp.hpl.jena.enhanced.*;
29 import com.hp.hpl.jena.graph.*;
30 import com.hp.hpl.jena.ontology.*;
31 import com.hp.hpl.jena.ontology.daml.*;
32 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
33 import com.hp.hpl.jena.vocabulary.*;
34
35
36
37
38 /**
39  * <p>Java representation of a DAML List. A list is the specified interpretation of
40  * rdf:parseType="daml:Collection" attributes, where a sequence of values is
41  * interpreted as a nested sequence of head/tail list cells. One consequence of this
42  * is that the list is quite specifically ordered, whereas the daml:collection
43  * is said to be an unordered collection. Consquently, we must caution that future
44  * versions of the DAML specificiation may create an unordered interpretation of
45  * daml:collection, and client code should not rely on the positionality of elements
46  * in the current list interpretation.</p>
47  *
48  * @author Ian Dickinson, HP Labs (<a HREF="mailto:Ian.Dickinson@hp.com">email</a>)
49  * @version CVS info: $Id: DAMLListImpl.java,v 1.13 2005/02/21 12:05:23 andy_seaborne Exp $
50  */

51 public class DAMLListImpl
52     extends RDFListImpl
53     implements DAMLList
54 {
55     // Constants
56
//////////////////////////////////
57

58
59     // Static variables
60
//////////////////////////////////
61

62     /**
63      * A factory for generating DAMLList facets from nodes in enhanced graphs.
64      * Note: should not be invoked directly by user code: use
65      * {@link com.hp.hpl.jena.rdf.model.RDFNode#as as()} instead.
66      */

67     public static Implementation factory = new Implementation() {
68         public EnhNode wrap( Node n, EnhGraph eg ) {
69             if (canWrap( n, eg )) {
70                 return new DAMLListImpl( n, eg );
71             }
72             else {
73                 throw new ConversionException( "Cannot convert node " + n.toString() + " to DAMLList" );
74             }
75         }
76             
77         public boolean canWrap( Node node, EnhGraph eg ) {
78             // node will support being an RDFList facet if it has rdf:type rdf:List or equivalent
79
Graph g = eg.asGraph();
80             
81             // node will support being an RDFList facet if it has rdf:type rdf:List, is nil, or is in the domain of a list property
82
return node.equals( DAML_OIL.nil.asNode() ) ||
83                     g.find( node, DAML_OIL.first.asNode(), Node.ANY ).hasNext() ||
84                     g.find( node, DAML_OIL.rest.asNode(), Node.ANY ).hasNext() ||
85                     g.find( node, RDF.type.asNode(), DAML_OIL.List.asNode() ).hasNext();
86         }
87     };
88
89
90
91     // Instance variables
92
//////////////////////////////////
93

94     
95     
96     // Constructors
97
//////////////////////////////////
98

99     /**
100      * <p>
101      * Construct a DAML list represented by the given node in the given graph.
102      * </p>
103      *
104      * @param n The node that represents the resource
105      * @param g The enh graph that contains n
106      */

107     public DAMLListImpl( Node n, EnhGraph g ) {
108         super( n, g );
109     }
110
111
112
113
114     // External signature methods
115
//////////////////////////////////
116

117     // vocabulary terms
118
public Resource listType() { return DAML_OIL.List; }
119     public Resource listNil() { return DAML_OIL.nil; }
120     public Property listFirst() { return DAML_OIL.first; }
121     public Property listRest() { return DAML_OIL.rest; }
122     public Class JavaDoc listAbstractionClass() { return DAMLList.class; }
123
124
125     /**
126      * Answer an iteration over the values in the list.
127      *
128      * @return An iterator over the DAML values in the list
129      */

130     public ExtendedIterator getAll() {
131         return iterator();
132     }
133
134
135     /**
136      * <p>Answer the value at the head of the list. Since, strictly speaking, DAML collections
137      * are unordered, the position items in the list should not be relied on in client
138      * code, as the definition of 'first' in the list may change in future releases.
139      * However, the identity
140      * <code><pre>
141      * List L = L.getFirst() + L.getRest()
142      * </pre></code>
143      * is guaranteed, providing that the contents of L do not change.
144      *
145      * @return The first value in the list.
146      */

147     public RDFNode getFirst() {
148         return getHead();
149     }
150
151
152     /**
153      * <p>Answer the list that consists of all values of the list save the first. Since, strictly
154      * speaking, DAML collections are unordered, this corresponds to returning the collection
155      * minus an unspecified one of its values. However, the identity
156      * <code><pre>
157      * List L = L.getFirst() + L.getRest()
158      * </pre></code>
159      * is guaranteed, providing that the contents of L do not change.</p>
160      *
161      * @return a list that contains all the elements of the current list, save the first one.
162      */

163     public DAMLList getRest() {
164         return (DAMLList) getTail().as( DAMLList.class );
165     }
166
167
168     /**
169      * <p>Answer a count of the items in the list. Does not check for duplications, so this
170      * is the count of entries in the list, not the count of distinct items in the list.</p>
171      *
172      * @return The number of entries in the list
173      */

174     public int getCount() {
175         return size();
176     }
177
178
179     /**
180      * <p>Set the property <code>daml:first</code> for the given list element. This is a single
181      * value that denotes the value at this position of the list.</p>
182      *
183      * @param value The value to be assigned to the 'first' property of a list cell
184      */

185     public void setFirst( DAMLCommon value ) {
186         setHead( value );
187     }
188
189
190     /**
191      * <p>Set the property <code>daml:rest</code> for the given list element. This is a single
192      * value that denotes the tail of the list.</p>
193      *
194      * @param tail The list that will be made the tail of this list
195      */

196     public void setRest( DAMLList tail ) {
197         setTail( tail );
198     }
199
200
201     /**
202      * <p>Set the property <code>daml:rest</code> for the given list element to be the
203      * nil list. This correctly terminates the list at this point.</p>
204      */

205     public void setRestNil() {
206         setRest( getNil() );
207     }
208
209
210     /**
211      * <p>Answer a new list formed by creating a new DAMLList element whose first is the
212      * given value and whose rest is the current list. This is the 'cons' operator
213      * familiar from other list processing languages.</p>
214      *
215      * @param value The new value to be added to the head of the list
216      * @return A new list whose <code>daml:first</code> is the value, and whose
217      * <code>daml:rest</code> is this list.
218      */

219     public DAMLList cons( DAMLCommon value ) {
220         return (DAMLList) super.cons( value ).as( DAMLList.class );
221     }
222
223
224     /**
225      * <p>Answer the well-known constant denoting the nil list.</p>
226      *
227      * @return The resource denoting nil
228      */

229     public DAMLList getNil() {
230         return (DAMLList) listNil().as( DAMLList.class );
231     }
232
233
234     /**
235      * <p>Answer true if the given resource is the nil list.</p>
236      *
237      * @param resource The resource to be tested
238      * @return true if the resource is the nil list
239      */

240     public boolean isNil( Resource resource ) {
241         return resource.equals( listNil() );
242     }
243
244
245     /**
246      * <p>Find the last list element, i.e. the one whose rest is nil.</p>
247      *
248      * @return A list element
249      */

250     public DAMLList findLast() {
251         return (DAMLList) findElement( true, 0 ).as( DAMLList.class );
252     }
253
254
255     /**
256      * <p>Answer the i'th element of the list, if it exists. If i is
257      * less than 1, or is larger than the length of the list, throw
258      * an illegal argument exception.</p>
259      *
260      * @param i The position of the list to return
261      * @return The DAML value at the i'th position in the list
262      * @exception ListIndexException if i is less than one, or
263      * larger than the length of the list.
264      */

265     public DAMLCommon getItem( int i ) {
266         return (DAMLCommon) get( i ).as( DAMLCommon.class );
267     }
268
269
270     // Internal implementation methods
271
//////////////////////////////////
272

273
274
275
276     //==============================================================================
277
// Inner class definitions
278
//==============================================================================
279

280
281 }
282
283
284 /*
285     (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
286     All rights reserved.
287
288     Redistribution and use in source and binary forms, with or without
289     modification, are permitted provided that the following conditions
290     are met:
291
292     1. Redistributions of source code must retain the above copyright
293        notice, this list of conditions and the following disclaimer.
294
295     2. Redistributions in binary form must reproduce the above copyright
296        notice, this list of conditions and the following disclaimer in the
297        documentation and/or other materials provided with the distribution.
298
299     3. The name of the author may not be used to endorse or promote products
300        derived from this software without specific prior written permission.
301
302     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
303     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
304     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
305     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
306     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
307     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
308     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
309     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
310     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
311     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
312 */

313
314
Popular Tags