001: /*****************************************************************************
002: * Source code information
003: * -----------------------
004: * Original author Ian Dickinson, HP Labs Bristol
005: * Author email Ian.Dickinson@hp.com
006: * Package Jena 2
007: * Web http://sourceforge.net/projects/jena/
008: * Created 4 Mar 2003
009: * Filename $RCSfile: CompositionBase.java,v $
010: * Revision $Revision: 1.15 $
011: * Release status $State: Exp $
012: *
013: * Last modified on $Date: 2008/01/02 12:10:19 $
014: * by $Author: andy_seaborne $
015: *
016: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
017: * (see footer for full conditions)
018: *****************************************************************************/package com.hp.hpl.jena.graph.compose;
019:
020: // Imports
021: ///////////////
022: import com.hp.hpl.jena.graph.*;
023: import com.hp.hpl.jena.graph.impl.*;
024: import com.hp.hpl.jena.util.IteratorCollection;
025: import com.hp.hpl.jena.util.iterator.*;
026:
027: import java.util.*;
028:
029: /**
030: * <p>
031: * Base class for graphs that are composed of multiple sub-graphs. This is to provide
032: * a home for shared functionality that was previously in {@link Dyadic} before
033: * refactoring.
034: * </p>
035: *
036: * @author Ian Dickinson, moved kers' code from Dyadic to this class, added commentage
037: * @author Chris Dollin (kers)
038: * @version CVS $Id: CompositionBase.java,v 1.15 2008/01/02 12:10:19 andy_seaborne Exp $
039: */
040: public abstract class CompositionBase extends GraphBase {
041: /**
042: * <p>
043: * Answer a {@link Filter} that will reject any element that is a member of iterator i.
044: * As a side-effect, i will be closed.
045: * </p>
046: *
047: * @param i A closable iterator
048: * @return A Filter that will accept any object not a member of i.
049: */
050: public static Filter reject(final ClosableIterator i) {
051: final Set suppress = IteratorCollection.iteratorToSet(i);
052: return new Filter() {
053: public boolean accept(Object o) {
054: return !suppress.contains(o);
055: }
056: };
057: }
058:
059: /**
060: * <p>
061: * Answer an iterator over the elements of iterator a that are not members of iterator b.
062: * As a side-effect, iterator b will be closed.
063: * </p>
064: *
065: * @param a An iterator that will be filtered by rejecting the elements of b
066: * @param b A closable iterator
067: * @return The iteration of elements in a but not in b.
068: */
069: public static ClosableIterator butNot(final ClosableIterator a,
070: final ClosableIterator b) {
071: return new FilterIterator(reject(b), a);
072: }
073:
074: /**
075: * <p>
076: * Answer an iterator that will record every element delived by <code>next()</code> in
077: * the set <code>seen</code>.
078: * </p>
079: *
080: * @param i A closable iterator
081: * @param seen A set that will record each element of i in turn
082: * @return An iterator that records the elements of i.
083: */
084: public static ExtendedIterator recording(final ClosableIterator i,
085: final Set seen) {
086: return new NiceIterator() {
087: public void remove() {
088: i.remove();
089: }
090:
091: public boolean hasNext() {
092: return i.hasNext();
093: }
094:
095: public Object next() {
096: Object x = i.next();
097: try {
098: seen.add(x);
099: } catch (OutOfMemoryError e) {
100: throw e;
101: }
102: return x;
103: }
104:
105: public void close() {
106: i.close();
107: }
108: };
109: }
110:
111: //static final Object absent = new Object();
112:
113: /**
114: * <p>
115: * Answer an iterator over the elements of iterator i that are not in the set <code>seen</code>.
116: * </p>
117: *
118: * @param i An extended iterator
119: * @param seen A set of objects
120: * @return An iterator over the elements of i that are not in the set <code>seen</code>.
121: */
122: public static ExtendedIterator rejecting(final ExtendedIterator i,
123: final Set seen) {
124: Filter seenFilter = new Filter() {
125: public boolean accept(Object x) {
126: return seen.contains(x);
127: }
128: };
129: return i.filterDrop(seenFilter);
130: }
131:
132: /**
133: Answer an iterator over the elements of <code>i</code> that are not in
134: the graph <code>seen</code>.
135: */
136: public static ExtendedIterator rejecting(final ExtendedIterator i,
137: final Graph seen) {
138: Filter seenFilter = new Filter() {
139: public boolean accept(Object x) {
140: return seen.contains((Triple) x);
141: }
142: };
143: return i.filterDrop(seenFilter);
144: }
145:
146: /**
147: * <p>
148: * Answer a {@link Filter} that will accept any object that is an element of
149: * iterator i. As a side-effect, i will be evaluated and closed.
150: * </p>
151: *
152: * @param i A closable iterator
153: * @return A Filter that will accept any object in iterator i.
154: */
155: public static Filter ifIn(final ClosableIterator i) {
156: final Set allow = IteratorCollection.iteratorToSet(i);
157: return new Filter() {
158: public boolean accept(Object x) {
159: return allow.contains(x);
160: }
161: };
162: }
163:
164: /**
165: * <p>
166: * Answer a {@link Filter} that will accept any triple that is an edge of
167: * graph g.
168: * </p>
169: *
170: * @param g A graph
171: * @return A Filter that will accept any triple that is an edge in g.
172: */
173: public static Filter ifIn(final Graph g) {
174: return new Filter() {
175: public boolean accept(Object x) {
176: return g.contains((Triple) x);
177: }
178: };
179: }
180:
181: // Internal implementation methods
182: //////////////////////////////////
183:
184: //==============================================================================
185: // Inner class definitions
186: //==============================================================================
187:
188: }
189:
190: /*
191: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
192: All rights reserved.
193:
194: Redistribution and use in source and binary forms, with or without
195: modification, are permitted provided that the following conditions
196: are met:
197:
198: 1. Redistributions of source code must retain the above copyright
199: notice, this list of conditions and the following disclaimer.
200:
201: 2. Redistributions in binary form must reproduce the above copyright
202: notice, this list of conditions and the following disclaimer in the
203: documentation and/or other materials provided with the distribution.
204:
205: 3. The name of the author may not be used to endorse or promote products
206: derived from this software without specific prior written permission.
207:
208: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
209: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
210: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
213: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
214: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
215: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
216: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
217: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
218: */
|