KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > compose > CompositionBase


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

19
20 // Package
21
///////////////
22
package com.hp.hpl.jena.graph.compose;
23
24
25 // Imports
26
///////////////
27
import com.hp.hpl.jena.graph.*;
28 import com.hp.hpl.jena.graph.impl.*;
29 import com.hp.hpl.jena.util.IteratorCollection;
30 import com.hp.hpl.jena.util.iterator.*;
31
32 import java.util.*;
33
34
35 /**
36  * <p>
37  * Base class for graphs that are composed of multiple sub-graphs. This is to provide
38  * a home for shared functionality that was previously in {@link Dyadic} before
39  * refactoring.
40  * </p>
41  *
42  * @author Ian Dickinson, moved kers' code from Dyadic to this class, added commentage
43  * @author Chris Dollin (kers)
44  * @version CVS $Id: CompositionBase.java,v 1.11 2005/02/21 11:52:02 andy_seaborne Exp $
45  */

46 public abstract class CompositionBase
47     extends GraphBase
48 {
49     /**
50      * <p>
51      * Answer the number of triples in this graph
52      * </p>
53      *
54      * @return The integer triple count
55      * @see com.hp.hpl.jena.graph.Graph#size()
56      */

57     public int graphBaseSize()
58         { return countIterator( GraphUtil.findAll( this ) ); }
59       
60     /**
61      * <p>
62      * Helper to throw an unsupported operation exception. For users whose brains
63      * have been infected by perl.
64      * </p>
65      *
66      * @param message
67      * @exception Throws {@link UnsupportedOperationException}
68      */

69     protected void die( String JavaDoc message )
70         { throw new UnsupportedOperationException JavaDoc( message ); }
71
72     /**
73      * <p>
74      * Answer a {@link Filter} that will reject any element that is a member of iterator i.
75      * As a side-effect, i will be closed.
76      * </p>
77      *
78      * @param i A closable iterator
79      * @return A Filter that will accept any object not a member of i.
80      */

81     public static Filter reject( final ClosableIterator i )
82         {
83         final Set suppress = IteratorCollection.iteratorToSet( i );
84         return new Filter()
85             { public boolean accept( Object JavaDoc o ) { return !suppress.contains( o ); } };
86         }
87         
88     /**
89      * <p>
90      * Answer an iterator over the elements of iterator a that are not members of iterator b.
91      * As a side-effect, iterator b will be closed.
92      * </p>
93      *
94      * @param a An iterator that will be filtered by rejecting the elements of b
95      * @param b A closable iterator
96      * @return The iteration of elements in a but not in b.
97      */

98     public static ClosableIterator butNot( final ClosableIterator a, final ClosableIterator b )
99         {
100         return new FilterIterator( reject( b ), a );
101         }
102         
103     /**
104      * <p>
105      * Answer an iterator that will record every element delived by <code>next()</code> in
106      * the set <code>seen</code>.
107      * </p>
108      *
109      * @param i A closable iterator
110      * @param seen A set that will record each element of i in turn
111      * @return An iterator that records the elements of i.
112      */

113     public static ExtendedIterator recording( final ClosableIterator i, final Set seen )
114         {
115         return new NiceIterator()
116             {
117             public void remove()
118                 { i.remove(); }
119             
120             public boolean hasNext()
121                 { return i.hasNext(); }
122             
123             public Object JavaDoc next()
124                 { Object JavaDoc x = i.next();
125                 try { seen.add( x ); } catch (OutOfMemoryError JavaDoc e) { throw e; } return x; }
126                 
127             public void close()
128                 { i.close(); }
129             };
130         }
131         
132     //static final Object absent = new Object();
133

134     /**
135      * <p>
136      * Answer an iterator over the elements of iterator i that are not in the set <code>seen</code>.
137      * </p>
138      *
139      * @param i An extended iterator
140      * @param seen A set of objects
141      * @return An iterator over the elements of i that are not in the set <code>seen</code>.
142      */

143     public static ExtendedIterator rejecting( final ExtendedIterator i, final Set seen )
144         {
145         Filter seenFilter = new Filter()
146             { public boolean accept( Object JavaDoc x ) { return seen.contains( x ); } };
147         return i.filterDrop( seenFilter );
148         }
149         
150     /**
151          Answer an iterator over the elements of <code>i</code> that are not in
152          the graph <code>seen</code>.
153     */

154     public static ExtendedIterator rejecting( final ExtendedIterator i, final Graph seen )
155         {
156         Filter seenFilter = new Filter()
157             { public boolean accept( Object JavaDoc x ) { return seen.contains( (Triple) x ); } };
158         return i.filterDrop( seenFilter );
159         }
160     
161     /**
162      * <p>
163      * Answer the number of items in the closable iterator i. As a side effect, i
164      * is closed.
165      * </p>
166      *
167      * @param i A closable iterator
168      * @return The number of elements of i
169      */

170     public static int countIterator( ClosableIterator i )
171         {
172         try { int n = 0; while (i.hasNext()) { n += 1; i.next(); } return n; }
173         finally { i.close(); }
174         }
175   
176     /**
177      * <p>
178      * Answer a {@link Filter} that will accept any object that is an element of
179      * iterator i. As a side-effect, i will be evaluated and closed.
180      * </p>
181      *
182      * @param i A closable iterator
183      * @return A Filter that will accept any object in iterator i.
184      */

185     public static Filter ifIn( final ClosableIterator i )
186         {
187         final Set allow = IteratorCollection.iteratorToSet( i );
188         return new Filter()
189             { public boolean accept( Object JavaDoc x ) { return allow.contains( x ); } };
190         }
191         
192     /**
193      * <p>
194      * Answer a {@link Filter} that will accept any triple that is an edge of
195      * graph g.
196      * </p>
197      *
198      * @param g A graph
199      * @return A Filter that will accept any triple that is an edge in g.
200      */

201     public static Filter ifIn( final Graph g )
202         {
203         return new Filter()
204             { public boolean accept( Object JavaDoc x ) { return g.contains( (Triple) x ); } };
205         }
206         
207
208     // Internal implementation methods
209
//////////////////////////////////
210

211
212     //==============================================================================
213
// Inner class definitions
214
//==============================================================================
215

216
217 }
218
219
220 /*
221     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
222     All rights reserved.
223
224     Redistribution and use in source and binary forms, with or without
225     modification, are permitted provided that the following conditions
226     are met:
227
228     1. Redistributions of source code must retain the above copyright
229        notice, this list of conditions and the following disclaimer.
230
231     2. Redistributions in binary form must reproduce the above copyright
232        notice, this list of conditions and the following disclaimer in the
233        documentation and/or other materials provided with the distribution.
234
235     3. The name of the author may not be used to endorse or promote products
236        derived from this software without specific prior written permission.
237
238     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
239     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
240     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
241     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
242     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
243     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
244     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
247     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
248 */

249
250
Popular Tags