View Javadoc

1   /*
2    * Copyright (c) 2007, James Leigh All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.sesame.iterators;
30  
31  import info.aduna.iteration.CloseableIteration;
32  
33  import java.io.Closeable;
34  import java.util.Iterator;
35  
36  import org.openrdf.elmo.exceptions.ElmoPersistException;
37  import org.openrdf.elmo.exceptions.ElmoIOException;
38  import org.openrdf.elmo.exceptions.ElmoException;
39  
40  /**
41   * A general purpose iteration wrapping Sesame's iterations. This class converts
42   * the results, converts the Exceptions into ElmoRuntimeExeptions, and ensures
43   * that the iteration is closed when all values have been read (on {{@link #next()}).
44   * 
45   * @author James Leigh
46   * 
47   * @param <S>
48   *            Type of the delegate (Statement)
49   * @param <E>
50   *            Type of the result
51   */
52  public abstract class ElmoIteration<S, E> implements Iterator<E>, Closeable,
53  		CloseableIteration<E, ElmoException> {
54  
55  	public static void close(Iterator<?> iter) {
56  		try {
57  			if (iter instanceof CloseableIteration)
58  				((CloseableIteration<?,?>) iter).close();
59  		} catch (RuntimeException e) {
60  			throw e;
61  		} catch (Exception e) {
62  			throw new ElmoIOException(e);
63  		}
64  	}
65  
66  	private CloseableIteration<? extends S, ?> delegate;
67  
68  	private S element;
69  
70  	public ElmoIteration(CloseableIteration<? extends S, ?> delegate) {
71  		this.delegate = delegate;
72  		if (!hasNext())
73  			close();
74  	}
75  
76  	public boolean hasNext() {
77  		try {
78  			return delegate.hasNext();
79  		} catch (RuntimeException e) {
80  			throw e;
81  		} catch (Exception e) {
82  			throw new ElmoIOException(e);
83  		}
84  	}
85  
86  	public E next() {
87  		try {
88  			E next = convert(element = delegate.next());
89  			if (!delegate.hasNext())
90  				close();
91  			return next;
92  		} catch (RuntimeException e) {
93  			throw e;
94  		} catch (Exception e) {
95  			throw new ElmoIOException(e);
96  		}
97  	}
98  
99  	public void remove() {
100 		try {
101 			remove(element);
102 		} catch (RuntimeException e) {
103 			throw e;
104 		} catch (Exception e) {
105 			throw new ElmoPersistException(e);
106 		}
107 	}
108 
109 	public void close() {
110 		try {
111 			delegate.close();
112 		} catch (RuntimeException e) {
113 			throw e;
114 		} catch (Exception e) {
115 			throw new ElmoIOException(e);
116 		}
117 	}
118 
119 	protected abstract E convert(S element) throws Exception;
120 
121 	protected void remove(S element) throws Exception {
122 		throw new UnsupportedOperationException();
123 	}
124 
125 }