Skip to content

Commit

Permalink
Changing indentation to two spaces. No logical change.
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Sep 13, 2018
1 parent a0ffc71 commit c43c9d9
Show file tree
Hide file tree
Showing 3 changed files with 512 additions and 513 deletions.
310 changes: 155 additions & 155 deletions src/main/java/org/apache/ibatis/cursor/defaults/DefaultCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,188 +37,188 @@
*/
public class DefaultCursor<T> implements Cursor<T> {

// ResultSetHandler stuff
private final DefaultResultSetHandler resultSetHandler;
private final ResultMap resultMap;
private final ResultSetWrapper rsw;
private final RowBounds rowBounds;
private final ObjectWrapperResultHandler<T> objectWrapperResultHandler = new ObjectWrapperResultHandler<>();

private final CursorIterator cursorIterator = new CursorIterator();
private boolean iteratorRetrieved;

private CursorStatus status = CursorStatus.CREATED;
private int indexWithRowBound = -1;

private enum CursorStatus {

/**
* A freshly created cursor, database ResultSet consuming has not started
*/
CREATED,
/**
* A cursor currently in use, database ResultSet consuming has started
*/
OPEN,
/**
* A closed cursor, not fully consumed
*/
CLOSED,
/**
* A fully consumed cursor, a consumed cursor is always closed
*/
CONSUMED
// ResultSetHandler stuff
private final DefaultResultSetHandler resultSetHandler;
private final ResultMap resultMap;
private final ResultSetWrapper rsw;
private final RowBounds rowBounds;
private final ObjectWrapperResultHandler<T> objectWrapperResultHandler = new ObjectWrapperResultHandler<>();

private final CursorIterator cursorIterator = new CursorIterator();
private boolean iteratorRetrieved;

private CursorStatus status = CursorStatus.CREATED;
private int indexWithRowBound = -1;

private enum CursorStatus {

/**
* A freshly created cursor, database ResultSet consuming has not started
*/
CREATED,
/**
* A cursor currently in use, database ResultSet consuming has started
*/
OPEN,
/**
* A closed cursor, not fully consumed
*/
CLOSED,
/**
* A fully consumed cursor, a consumed cursor is always closed
*/
CONSUMED
}

public DefaultCursor(DefaultResultSetHandler resultSetHandler, ResultMap resultMap, ResultSetWrapper rsw, RowBounds rowBounds) {
this.resultSetHandler = resultSetHandler;
this.resultMap = resultMap;
this.rsw = rsw;
this.rowBounds = rowBounds;
}

@Override
public boolean isOpen() {
return status == CursorStatus.OPEN;
}

@Override
public boolean isConsumed() {
return status == CursorStatus.CONSUMED;
}

@Override
public int getCurrentIndex() {
return rowBounds.getOffset() + cursorIterator.iteratorIndex;
}

@Override
public Iterator<T> iterator() {
if (iteratorRetrieved) {
throw new IllegalStateException("Cannot open more than one iterator on a Cursor");
}

public DefaultCursor(DefaultResultSetHandler resultSetHandler, ResultMap resultMap, ResultSetWrapper rsw, RowBounds rowBounds) {
this.resultSetHandler = resultSetHandler;
this.resultMap = resultMap;
this.rsw = rsw;
this.rowBounds = rowBounds;
if (isClosed()) {
throw new IllegalStateException("A Cursor is already closed.");
}

@Override
public boolean isOpen() {
return status == CursorStatus.OPEN;
iteratorRetrieved = true;
return cursorIterator;
}

@Override
public void close() {
if (isClosed()) {
return;
}

@Override
public boolean isConsumed() {
return status == CursorStatus.CONSUMED;
}
ResultSet rs = rsw.getResultSet();
try {
if (rs != null) {
Statement statement = rs.getStatement();

@Override
public int getCurrentIndex() {
return rowBounds.getOffset() + cursorIterator.iteratorIndex;
rs.close();
if (statement != null) {
statement.close();
}
}
status = CursorStatus.CLOSED;
} catch (SQLException e) {
// ignore
}
}

@Override
public Iterator<T> iterator() {
if (iteratorRetrieved) {
throw new IllegalStateException("Cannot open more than one iterator on a Cursor");
}
if (isClosed()) {
throw new IllegalStateException("A Cursor is already closed.");
}
iteratorRetrieved = true;
return cursorIterator;
protected T fetchNextUsingRowBound() {
T result = fetchNextObjectFromDatabase();
while (result != null && indexWithRowBound < rowBounds.getOffset()) {
result = fetchNextObjectFromDatabase();
}
return result;
}

@Override
public void close() {
if (isClosed()) {
return;
}
protected T fetchNextObjectFromDatabase() {
if (isClosed()) {
return null;
}

ResultSet rs = rsw.getResultSet();
try {
if (rs != null) {
Statement statement = rs.getStatement();

rs.close();
if (statement != null) {
statement.close();
}
}
status = CursorStatus.CLOSED;
} catch (SQLException e) {
// ignore
}
try {
status = CursorStatus.OPEN;
resultSetHandler.handleRowValues(rsw, resultMap, objectWrapperResultHandler, RowBounds.DEFAULT, null);
} catch (SQLException e) {
throw new RuntimeException(e);
}

protected T fetchNextUsingRowBound() {
T result = fetchNextObjectFromDatabase();
while (result != null && indexWithRowBound < rowBounds.getOffset()) {
result = fetchNextObjectFromDatabase();
}
return result;
T next = objectWrapperResultHandler.result;
if (next != null) {
indexWithRowBound++;
}
// No more object or limit reached
if (next == null || getReadItemsCount() == rowBounds.getOffset() + rowBounds.getLimit()) {
close();
status = CursorStatus.CONSUMED;
}
objectWrapperResultHandler.result = null;

protected T fetchNextObjectFromDatabase() {
if (isClosed()) {
return null;
}
return next;
}

try {
status = CursorStatus.OPEN;
resultSetHandler.handleRowValues(rsw, resultMap, objectWrapperResultHandler, RowBounds.DEFAULT, null);
} catch (SQLException e) {
throw new RuntimeException(e);
}
private boolean isClosed() {
return status == CursorStatus.CLOSED || status == CursorStatus.CONSUMED;
}

T next = objectWrapperResultHandler.result;
if (next != null) {
indexWithRowBound++;
}
// No more object or limit reached
if (next == null || getReadItemsCount() == rowBounds.getOffset() + rowBounds.getLimit()) {
close();
status = CursorStatus.CONSUMED;
}
objectWrapperResultHandler.result = null;
private int getReadItemsCount() {
return indexWithRowBound + 1;
}

return next;
}
private static class ObjectWrapperResultHandler<T> implements ResultHandler<T> {

private boolean isClosed() {
return status == CursorStatus.CLOSED || status == CursorStatus.CONSUMED;
}
private T result;

private int getReadItemsCount() {
return indexWithRowBound + 1;
@Override
public void handleResult(ResultContext<? extends T> context) {
this.result = context.getResultObject();
context.stop();
}
}

private static class ObjectWrapperResultHandler<T> implements ResultHandler<T> {

private T result;
private class CursorIterator implements Iterator<T> {

@Override
public void handleResult(ResultContext<? extends T> context) {
this.result = context.getResultObject();
context.stop();
}
}
/**
* Holder for the next object to be returned
*/
T object;

private class CursorIterator implements Iterator<T> {
/**
* Index of objects returned using next(), and as such, visible to users.
*/
int iteratorIndex = -1;

/**
* Holder for the next object to be returned
*/
T object;
@Override
public boolean hasNext() {
if (object == null) {
object = fetchNextUsingRowBound();
}
return object != null;
}

/**
* Index of objects returned using next(), and as such, visible to users.
*/
int iteratorIndex = -1;
@Override
public T next() {
// Fill next with object fetched from hasNext()
T next = object;

@Override
public boolean hasNext() {
if (object == null) {
object = fetchNextUsingRowBound();
}
return object != null;
}
if (next == null) {
next = fetchNextUsingRowBound();
}

@Override
public T next() {
// Fill next with object fetched from hasNext()
T next = object;

if (next == null) {
next = fetchNextUsingRowBound();
}

if (next != null) {
object = null;
iteratorIndex++;
return next;
}
throw new NoSuchElementException();
}
if (next != null) {
object = null;
iteratorIndex++;
return next;
}
throw new NoSuchElementException();
}

@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove element from Cursor");
}
@Override
public void remove() {
throw new UnsupportedOperationException("Cannot remove element from Cursor");
}
}
}
Loading

0 comments on commit c43c9d9

Please sign in to comment.