KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > impl > FragmentTripleIterator


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: FragmentTripleIterator.java,v 1.9 2005/02/21 11:52:10 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.impl;
8
9 import com.hp.hpl.jena.graph.*;
10 import com.hp.hpl.jena.util.iterator.*;
11
12 import java.util.*;
13
14 /**
15      Iterator which delivers all the triples from a Fragment's map.
16 */

17 public abstract class FragmentTripleIterator extends NiceIterator
18     {
19     private final GraphAddList pending;
20     private final Iterator it;
21     
22     /**
23         An iterator over all the reification triples buried in <code>it</code> that match
24         <code>match</code>. The elements of the iterator are either Triples
25         or Fragmentss.
26     */

27     public FragmentTripleIterator( Triple match, Iterator it )
28         {
29         super();
30         this.it = it;
31         this.pending = new GraphAddList( match );
32         }
33
34             
35     /**
36         Answer true iff there are any triples left, ie, there are some triples in the pending
37         list once we've refilled.
38         @return true iff there are more triples to come
39     */

40     public boolean hasNext()
41         {
42         refill();
43         return pending.size() > 0;
44         }
45         
46     /**
47         Answer the next triple in the iteration, if one exists. The triples are stored in the
48         pending list and removed from its end (which we hope is efficient).
49         @return the next triple
50         @throws NoSuchElementException if there isn't one
51     */

52     public Object JavaDoc next()
53         {
54         ensureHasNext();
55         return pending.remove( pending.size() - 1 );
56         }
57         
58     /**
59         Add all the [implied, matching] triples from the Object into the GraphAdd
60         entity (which will be our list). It would be nice if we could create an interface
61         for the fragmentObject's type.
62     */

63     protected abstract void fill( GraphAdd ga, Node n, Object JavaDoc fragmentsObject );
64         
65     /**
66         Refill the pending list. Keep trying until either there are some elements
67         to be found in it, or we've run out of elements in the original iterator.
68     */

69     private void refill()
70         { while (pending.size() == 0 && it.hasNext()) refillFrom( pending, it.next() ); }
71
72     /**
73          Refill the buffer <code>pending</code> from the iterator element
74          <code>next</code>. The default behaviour is to assume that this object is
75          a Map.Entry; over-ride if it's something else.
76     */

77     protected void refillFrom( GraphAdd pending, Object JavaDoc next )
78         {
79         Map.Entry e = (Map.Entry) next;
80         fill( pending, (Node) e.getKey(), e.getValue() );
81         }
82 }
83
84 /*
85     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
86     All rights reserved.
87
88     Redistribution and use in source and binary forms, with or without
89     modification, are permitted provided that the following conditions
90     are met:
91
92     1. Redistributions of source code must retain the above copyright
93        notice, this list of conditions and the following disclaimer.
94
95     2. Redistributions in binary form must reproduce the above copyright
96        notice, this list of conditions and the following disclaimer in the
97        documentation and/or other materials provided with the distribution.
98
99     3. The name of the author may not be used to endorse or promote products
100        derived from this software without specific prior written permission.
101
102     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
103     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
104     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
105     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
106     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
107     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
108     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
109     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
110     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
111     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
112 */
Popular Tags