DATACASS-395 - Refactor to immutable converters.

Mutable ResultSet to List/Array converters are now immutable.
This commit is contained in:
Mark Paluch
2017-05-04 14:38:47 +02:00
parent 438c6ae9e2
commit 8737126725
3 changed files with 17 additions and 38 deletions

View File

@@ -39,23 +39,33 @@ import com.datastax.driver.core.ResultSet;
* {@link IllegalArgumentException}.
*
* @author Matthew T. Adams
* @author Mark Paluch
* @param <T>
*/
public abstract class AbstractResultSetConverter<T> implements Converter<ResultSet, T> {
private final static ResultSetToListConverter converter = new ResultSetToListConverter();
/**
* Converts the given value to this converter's type or throws {@link IllegalArgumentException}.
*/
protected abstract T doConvertSingleValue(Object object);
/**
* @return the target type.
*/
protected abstract Class<?> getType();
protected ResultSetToListConverter converter = new ResultSetToListConverter();
/**
* @return surrogate value if the {@link ResultSet} is {@literal null}.
*/
protected T getNullResultSetValue() {
return null;
}
/**
* @return surrogate value if the {@link ResultSet} is {@link ResultSet#isExhausted() exhausted}.
*/
protected T getExhaustedResultSetValue() {
return null;
}
@@ -100,8 +110,8 @@ public abstract class AbstractResultSetConverter<T> implements Converter<ResultS
return null;
}
protected void doThrow(String string) {
void doThrow(String string) {
throw new IllegalArgumentException(
String.format("can't convert %s to desired type [%s]", string, getType().getName()));
String.format("Cannot convert %s to desired type [%s]", string, getType().getName()));
}
}

View File

@@ -31,7 +31,7 @@ import com.datastax.driver.core.Row;
*/
public class ResultSetToArrayConverter implements Converter<ResultSet, Object[]> {
protected Converter<Row, Object[]> rowConverter;
private final Converter<Row, Object[]> rowConverter;
/**
* Create a new {@link ResultSetToArrayConverter} given a row {@link Converter}.
@@ -39,24 +39,9 @@ public class ResultSetToArrayConverter implements Converter<ResultSet, Object[]>
* @param rowConverter must not be {@literal null}.
*/
public ResultSetToArrayConverter(Converter<Row, Object[]> rowConverter) {
setRowConverter(rowConverter);
}
/**
* @return the row {@link Converter}.
*/
public Converter<Row, Object[]> getRowConverter() {
return rowConverter;
}
/**
* Set the row {@link Converter}.
*
* @param rowConverter must not be {@literal null}.
*/
public void setRowConverter(Converter<Row, Object[]> rowConverter) {
Assert.notNull(rowConverter, "Converter must not be null");
this.rowConverter = rowConverter;
}

View File

@@ -32,7 +32,7 @@ import com.datastax.driver.core.Row;
*/
public class ResultSetToListConverter implements Converter<ResultSet, List<Map<String, Object>>> {
private Converter<Row, Map<String, Object>> rowConverter;
private final Converter<Row, Map<String, Object>> rowConverter;
/**
* Create a new {@link ResultSetToListConverter} using a default {@link RowToMapConverter}.
@@ -47,22 +47,6 @@ public class ResultSetToListConverter implements Converter<ResultSet, List<Map<S
* @param rowConverter must not be {@literal null}.
*/
public ResultSetToListConverter(Converter<Row, Map<String, Object>> rowConverter) {
setRowConverter(rowConverter);
}
/**
* @return the row to map {@link Converter}.
*/
public Converter<Row, Map<String, Object>> getRowConverter() {
return rowConverter;
}
/**
* Set the the row to map {@link Converter}.
*
* @param rowConverter must not be {@literal null}.
*/
public void setRowConverter(Converter<Row, Map<String, Object>> rowConverter) {
Assert.notNull(rowConverter, "Converter must not be null");