From c63ce0c10eee4ee67bbef7cf6bcc42a7f0b83f2f Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Mon, 6 May 2013 18:19:31 +0000 Subject: [PATCH] Out with arrays, in with collections. --- .../depot/impl/DepotMarshaller.java | 32 ++++++++----------- .../samskivert/depot/impl/DepotMetaData.java | 14 ++++---- .../depot/impl/TableValueGenerator.java | 12 +++---- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java b/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java index eff1bf6..930afb8 100644 --- a/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java +++ b/src/main/java/com/samskivert/depot/impl/DepotMarshaller.java @@ -494,21 +494,18 @@ public class DepotMarshaller implements QueryMarshal // figure out the list of fields that correspond to actual table columns and generate the // SQL used to create and migrate our table (unless we're a computed entity) - _columnFields = new ColumnExp[_allFields.length]; - ColumnDefinition[] declarations = new ColumnDefinition[_allFields.length]; - int jj = 0; + List> columnFields = Lists.newArrayList(); + List declarations = Lists.newArrayList(); for (ColumnExp field : _allFields) { FieldMarshaller fm = _fields.get(field.name); // include all persistent non-computed fields ColumnDefinition colDef = fm.getColumnDefinition(); if (colDef != null) { - _columnFields[jj] = field; - declarations[jj] = colDef; - jj ++; + columnFields.add(field); + declarations.add(colDef); } } - _columnFields = ArrayUtil.splice(_columnFields, jj); - declarations = ArrayUtil.splice(declarations, jj); + _columnFields = columnFields.toArray(new ColumnExp[columnFields.size()]); // determine whether or not this record has ever been seen int currentVersion = _meta.getVersion(getTableName(), false); @@ -681,7 +678,7 @@ public class DepotMarshaller implements QueryMarshal } protected void createTable (PersistenceContext ctx, final SQLBuilder builder, - final ColumnDefinition[] declarations) + final List declarations) throws DatabaseException { log.info("Creating initial table '" + getTableName() + "'."); @@ -690,15 +687,14 @@ public class DepotMarshaller implements QueryMarshal @Override protected int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException { // create the table - String[] primaryKeyColumns = null; + List pkColumns = Lists.newArrayList(); if (hasPrimaryKey()) { - primaryKeyColumns = new String[_pkColumns.size()]; - for (int ii = 0; ii < primaryKeyColumns.length; ii ++) { - primaryKeyColumns[ii] = _pkColumns.get(ii).getColumnName(); + for (int ii = 0; ii < _pkColumns.size(); ii ++) { + pkColumns.add(_pkColumns.get(ii).getColumnName()); } } liaison.createTableIfMissing(conn, getTableName(), fieldsToColumns(_columnFields), - declarations, null, primaryKeyColumns); + declarations, pkColumns); // add its indexen for (CreateIndexClause iclause : _indexes) { @@ -964,14 +960,14 @@ public class DepotMarshaller implements QueryMarshal } // translate an array of field names to an array of column names - protected String[] fieldsToColumns (ColumnExp[] fields) + protected List fieldsToColumns (ColumnExp[] fields) { - String[] columns = new String[fields.length]; - for (int ii = 0; ii < columns.length; ii ++) { + List columns = Lists.newArrayListWithExpectedSize(fields.length); + for (int ii = 0; ii < fields.length; ii ++) { FieldMarshaller fm = _fields.get(fields[ii].name); checkArgument(fm != null, "Unknown field on record [field=%s, class=%s]", fields[ii], _pClass); - columns[ii] = fm.getColumnName(); + columns.add(fm.getColumnName()); } return columns; } diff --git a/src/main/java/com/samskivert/depot/impl/DepotMetaData.java b/src/main/java/com/samskivert/depot/impl/DepotMetaData.java index b6e3574..83e7347 100644 --- a/src/main/java/com/samskivert/depot/impl/DepotMetaData.java +++ b/src/main/java/com/samskivert/depot/impl/DepotMetaData.java @@ -8,6 +8,7 @@ import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Arrays; import java.util.Map; import com.google.common.collect.Maps; @@ -37,14 +38,11 @@ public class DepotMetaData // create our schema version table if needed liaison.createTableIfMissing( stmt.getConnection(), SCHEMA_VERSION_TABLE, - new String[] { P_COLUMN, V_COLUMN, MV_COLUMN }, - new ColumnDefinition[] { - new ColumnDefinition("VARCHAR(255)", false, true, null), - new ColumnDefinition("INTEGER", false, false, null), - new ColumnDefinition("INTEGER", false, false, null) - }, - null, - new String[] { P_COLUMN }); + Arrays.asList(P_COLUMN, V_COLUMN, MV_COLUMN), + Arrays.asList(new ColumnDefinition("VARCHAR(255)", false, true, null), + new ColumnDefinition("INTEGER", false, false, null), + new ColumnDefinition("INTEGER", false, false, null)), + Arrays.asList(P_COLUMN)); // slurp in the current versions of all records readVersions(liaison, stmt); diff --git a/src/main/java/com/samskivert/depot/impl/TableValueGenerator.java b/src/main/java/com/samskivert/depot/impl/TableValueGenerator.java index 007d26c..ff288e8 100644 --- a/src/main/java/com/samskivert/depot/impl/TableValueGenerator.java +++ b/src/main/java/com/samskivert/depot/impl/TableValueGenerator.java @@ -8,6 +8,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.Arrays; import com.samskivert.depot.annotation.GeneratedValue; import com.samskivert.depot.annotation.TableGenerator; @@ -43,13 +44,10 @@ public class TableValueGenerator extends ValueGenerator // make sure our table exists liaison.createTableIfMissing( conn, _valueTable, - new String[] { _pkColumnName, _valueColumnName }, - new ColumnDefinition[] { - new ColumnDefinition("VARCHAR(255)", true, false, null), - new ColumnDefinition("INTEGER") - }, - null, - new String[] { _pkColumnName }); + Arrays.asList(_pkColumnName, _valueColumnName), + Arrays.asList(new ColumnDefinition("VARCHAR(255)", true, false, null), + new ColumnDefinition("INTEGER")), + Arrays.asList(_pkColumnName)); // and also that there's a row in it for us PreparedStatement stmt = conn.prepareStatement(