Skip to content

Make ResultHandler and ResultContext to generic type. #352

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

Merged
merged 1 commit into from
Feb 22, 2015
Merged
Show file tree
Hide file tree
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 @@ -26,7 +26,7 @@
/**
* @author Clinton Begin
*/
public class DefaultMapResultHandler<K, V> implements ResultHandler {
public class DefaultMapResultHandler<K, V> implements ResultHandler<V> {

private final Map<K, V> mappedResults;
private final String mapKey;
Expand All @@ -42,9 +42,8 @@ public DefaultMapResultHandler(String mapKey, ObjectFactory objectFactory, Objec
}

@Override
public void handleResult(ResultContext context) {
// TODO is that assignment always true?
final V value = (V) context.getResultObject();
public void handleResult(ResultContext<? extends V> context) {
final V value = context.getResultObject();
final MetaObject mo = MetaObject.forObject(value, objectFactory, objectWrapperFactory);
// TODO is that assignment always true?
final K key = (K) mo.getValue(mapKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
/**
* @author Clinton Begin
*/
public class DefaultResultContext implements ResultContext {
public class DefaultResultContext<T> implements ResultContext<T> {

private Object resultObject;
private T resultObject;
private int resultCount;
private boolean stopped;

Expand All @@ -33,7 +33,7 @@ public DefaultResultContext() {
}

@Override
public Object getResultObject() {
public T getResultObject() {
return resultObject;
}

Expand All @@ -47,7 +47,7 @@ public boolean isStopped() {
return stopped;
}

public void nextResultObject(Object resultObject) {
public void nextResultObject(T resultObject) {
resultCount++;
this.resultObject = resultObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @author Clinton Begin
*/
public class DefaultResultHandler implements ResultHandler {
public class DefaultResultHandler implements ResultHandler<Object> {

private final List<Object> list;

Expand All @@ -39,7 +39,7 @@ public DefaultResultHandler(ObjectFactory objectFactory) {
}

@Override
public void handleResult(ResultContext context) {
public void handleResult(ResultContext<? extends Object> context) {
list.add(context.getResultObject());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class DefaultResultSetHandler implements ResultSetHandler {
private final MappedStatement mappedStatement;
private final RowBounds rowBounds;
private final ParameterHandler parameterHandler;
private final ResultHandler resultHandler;
private final ResultHandler<?> resultHandler;
private final BoundSql boundSql;
private final TypeHandlerRegistry typeHandlerRegistry;
private final ObjectFactory objectFactory;
Expand All @@ -87,7 +87,7 @@ private static class PendingRelation {
public ResultMapping propertyMapping;
}

public DefaultResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler, ResultHandler resultHandler, BoundSql boundSql,
public DefaultResultSetHandler(Executor executor, MappedStatement mappedStatement, ParameterHandler parameterHandler, ResultHandler<?> resultHandler, BoundSql boundSql,
RowBounds rowBounds) {
this.executor = executor;
this.configuration = mappedStatement.getConfiguration();
Expand Down Expand Up @@ -260,7 +260,7 @@ private List<Object> collapseSingleResultList(List<Object> multipleResults) {
// HANDLE ROWS FOR SIMPLE RESULTMAP
//

private void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
private void handleRowValues(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
if (resultMap.hasNestedResultMaps()) {
ensureNoRowBounds();
checkResultHandler();
Expand All @@ -285,9 +285,9 @@ protected void checkResultHandler() {
}
}

private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping)
throws SQLException {
DefaultResultContext resultContext = new DefaultResultContext();
DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();
skipRows(rsw.getResultSet(), rowBounds);
while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
ResultMap discriminatedResultMap = resolveDiscriminatedResultMap(rsw.getResultSet(), resultMap, null);
Expand All @@ -296,20 +296,21 @@ private void handleRowValuesForSimpleResultMap(ResultSetWrapper rsw, ResultMap r
}
}

private void storeObject(ResultHandler resultHandler, DefaultResultContext resultContext, Object rowValue, ResultMapping parentMapping, ResultSet rs) throws SQLException {
private void storeObject(ResultHandler<?> resultHandler, DefaultResultContext<Object> resultContext, Object rowValue, ResultMapping parentMapping, ResultSet rs) throws SQLException {
if (parentMapping != null) {
linkToParents(rs, parentMapping, rowValue);
} else {
callResultHandler(resultHandler, resultContext, rowValue);
}
}

private void callResultHandler(ResultHandler resultHandler, DefaultResultContext resultContext, Object rowValue) {
@SuppressWarnings("unchecked" /* because ResultHandler<?> is always ResultHandler<Object>*/)
private void callResultHandler(ResultHandler<?> resultHandler, DefaultResultContext<Object> resultContext, Object rowValue) {
resultContext.nextResultObject(rowValue);
resultHandler.handleResult(resultContext);
((ResultHandler<Object>)resultHandler).handleResult(resultContext);
}

private boolean shouldProcessMoreRows(ResultContext context, RowBounds rowBounds) throws SQLException {
private boolean shouldProcessMoreRows(ResultContext<?> context, RowBounds rowBounds) throws SQLException {
return !context.isStopped() && context.getResultCount() < rowBounds.getLimit();
}

Expand Down Expand Up @@ -758,8 +759,8 @@ private String prependPrefix(String columnName, String prefix) {
// HANDLE NESTED RESULT MAPS
//

private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
final DefaultResultContext resultContext = new DefaultResultContext();
private void handleRowValuesForNestedResultMap(ResultSetWrapper rsw, ResultMap resultMap, ResultHandler<?> resultHandler, RowBounds rowBounds, ResultMapping parentMapping) throws SQLException {
final DefaultResultContext<Object> resultContext = new DefaultResultContext<Object>();
skipRows(rsw.getResultSet(), rowBounds);
Object rowValue = null;
while (shouldProcessMoreRows(resultContext, rowBounds) && rsw.getResultSet().next()) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/session/ResultContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
/**
* @author Clinton Begin
*/
public interface ResultContext {
public interface ResultContext<T> {

Object getResultObject();
T getResultObject();

int getResultCount();

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/session/ResultHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
/**
* @author Clinton Begin
*/
public interface ResultHandler {
public interface ResultHandler<T> {

void handleResult(ResultContext context);
void handleResult(ResultContext<? extends T> resultContext);

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public <K, V> Map<K, V> selectMap(String statement, Object parameter, String map

@Override
public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
final List<?> list = selectList(statement, parameter, rowBounds);
final List<? extends V> list = selectList(statement, parameter, rowBounds);
final DefaultMapResultHandler<K, V> mapResultHandler = new DefaultMapResultHandler<K, V>(mapKey,
configuration.getObjectFactory(), configuration.getObjectWrapperFactory());
final DefaultResultContext context = new DefaultResultContext();
for (Object o : list) {
final DefaultResultContext<V> context = new DefaultResultContext<V>();
for (V o : list) {
context.nextResultObject(o);
mapResultHandler.handleResult(context);
}
Expand Down