Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make batch iterator a generic class #667

Merged
merged 1 commit into from
Aug 11, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
* objects requested from the database at once, and may differ from the number
* of objects returned by <b>nextBatch()</b>, for instance if the batch size is
* changed after fetching, or if <b>filtersBatches()</b> is set to true.
*
* @param <E> the type of elements returned by this iterator
*/
public class ERXFetchSpecificationBatchIterator implements Iterator, Enumeration {
public class ERXFetchSpecificationBatchIterator<E> implements Iterator<E>, Enumeration<E> {

/** holds the default batch size, any bigger than this an Oracle has a fit */
public static final int DefaultBatchSize = 250;
Expand All @@ -65,7 +67,7 @@ public class ERXFetchSpecificationBatchIterator implements Iterator, Enumeration
protected NSArray primaryKeys;

/** holds array of fetched but not-yet-returned objects; used by the Iterator and Enumeration interfaces */
protected NSMutableArray cachedBatch;
protected NSMutableArray<E> cachedBatch;
/** holds the number of objects fetched */
protected int currentObjectFetchCount;
/** determines whether we should re-apply the original qualifier to each batch of objects fetched */
Expand Down Expand Up @@ -123,12 +125,12 @@ public ERXFetchSpecificationBatchIterator(EOFetchSpecification fetchSpecificatio
super();

EOEntity entity = ERXEOAccessUtilities.entityNamed(ec, fetchSpecification.entityName());
NSArray primaryKeyAttributes = entity.primaryKeyAttributes();
NSArray<EOAttribute> primaryKeyAttributes = entity.primaryKeyAttributes();
if (primaryKeyAttributes.count() > 1) {
throw new RuntimeException("ERXFetchSpecificationBatchIterator: Currently only single primary key entities are supported.");
}

primaryKeyAttributeName = ((EOAttribute)primaryKeyAttributes.lastObject()).name();
primaryKeyAttributeName = primaryKeyAttributes.lastObject().name();
this.fetchSpecification = (EOFetchSpecification) fetchSpecification.clone();
primaryKeys = pkeys;
setEditingContext(ec != null ? ec : ERXEC.newEditingContext());
Expand Down Expand Up @@ -276,9 +278,9 @@ protected boolean _hasMoreToFetch() {
* time they were fetched.)
* @return batch of enterprise objects
*/
public NSArray nextBatch() {
public NSArray<E> nextBatch() {
if(cachedBatch != null) {
NSArray nextBatch = cachedBatch;
NSArray<E> nextBatch = cachedBatch;
cachedBatch = null;
return nextBatch;
}
Expand All @@ -292,10 +294,10 @@ public NSArray nextBatch() {
* automatic support for the Iterator and Enumeration interfaces.
* @return next batch
*/
protected NSArray _fetchNextBatch() {
protected NSArray<E> _fetchNextBatch() {
if (hasNextBatch()) {
NSRange range = _rangeForOffset(currentObjectFetchCount);
NSArray nextBatch = batchWithRange(range);
NSArray<E> nextBatch = batchWithRange(range);
currentObjectFetchCount += range.length();
return nextBatch;
}
Expand Down Expand Up @@ -328,7 +330,7 @@ private NSRange _rangeForOffset(int start) {
* @param index index of batch to retrieve
* @return batch of enterprise objects
*/
public NSArray batchWithIndex(int index) {
public NSArray<E> batchWithIndex(int index) {
NSRange range = _rangeForBatchIndex(index);
return batchWithRange(range);
}
Expand All @@ -347,13 +349,13 @@ public NSArray batchWithIndex(int index) {
* @param requestedRange range of batch to retrieve
* @return batch of enterprise objects
*/
public NSArray batchWithRange(NSRange requestedRange) {
public NSArray<E> batchWithRange(NSRange requestedRange) {
EOEditingContext ec = editingContext();
if ( ec == null) {
throw new IllegalStateException("ERXFetchSpecificationBatchIterator: Calling nextBatch with a null editing context!");
}

NSArray nextBatch = null;
NSArray<E> nextBatch = null;
NSRange range = requestedRange.rangeByIntersectingRange( new NSRange(0, count()) ); //intersect with legal range
if ( range.length() > 0 ) {
NSArray primaryKeys = primaryKeys();
Expand Down Expand Up @@ -456,16 +458,16 @@ public boolean hasNext() {
/**
* Implementation of the Iterator interface
*/
public Object next() {
public E next() {
if( cachedBatch == null) {
NSArray nextBatch = _fetchNextBatch(); //will raise if no more batches, which is expected behavior if next() is called w/o first checking hasNext()
NSArray<E> nextBatch = _fetchNextBatch(); //will raise if no more batches, which is expected behavior if next() is called w/o first checking hasNext()
while(nextBatch.count() == 0 && hasNextBatch()) { //if filtersBatches, we can get empty batches, so repeat until we get something, or run out
nextBatch = _fetchNextBatch();
}
cachedBatch = nextBatch.mutableClone();
}

Object nextObject = null;
E nextObject = null;
if( cachedBatch.count() > 0 ) {
nextObject = cachedBatch.removeObjectAtIndex(0);
}
Expand All @@ -492,7 +494,7 @@ public boolean hasMoreElements() {
/**
* Implementation of the Enumeration interface
*/
public Object nextElement() {
public E nextElement() {
return next();
}

Expand Down