KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > impl > ContainerImpl


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

6
7 package com.hp.hpl.jena.rdf.model.impl;
8
9 import com.hp.hpl.jena.rdf.model.*;
10 import com.hp.hpl.jena.vocabulary.RDF;
11
12 import com.hp.hpl.jena.graph.*;
13 import com.hp.hpl.jena.enhanced.*;
14 import com.hp.hpl.jena.shared.*;
15
16 import java.util.*;
17
18 /** An internal class not normally of interest to application developers.
19  * A base class on which the other containers are built.
20  *
21  * @author bwm, kers
22  * @version $Id: ContainerImpl.java,v 1.14 2005/02/21 12:14:32 andy_seaborne Exp $
23 */

24
25 public class ContainerImpl extends ResourceImpl
26                            implements Container, ContainerI {
27     
28     static NodeIteratorFactory iteratorFactory;
29     
30     static {
31         iteratorFactory = new ContNodeIteratorFactoryImpl();
32     }
33
34     /** Creates new ContainerImpl */
35     public ContainerImpl( ModelCom model ) {
36         super(model);
37     }
38     
39     public ContainerImpl( String JavaDoc uri, ModelCom model ){
40         super(uri, model);
41     }
42     
43     public ContainerImpl(Resource r, ModelCom model) {
44         super(r.asNode(), model);
45     }
46     
47     public ContainerImpl(Node n, EnhGraph g) {
48         super(n,g);
49     }
50     
51     protected ContainerImpl( Resource r )
52         { this( r, (ModelCom) r.getModel() ); }
53     
54     private boolean is( Resource r ) {
55         return hasProperty(RDF.type, r);
56     }
57     
58     public boolean isAlt() {
59         return is(RDF.Alt);
60     }
61     
62     public boolean isBag() {
63         return is(RDF.Bag);
64     }
65     
66     public boolean isSeq() {
67         return is(RDF.Seq);
68     }
69     
70     public Container add(RDFNode n) {
71         int i = size();
72         addProperty(RDF.li(i+1), n);
73         return this;
74     }
75     
76     public Container add(boolean o) {
77         return add( String.valueOf( o ) );
78     }
79     
80     public Container add(long o) {
81         return add( String.valueOf( o ) );
82     }
83     
84     public Container add(char o) {
85         return add( String.valueOf( o ) );
86     }
87     
88     public Container add(float o) {
89         return add( String.valueOf( o ) );
90     }
91     
92     public Container add(double o) {
93         return add( String.valueOf( o ) );
94     }
95
96     public Container add(Object JavaDoc o) {
97         return add( String.valueOf( o ) );
98     }
99      
100     public Container add(String JavaDoc o) {
101         return add( o, "" );
102     }
103     
104     public Container add(String JavaDoc o, String JavaDoc l) {
105         return add( literal( o, l ) );
106     }
107     
108     public boolean contains(RDFNode n) {
109         return containerContains( n );
110     }
111
112     public boolean contains(boolean o) {
113         return contains( String.valueOf( o ) );
114     }
115
116     public boolean contains(long o) {
117         return contains( String.valueOf( o ) );
118     }
119
120     public boolean contains(char o) {
121         return contains( String.valueOf( o ) );
122     }
123
124     public boolean contains(float o) {
125         return contains( String.valueOf( o ) );
126     }
127
128     public boolean contains(double o) {
129         return contains( String.valueOf( o ) );
130     }
131
132     public boolean contains(Object JavaDoc o) {
133         return contains( String.valueOf( o ) );
134     }
135     
136     public boolean contains(String JavaDoc o) {
137         return contains( o, "" );
138     }
139     
140     public boolean contains( String JavaDoc o, String JavaDoc l ) {
141         return contains( literal( o, l ) );
142     }
143
144     private Literal literal( String JavaDoc s, String JavaDoc lang )
145         { return new LiteralImpl( Node.createLiteral( s, lang, false ), getModelCom() ); }
146             
147     public NodeIterator iterator()
148         { return listContainerMembers( iteratorFactory ); }
149         
150     public int size()
151         {
152         int result = 0;
153         StmtIterator iter = listProperties();
154         while (iter.hasNext())
155             if (iter.nextStatement().getPredicate().getOrdinal() != 0) result += 1;
156         iter.close();
157         return result;
158         }
159         
160     public Container remove(Statement s) {
161         int size = size();
162         Statement last = null;
163         if (s.getPredicate().getOrdinal() == size) { // if last
164
getModel().remove(s);
165         } else {
166             last = getModel().getRequiredProperty(this, RDF.li(size));
167             s.changeObject(last.getObject());
168             getModel().remove(last);
169         }
170         if (size() != (size -1))
171             throw new AssertionFailureException( "container size" );
172         return this;
173     }
174     
175     public Container remove(int index, RDFNode object) {
176         remove(getModel().createStatement(this, RDF.li(index), object));
177         return this;
178     }
179         
180    /**
181         Answer an iterator over the members of this container.
182         @param f the factory for constructing the final iterator
183         @return the member iterator
184    */

185    public NodeIterator listContainerMembers( NodeIteratorFactory f )
186         {
187         StmtIterator iter = listProperties();
188         Vector result = new Vector();
189         int maxOrdinal = 0;
190         while (iter.hasNext()) {
191             Statement stmt = iter.nextStatement();
192             int ordinal = stmt.getPredicate().getOrdinal();
193             if (ordinal != 0) {
194                 if (ordinal > maxOrdinal) {
195                     maxOrdinal = ordinal;
196                     result.setSize(ordinal);
197                 }
198                 result.setElementAt(stmt, ordinal-1);
199             }
200         }
201         iter.close();
202         return f.createIterator( result.iterator(), result, this );
203     }
204     
205     public int containerIndexOf( RDFNode n ) {
206         int result = 0;
207         StmtIterator iter = listProperties();
208         while (iter.hasNext()) {
209             Statement stmt = iter.nextStatement();
210             int ordinal = stmt.getPredicate().getOrdinal();
211             if (ordinal != 0 && n.equals( stmt.getObject() )) {
212                 result = ordinal;
213                 break;
214             }
215         }
216         iter.close();
217         return result;
218     }
219     
220    public boolean containerContains( RDFNode n)
221         { return containerIndexOf( n ) != 0; }
222             
223 }
224
225 /*
226  * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
227  * All rights reserved.
228  *
229  * Redistribution and use in source and binary forms, with or without
230  * modification, are permitted provided that the following conditions
231  * are met:
232  * 1. Redistributions of source code must retain the above copyright
233  * notice, this list of conditions and the following disclaimer.
234  * 2. Redistributions in binary form must reproduce the above copyright
235  * notice, this list of conditions and the following disclaimer in the
236  * documentation and/or other materials provided with the distribution.
237  * 3. The name of the author may not be used to endorse or promote products
238  * derived from this software without specific prior written permission.
239
240  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
241  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
242  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
243  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
244  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
245  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
246  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
249  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
250  *
251  * ContainerImpl.java
252  *
253  * Created on 08 August 2000, 16:33
254  */

255
Popular Tags