001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: ContainerImpl.java,v 1.18 2008/01/02 12:05:02 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.rdf.model.impl;
008:
009: import com.hp.hpl.jena.rdf.model.*;
010: import com.hp.hpl.jena.vocabulary.RDF;
011:
012: import com.hp.hpl.jena.graph.*;
013: import com.hp.hpl.jena.enhanced.*;
014: import com.hp.hpl.jena.shared.*;
015:
016: import java.util.*;
017:
018: /** An internal class not normally of interest to application developers.
019: * A base class on which the other containers are built.
020: *
021: * @author bwm, kers
022: * @version $Id: ContainerImpl.java,v 1.18 2008/01/02 12:05:02 andy_seaborne Exp $
023: */
024:
025: public class ContainerImpl extends ResourceImpl implements Container,
026: ContainerI {
027:
028: static NodeIteratorFactory iteratorFactory;
029:
030: static {
031: iteratorFactory = new ContNodeIteratorFactoryImpl();
032: }
033:
034: /** Creates new ContainerImpl */
035: public ContainerImpl(ModelCom model) {
036: super (model);
037: }
038:
039: public ContainerImpl(String uri, ModelCom model) {
040: super (uri, model);
041: }
042:
043: public ContainerImpl(Resource r, ModelCom model) {
044: super (r.asNode(), model);
045: }
046:
047: public ContainerImpl(Node n, EnhGraph g) {
048: super (n, g);
049: }
050:
051: protected ContainerImpl(Resource r) {
052: this (r, (ModelCom) r.getModel());
053: }
054:
055: private boolean is(Resource r) {
056: return hasProperty(RDF.type, r);
057: }
058:
059: public boolean isAlt() {
060: return is(RDF.Alt);
061: }
062:
063: public boolean isBag() {
064: return is(RDF.Bag);
065: }
066:
067: public boolean isSeq() {
068: return is(RDF.Seq);
069: }
070:
071: public Container add(RDFNode n) {
072: int i = size();
073: addProperty(RDF.li(i + 1), n);
074: return this ;
075: }
076:
077: public Container add(boolean o) {
078: return add(String.valueOf(o));
079: }
080:
081: public Container add(long o) {
082: return add(String.valueOf(o));
083: }
084:
085: public Container add(char o) {
086: return add(String.valueOf(o));
087: }
088:
089: public Container add(float o) {
090: return add(String.valueOf(o));
091: }
092:
093: public Container add(double o) {
094: return add(String.valueOf(o));
095: }
096:
097: public Container add(Object o) {
098: return add(String.valueOf(o));
099: }
100:
101: public Container add(String o) {
102: return add(o, "");
103: }
104:
105: public Container add(String o, String l) {
106: return add(literal(o, l));
107: }
108:
109: public boolean contains(RDFNode n) {
110: return containerContains(n);
111: }
112:
113: public boolean contains(boolean o) {
114: return contains(String.valueOf(o));
115: }
116:
117: public boolean contains(long o) {
118: return contains(String.valueOf(o));
119: }
120:
121: public boolean contains(char o) {
122: return contains(String.valueOf(o));
123: }
124:
125: public boolean contains(float o) {
126: return contains(String.valueOf(o));
127: }
128:
129: public boolean contains(double o) {
130: return contains(String.valueOf(o));
131: }
132:
133: public boolean contains(Object o) {
134: return contains(String.valueOf(o));
135: }
136:
137: public boolean contains(String o) {
138: return contains(o, "");
139: }
140:
141: public boolean contains(String o, String l) {
142: return contains(literal(o, l));
143: }
144:
145: private Literal literal(String s, String lang) {
146: return new LiteralImpl(Node.createLiteral(s, lang, false),
147: getModelCom());
148: }
149:
150: public NodeIterator iterator() {
151: return listContainerMembers(iteratorFactory);
152: }
153:
154: public int size() {
155: int result = 0;
156: StmtIterator iter = listProperties();
157: while (iter.hasNext())
158: if (iter.nextStatement().getPredicate().getOrdinal() != 0)
159: result += 1;
160: iter.close();
161: return result;
162: }
163:
164: public Container remove(Statement s) {
165: int size = size();
166: Statement last = null;
167: if (s.getPredicate().getOrdinal() == size) { // if last
168: getModel().remove(s);
169: } else {
170: last = getModel().getRequiredProperty(this , RDF.li(size));
171: s.changeObject(last.getObject());
172: getModel().remove(last);
173: }
174: if (size() != (size - 1))
175: throw new AssertionFailureException("container size");
176: return this ;
177: }
178:
179: public Container remove(int index, RDFNode object) {
180: remove(getModel().createStatement(this , RDF.li(index), object));
181: return this ;
182: }
183:
184: /**
185: Answer an iterator over the members of this container.
186: @param f the factory for constructing the final iterator
187: @return the member iterator
188: */
189: public NodeIterator listContainerMembers(NodeIteratorFactory f) {
190: StmtIterator iter = listProperties();
191: Vector result = new Vector();
192: int maxOrdinal = 0;
193: while (iter.hasNext()) {
194: Statement stmt = iter.nextStatement();
195: int ordinal = stmt.getPredicate().getOrdinal();
196: if (ordinal != 0) {
197: if (ordinal > maxOrdinal) {
198: maxOrdinal = ordinal;
199: result.setSize(ordinal);
200: }
201: result.setElementAt(stmt, ordinal - 1);
202: }
203: }
204: iter.close();
205: return f.createIterator(result.iterator(), result, this );
206: }
207:
208: public int containerIndexOf(RDFNode n) {
209: int result = 0;
210: StmtIterator iter = listProperties();
211: while (iter.hasNext()) {
212: Statement stmt = iter.nextStatement();
213: int ordinal = stmt.getPredicate().getOrdinal();
214: if (ordinal != 0 && n.equals(stmt.getObject())) {
215: result = ordinal;
216: break;
217: }
218: }
219: iter.close();
220: return result;
221: }
222:
223: public boolean containerContains(RDFNode n) {
224: return containerIndexOf(n) != 0;
225: }
226:
227: }
228:
229: /*
230: * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
231: * All rights reserved.
232: *
233: * Redistribution and use in source and binary forms, with or without
234: * modification, are permitted provided that the following conditions
235: * are met:
236: * 1. Redistributions of source code must retain the above copyright
237: * notice, this list of conditions and the following disclaimer.
238: * 2. Redistributions in binary form must reproduce the above copyright
239: * notice, this list of conditions and the following disclaimer in the
240: * documentation and/or other materials provided with the distribution.
241: * 3. The name of the author may not be used to endorse or promote products
242: * derived from this software without specific prior written permission.
243:
244: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
245: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
246: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
248: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
249: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
250: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
253: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
254: *
255: * ContainerImpl.java
256: *
257: * Created on 08 August 2000, 16:33
258: */
|