001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: SeqImpl.java,v 1.25 2008/01/02 12:05:05 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: import com.hp.hpl.jena.graph.*;
012:
013: import com.hp.hpl.jena.enhanced.*;
014:
015: /** An implementation of Seq
016: *
017: * @author bwm
018: * @version Release='$Name: $' Revision='$Revision: 1.25 $' Date='$Date: 2008/01/02 12:05:05 $'
019: */
020:
021: public class SeqImpl extends ContainerImpl implements Seq {
022:
023: final static public Implementation factory = new Implementation() {
024: public boolean canWrap(Node n, EnhGraph eg) {
025: return true;
026: }
027:
028: public EnhNode wrap(Node n, EnhGraph eg) {
029: return new SeqImpl(n, eg);
030: }
031: };
032:
033: static NodeIteratorFactory seqIteratorFactory = new SeqNodeIteratorFactoryImpl();
034:
035: /** Creates new SeqMem */
036: public SeqImpl(ModelCom model) {
037: super (model);
038: }
039:
040: public SeqImpl(String uri, ModelCom model) {
041: super (uri, model);
042: }
043:
044: public SeqImpl(Resource r, ModelCom m) {
045: super (r, m);
046: }
047:
048: public SeqImpl(Node n, EnhGraph g) {
049: super (n, g);
050: }
051:
052: public Resource getResource(int index) {
053: return getRequiredProperty(RDF.li(index)).getResource();
054: }
055:
056: public Literal getLiteral(int index) {
057: return getRequiredProperty(RDF.li(index)).getLiteral();
058: }
059:
060: public RDFNode getObject(int index) {
061: return getRequiredProperty(RDF.li(index)).getObject();
062: }
063:
064: public boolean getBoolean(int index) {
065: checkIndex(index);
066: return getRequiredProperty(RDF.li(index)).getBoolean();
067: }
068:
069: public byte getByte(int index) {
070: checkIndex(index);
071: return getRequiredProperty(RDF.li(index)).getByte();
072: }
073:
074: public short getShort(int index) {
075: checkIndex(index);
076: return getRequiredProperty(RDF.li(index)).getShort();
077: }
078:
079: public int getInt(int index) {
080: checkIndex(index);
081: return getRequiredProperty(RDF.li(index)).getInt();
082: }
083:
084: public long getLong(int index) {
085: checkIndex(index);
086: return getRequiredProperty(RDF.li(index)).getLong();
087: }
088:
089: public char getChar(int index) {
090: checkIndex(index);
091: return getRequiredProperty(RDF.li(index)).getChar();
092: }
093:
094: public float getFloat(int index) {
095: checkIndex(index);
096: return getRequiredProperty(RDF.li(index)).getFloat();
097: }
098:
099: public double getDouble(int index) {
100: checkIndex(index);
101: return getRequiredProperty(RDF.li(index)).getDouble();
102: }
103:
104: public String getString(int index) {
105: checkIndex(index);
106: return getRequiredProperty(RDF.li(index)).getString();
107: }
108:
109: public String getLanguage(int index) {
110: checkIndex(index);
111: return getRequiredProperty(RDF.li(index)).getLanguage();
112: }
113:
114: public Object getObject(int index, ObjectF f) {
115: return getRequiredProperty(RDF.li(index)).getObject(f);
116: }
117:
118: public Resource getResource(int index, ResourceF f) {
119: return getRequiredProperty(RDF.li(index)).getResource(f);
120: }
121:
122: public Bag getBag(int index) {
123: checkIndex(index);
124: return getRequiredProperty(RDF.li(index)).getBag();
125: }
126:
127: public Alt getAlt(int index) {
128: checkIndex(index);
129: return getRequiredProperty(RDF.li(index)).getAlt();
130: }
131:
132: public Seq getSeq(int index) {
133: checkIndex(index);
134: return getRequiredProperty(RDF.li(index)).getSeq();
135: }
136:
137: public Seq set(int index, RDFNode o) {
138: checkIndex(index);
139: getRequiredProperty(RDF.li(index)).changeObject(o);
140: return this ;
141: }
142:
143: public Seq set(int index, boolean o) {
144: checkIndex(index);
145: getRequiredProperty(RDF.li(index)).changeLiteralObject(o);
146: return this ;
147: }
148:
149: public Seq set(int index, long o) {
150: checkIndex(index);
151: getRequiredProperty(RDF.li(index)).changeLiteralObject(o);
152: return this ;
153: }
154:
155: public Seq set(int index, float o) {
156: checkIndex(index);
157: getRequiredProperty(RDF.li(index)).changeLiteralObject(o);
158: return this ;
159: }
160:
161: public Seq set(int index, double o) {
162: checkIndex(index);
163: getRequiredProperty(RDF.li(index)).changeLiteralObject(o);
164: return this ;
165: }
166:
167: public Seq set(int index, char o) {
168: checkIndex(index);
169: getRequiredProperty(RDF.li(index)).changeLiteralObject(o);
170: return this ;
171: }
172:
173: public Seq set(int index, String o) {
174: checkIndex(index);
175: getRequiredProperty(RDF.li(index)).changeObject(o);
176: return this ;
177: }
178:
179: public Seq set(int index, String o, String l) {
180: checkIndex(index);
181: getRequiredProperty(RDF.li(index)).changeObject(o, l);
182: return this ;
183: }
184:
185: public Seq set(int index, Object o) {
186: checkIndex(index);
187: getRequiredProperty(RDF.li(index)).changeObject(o);
188: return this ;
189: }
190:
191: public Seq add(int index, boolean o) {
192: return add(index, String.valueOf(o));
193: }
194:
195: public Seq add(int index, long o) {
196: return add(index, String.valueOf(o));
197: }
198:
199: public Seq add(int index, char o) {
200: return add(index, String.valueOf(o));
201: }
202:
203: public Seq add(int index, float o) {
204: return add(index, String.valueOf(o));
205: }
206:
207: public Seq add(int index, double o) {
208: return add(index, String.valueOf(o));
209: }
210:
211: public Seq add(int index, Object o) {
212: return add(index, String.valueOf(o));
213: }
214:
215: public Seq add(int index, String o) {
216: return add(index, o, "");
217: }
218:
219: public Seq add(int index, String o, String l) {
220: return add(index, literal(o, l));
221: }
222:
223: public Seq add(int index, RDFNode o) {
224: int size = size();
225: checkIndex(index, size + 1);
226: shiftUp(index, size);
227: addProperty(RDF.li(index), o);
228: return this ;
229: }
230:
231: public NodeIterator iterator() {
232: return listContainerMembers(seqIteratorFactory);
233: }
234:
235: public Container remove(Statement s) {
236: // System.err.println( "]] SeqImpl.remove " + s );
237: getModel().remove(s);
238: // System.err.println( "]] SeqImpl.remove - about to shift down " + (s.getPredicate().getOrdinal()+1) + " to " + (size()+1) );
239: shiftDown(s.getPredicate().getOrdinal() + 1, size() + 1);
240: // System.err.println( "]] SeqImpl.remov completed" );
241: return this ;
242: }
243:
244: public Seq remove(int index) {
245: getRequiredProperty(RDF.li(index)).remove();
246: shiftDown(index + 1, size() + 1);
247: return this ;
248: }
249:
250: public Container remove(int index, RDFNode o) {
251: // System.err.println( "]] SeqImpl::remove( " + index + ", " + o + ")" );
252: return remove(getModel()
253: .createStatement(this , RDF.li(index), o).remove());
254: }
255:
256: public int indexOf(RDFNode o) {
257: return containerIndexOf(o);
258: }
259:
260: public int indexOf(boolean o) {
261: return indexOf(String.valueOf(o));
262: }
263:
264: public int indexOf(long o) {
265: return indexOf(String.valueOf(o));
266: }
267:
268: public int indexOf(char o) {
269: return indexOf(String.valueOf(o));
270: }
271:
272: public int indexOf(float o) {
273: return indexOf(String.valueOf(o));
274: }
275:
276: public int indexOf(double o) {
277: return indexOf(String.valueOf(o));
278: }
279:
280: public int indexOf(Object o) {
281: return indexOf(String.valueOf(o));
282: }
283:
284: public int indexOf(String o) {
285: return indexOf(o, "");
286: }
287:
288: public int indexOf(String o, String l) {
289: return indexOf(literal(o, l));
290: }
291:
292: private Literal literal(String s, String lang) {
293: return new LiteralImpl(Node.createLiteral(s, lang, false),
294: getModelCom());
295: }
296:
297: protected void shiftUp(int start, int finish) {
298: Statement stmt = null;
299: for (int i = finish; i >= start; i--) {
300: stmt = getRequiredProperty(RDF.li(i));
301: getModel().remove(stmt);
302: addProperty(RDF.li(i + 1), stmt.getObject());
303: }
304: }
305:
306: protected void shiftDown(int start, int finish) {
307: for (int i = start; i <= finish; i++) {
308: Statement stmt = getRequiredProperty(RDF.li(i));
309: // System.err.println( "]]* remove " + stmt );
310: stmt.remove();
311: // System.err.println( "]]* addProperty( " + RDF.li(i-1) + " " + stmt.getObject() );
312: addProperty(RDF.li(i - 1), stmt.getObject());
313: }
314: }
315:
316: protected void checkIndex(int index) {
317: checkIndex(index, size());
318: }
319:
320: protected void checkIndex(int index, int max) {
321: if (!(1 <= index && index <= max)) {
322: throw new SeqIndexBoundsException(max, index);
323: }
324: }
325: }
326: /*
327: * (c) Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
328: * All rights reserved.
329: *
330: * Redistribution and use in source and binary forms, with or without
331: * modification, are permitted provided that the following conditions
332: * are met:
333: * 1. Redistributions of source code must retain the above copyright
334: * notice, this list of conditions and the following disclaimer.
335: * 2. Redistributions in binary form must reproduce the above copyright
336: * notice, this list of conditions and the following disclaimer in the
337: * documentation and/or other materials provided with the distribution.
338: * 3. The name of the author may not be used to endorse or promote products
339: * derived from this software without specific prior written permission.
340:
341: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
342: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
343: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
344: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
345: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
346: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
347: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
348: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
349: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
350: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
351: *
352: * SeqImpl.java
353: *
354: * Created on 08 August 2000, 17:10
355: */
|