[#1988] Add Record.fromArray() as the inverse operation of

Record.intoArray()
This commit is contained in:
Lukas Eder 2012-12-02 12:53:22 +01:00
parent 2aa2e69ea2
commit b05ae01be9
3 changed files with 29 additions and 0 deletions

View File

@ -327,6 +327,13 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T725,
Object[][] booksArray = create().selectFrom(TBook()).orderBy(TBook_ID()).fetchArrays();
for (int j = 0; j < books.size(); j++) {
B bookJ = books.get(j);
Object[] array = bookJ.intoArray();
B book2 = create().newRecord(TBook());
book2.fromArray(array);
assertEquals(bookJ, book2);
for (int i = 0; i < TBook().getFields().size(); i++) {
assertEquals(books.getValue(j, i), booksArray[j][i]);
assertEquals(books.getValue(j, i), books.intoArray()[j][i]);

View File

@ -460,6 +460,7 @@ public interface Record extends FieldProvider, Attachable {
* This is the same as calling <code>into(Object[].class)</code>
*
* @return This record as an array
* @see #fromArray(Object...)
*/
Object[] intoArray();
@ -722,4 +723,15 @@ public interface Record extends FieldProvider, Attachable {
*/
void fromMap(Map<String, ?> map);
/**
* Load data from an array into this record
* <p>
* The argument array is expected to hold values for this record's field
* indexes. Missing values will be left untouched. Excess values will be
* ignored.
* <p>
* This is the inverse operation to {@link #intoArray()}
*/
void fromArray(Object... array);
}

View File

@ -780,6 +780,16 @@ abstract class AbstractRecord extends AbstractStore implements Record {
}
}
@Override
public final void fromArray(Object... array) {
List<Field<?>> f = getFields();
int size = f.size();
for (int i = 0; i < size && i < array.length; i++) {
Utils.setValue(this, f.get(i), array[i]);
}
}
/**
* This method was implemented with [#799]. It may be useful to make it
* public for broader use...?