Permalink
Browse files

[core] Refactor RowList.Column.defineCol as RowList.Column:

```java
import static acolyte.RowList.Column;

Column(String.class, "colName");
```
  • Loading branch information...
cchantep
cchantep committed Jan 7, 2014
1 parent 7a5f916 commit 2397517f22e7642851e10f490abd2d2f99a8301f
@@ -23,6 +23,8 @@
* Type-safe list of row.
*
* @author Cedric Chantepie
* @todo Add nullable property to Column
* @todo Check NPE in RowResultSet.getObject
*/
public abstract class RowList<R extends Row> {
@@ -168,6 +170,18 @@ private NilRowList() { }
} // end of getColumnLabels
} // end of class Nil
/**
* Creates column definition.
*
* @throws IllegalArgumentException if |columnClass| is null,
* or |name| is empty.
*/
public static <T> Column<T> Column(final Class<T> columnClass,
final String name) {
return new Column<T>(columnClass, name);
} // end of column
/**
* Column definition.
*/
@@ -199,18 +213,6 @@ private Column(final Class<T> columnClass, final String name) {
this.columnClass = columnClass;
this.name = name;
} // end of <init>
/**
* Creates column definition.
*
* @throws IllegalArgumentException if |columnClass| is null,
* or |name| is empty.
*/
public static <T> Column<T> defineCol(final Class<T> columnClass,
final String name) {
return new Column<T>(columnClass, name);
} // end of column
} // end of Column
/**
@@ -365,6 +367,7 @@ public Object getObject(final int columnIndex) throws SQLException {
final int idx = columnIndex - 1;
final List<Object> cells = this.rows.get(this.row-1).cells();
// stringList :+ null -> .get -> NPE
if (idx < 0 || idx >= cells.size()) {
throw new SQLException("Invalid column index: " + columnIndex);
@@ -1253,6 +1256,7 @@ public boolean isSigned(final int column) throws SQLException {
* {@inheritDoc}
*/
public int isNullable(final int column) throws SQLException {
// TODO: See anorm.RowSpec
return ResultSetMetaData.columnNullableUnknown;
} // end of isNullable
@@ -16,10 +16,10 @@
import acolyte.StatementHandler.Parameter;
import static acolyte.RowList.Column.defineCol;
import static acolyte.RowLists.booleanList;
import static acolyte.RowLists.rowList1;
import static acolyte.RowLists.rowList3;
import static acolyte.RowList.Column;
import static acolyte.Rows.row1;
import static acolyte.Rows.row3;
@@ -98,9 +98,9 @@ public QueryResult apply(String sql,
// Prepare list of 2 rows
// with 3 columns of types String, Float, Date
return rowList3(defineCol(String.class, "str"),
defineCol(Float.class, "f"),
defineCol(Date.class, "date")).
return rowList3(Column(String.class, "str"),
Column(Float.class, "f"),
Column(Date.class, "date")).
append(row3("text", 2.3f, new Date(3l))).
append(row3("label", 4.56f, new Date(4l))).
asResult();
@@ -2,30 +2,30 @@ package acolyte
import org.specs2.mutable.Specification
import acolyte.RowList.Column.defineCol
import acolyte.RowList.Column
object ColumnSpec extends Specification {
"Column" title
"Definition" should {
"refuse null class" in {
defineCol(null, "col") aka "define" must throwA[IllegalArgumentException](
Column(null, "col") aka "define" must throwA[IllegalArgumentException](
message = "No column class")
}
"refuse empty name" in {
(defineCol(classOf[String], null).
(Column(classOf[String], null).
aka("null name") must throwA[IllegalArgumentException](
message = "Invalid column name: null")).
and(defineCol(classOf[String], "").
and(Column(classOf[String], "").
aka("empty name") must throwA[IllegalArgumentException](
message = "Invalid column name: "))
}
"be successful" in {
defineCol(classOf[Int], "int") aka "define" must beLike {
Column(classOf[Int], "int") aka "define" must beLike {
case col (col.columnClass aka "class" mustEqual classOf[Int]).
and(col.name aka "name" mustEqual "int")
View
@@ -1,6 +1,6 @@
#! /bin/sh
LIMIT="f42acaaf95e9eab597c8e572373b93612e28bc03"
LIMIT="7a5f9161f72585506268fdd42a80456a99e26574"
echo "## $1\n"
View
@@ -186,17 +186,17 @@ list2 = list2.withLabel(2, "first label").withLabel(3, "third name");
Both column classes and names can be declared in bulk way, using [definition class](http://cchantep.github.io/acolyte/apidocs/acolyte/RowList.Column.html):
```java
import static acolyte.RowList.Column.defineCol;
import static acolyte.RowList.Column;
// ...
RowList1<String> list1 = RowLists.
rowList1(defineCol(String.class, "first label"));
rowList1(Column(String.class, "first label"));
RowList3<Integer, Float, Character> list2 = RowLists.
rowList3(defineCol(Integer.class, "1st"),
defineCol(Float.class, "2nd"),
defineCol(Character.class, "3rd"));
rowList3(Column(Integer.class, "1st"),
Column(Float.class, "2nd"),
Column(Character.class, "3rd"));
```
Once you have declared your row list, and before turning it as result set, you can either add rows to it, or leave it empty.
@@ -730,18 +730,18 @@ import acolyte.StatementHandler.Parameter;
import acolyte.QueryResult;
import acolyte.RowList5;
import static acolyte.RowList.Column.defineCol;
import static acolyte.RowList.Column;
import static acolyte.RowLists.rowList5;
import static acolyte.Rows.row5;
@org.junit.runner.RunWith(org.junit.runners.JUnit4.class)
public class ZooTest {
private RowList5<String,Integer,String,Boolean,String> zooSchema =
rowList5(defineCol(String.class, "type"),
defineCol(Integer.class, "location"),
defineCol(String.class, "name"),
defineCol(Boolean.class, "fly"),
defineCol(String.class, "color"));
rowList5(Column(String.class, "type"),
Column(Integer.class, "location"),
Column(String.class, "name"),
Column(Boolean.class, "fly"),
Column(String.class, "color"));
@org.junit.Test
public void dogAtLocation() {
@@ -124,7 +124,7 @@ object Implicits
* }}}
*/
implicit def PairAsColumn[T](c: (Class[T], String)): Column[T] =
Column.defineCol(c._1, c._2)
Column(c._1, c._2)
}
@@ -31,7 +31,7 @@ object Acolyte {
implicit def RowListAsScala[R <: Row](l: RowList[R]): ScalaRowList[RowList[R], R] = new ScalaRowList(l)
implicit def PairAsColumn[T](c: (Class[T], String)): Column[T] =
Column.defineCol(c._1, c._2)
Column(c._1, c._2)
}
@@ -14,8 +14,8 @@
* Java formatting
*/
Java("Java", "text/java",
"import acolyte.RowList.Column;\r\nimport acolyte.RowLists;",
"Column.defineCol(%s.class, \"%s\")" /* colDef */,
"import static acolyte.RowList.Column;\r\nimport acolyte.RowLists;",
"Column(%s.class, \"%s\")" /* colDef */,
".append(" /* rowStart */,
")" /* rowEnd */,
FormattingConstants.javaMap, /* type map */

0 comments on commit 2397517

Please sign in to comment.