Revision 26898

View differences:

trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/selection/SelectionIterator.java
1
package com.iver.cit.gvsig.fmap.selection;
2

  
3
import java.util.BitSet;
4
import java.util.NoSuchElementException;
5

  
6
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7
import com.hardcode.gdbms.engine.data.DataSource;
8
import com.hardcode.gdbms.engine.values.Value;
9
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
10

  
11
/**
12
 * <p>Convenience class which allows to access a selection of rows in a DataSource,
13
 * using an Iterator interface (instead of the painful BitSet interface).</p>
14
 * 
15
 * <p>This class is intended to smooth transition to 2.0 version, in which
16
 * random access has been replaced by iterators.</p>
17
 * 
18
 * <p>Warning: This object is not thread-safe!!! Use external synchronization
19
 * if you plan to use it from different threads, otherwise it is warranted
20
 * to fail!!</p>
21
 * 
22
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es> 18/02/2009
23
 * @author IVER T.I. <http://www.iver.es> 18/02/2009
24
 *
25
 */
26
public class SelectionIterator
27
		implements ResettableIterator<Value[]>, Cloneable {
28
	DataSource ds = null;
29
	BitSet selectedRows = null;
30
	int currBit;
31
	boolean hasNext;
32
	boolean usedNext = true;
33

  
34
	/**
35
	 * <p>Creates a new Iterator, which is able to access the provided
36
	 * <code>selection</code> from <code>ds</code>.</p>
37
	 * 
38
	 * @param ds
39
	 * @param selection The selection of rows to iterate through
40
	 */
41
	public SelectionIterator(DataSource ds, BitSet selection) {
42
		this.selectedRows = (BitSet) selection.clone();
43
		this.ds = ds;
44
		reset();
45
	}
46

  
47
	public boolean hasNext() {
48
		if (usedNext==true) {
49
			currBit = selectedRows.nextSetBit(currBit+1);
50
			if (currBit<0) {
51
				hasNext = false;
52
			}
53
			else {
54
				hasNext = true; 
55
			}
56
			usedNext = false;
57
		}
58
		return hasNext;
59
	}
60

  
61
	public Value[] next() {
62
		if (!hasNext()) {
63
			throw new NoSuchElementException();
64
		}
65
		usedNext = true;
66
		try {
67
			return ds.getRow(currBit);	
68
		} catch (ReadDriverException e) {
69
			NoSuchElementException e1 = new NoSuchElementException();
70
			e1.initCause(e);
71
			throw e1;
72
		}
73
	}
74

  
75
	public void remove() {
76
		throw new UnsupportedOperationException();
77
	}
78

  
79
	public void reset() {
80
		currBit = -1;
81
		usedNext = true;
82
	}
83

  
84
	public Object clone() throws CloneNotSupportedException {
85
		SelectionFieldIterator obj = (SelectionFieldIterator) super.clone();
86
		obj.reset();
87
		return obj;
88
	}
89
}
0 90

  
trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/selection/SelectionFieldIterator.java
1
package com.iver.cit.gvsig.fmap.selection;
2

  
3
import java.util.BitSet;
4
import java.util.NoSuchElementException;
5

  
6
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7
import com.hardcode.gdbms.engine.data.DataSource;
8
import com.hardcode.gdbms.engine.values.Value;
9
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
10
/**
11
 * <p>Allows to access a field from a selection of rows in a DataSource,
12
 * using an Iterator interface (instead of the painful BitSet interface).</p>
13
 * 
14
 * <p>This class is intended to smooth transition to 2.0 version, in which
15
 * random access has been replaced by iterators.</p>
16
 * 
17
 * <p>Warning: This object is not thread-safe!!! Use external synchronization
18
 * if you plan to use it from different threads, otherwise it is warranted
19
 * to fail!!</p>
20
 * 
21
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es> 18/02/2009
22
 * @author IVER T.I. <http://www.iver.es> 18/02/2009
23
 */
24
public class SelectionFieldIterator
25
		implements ResettableIterator<Value>, Cloneable {
26
	DataSource ds = null;
27
	BitSet selectedRows = null;
28
	int fieldIndex;
29
	int currBit;
30
	int nextSet;
31
	boolean hasNext;
32
	boolean usedNext = true;
33

  
34
	/**
35
	 * <p>Creates a new Iterator, which is able to access the provided
36
	 * <code>selection</code> from <code>ds</code>.</p>
37
	 * 
38
	 * @param ds 
39
	 * @param selection The selection of rows to iterate through
40
	 * @param fieldIndex The index of the field to iterate on
41
	 */
42
	public SelectionFieldIterator(DataSource ds, BitSet selection, int fieldIndex) {
43
		this.selectedRows = selection;
44
		this.ds = ds;
45
		this.fieldIndex = fieldIndex;
46
		reset();
47
	}
48

  
49
	/**
50
	 * <p>Creates a new Iterator, which is able to access the provided
51
	 * <code>selection</code> from <code>ds</code>.</p>
52
	 * 
53
	 * @param ds 
54
	 * @param selection The selection of rows to iterate through
55
	 * @param fieldIndex The name of the field to iterate on
56
	 */
57
	public SelectionFieldIterator(SelectableDataSource ds, BitSet selection, String fieldName) throws ReadDriverException {
58
		this(ds, selection, ds.getFieldIndexByName(fieldName));
59
	}
60

  
61
	public boolean hasNext() {
62
		if (usedNext==true) {
63
			currBit = selectedRows.nextSetBit(currBit+1);
64
			if (currBit<0) {
65
				hasNext = false;
66
			}
67
			else {
68
				hasNext = true; 
69
			}
70
			usedNext = false;
71
		}
72
		return hasNext;
73
	}
74

  
75
	public Value next() {
76
		if (!hasNext()) {
77
			throw new NoSuchElementException();
78
		}
79
		usedNext = true;
80
		try {
81
			return ds.getFieldValue(currBit, fieldIndex);	
82
		} catch (ReadDriverException e) {
83
			NoSuchElementException e1 = new NoSuchElementException();
84
			e1.initCause(e);
85
			throw e1;
86
		}
87
	}
88

  
89
	public void remove() {
90
		throw new UnsupportedOperationException();
91
	}
92

  
93
	public void reset() {
94
		currBit = -1;
95
		usedNext = true;
96
	}
97

  
98
	public Object clone() throws CloneNotSupportedException {
99
		SelectionFieldIterator obj = (SelectionFieldIterator) super.clone();
100
		obj.reset();
101
		return obj;
102
	}
103
}
0 104

  
trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/selection/ResettableIterator.java
1
package com.iver.cit.gvsig.fmap.selection;
2

  
3
import java.util.Iterator;
4

  
5
/**
6
 * <p>Extends the Iterator Interface, allowing to reset the
7
 * Iterator. Calling
8
 * <code>next();</code> after a <code>reset();</code> 
9
 * will get the first element in
10
 * the iterator.</p>
11
 * 
12
 * @author IVER T.I. <http://www.iver.es> 23/02/2009
13
 * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es> 23/02/2009
14
 *
15
 * @param <E>
16
 */
17
public interface ResettableIterator<E> extends Iterator<E> {
18
	/**
19
	 * <p>Sets the iterator to its initial state. Calling
20
	 * <code>next();</code> after a <code>reset();</code> 
21
	 * will get the first element in
22
	 * the iterator.</p>.
23
	 */
24
	public void reset();
25
}
0 26

  

Also available in: Unified diff