diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 5d486d1..adb468c 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,12 @@ +------------------------------------------------------------------------------ + qJava 2.2.0 [2015.08.x] +------------------------------------------------------------------------------ + + - API convenience extensions + - Improved memory reuse while iterating over QTable, QDictionary + and QKeyedTable + - Introduce common interface for QTable and QKeyedTable: Table + ------------------------------------------------------------------------------ qJava 2.1.2 [2015.03.23] ------------------------------------------------------------------------------ diff --git a/pom.xml b/pom.xml index 75c5d12..e0f7711 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ 2014 bundle - 2.1.3-SNAPSHOT + 2.2.0-SNAPSHOT exxeleron @@ -25,7 +25,7 @@ Apache License Version 2.0 http://www.apache.org/licenses/ - Copyright (c) 2011-2014 Exxeleron GmbH + Copyright (c) 2011-2015 Exxeleron GmbH diff --git a/src/main/java/com/exxeleron/qjava/QDictionary.java b/src/main/java/com/exxeleron/qjava/QDictionary.java index 31c8628..97353be 100644 --- a/src/main/java/com/exxeleron/qjava/QDictionary.java +++ b/src/main/java/com/exxeleron/qjava/QDictionary.java @@ -73,6 +73,15 @@ public Object getKeys() { public Object getValues() { return values; } + + /** + * Gets a number of entries in {@link QDictionary}. + * + * @return a number of entries + */ + public int size() { + return length; + } /** *

diff --git a/src/main/java/com/exxeleron/qjava/QKeyedTable.java b/src/main/java/com/exxeleron/qjava/QKeyedTable.java index ab3ec39..b09ae2f 100644 --- a/src/main/java/com/exxeleron/qjava/QKeyedTable.java +++ b/src/main/java/com/exxeleron/qjava/QKeyedTable.java @@ -20,7 +20,7 @@ /** * Represents a q keyed table type. */ -public class QKeyedTable implements Iterable { +public class QKeyedTable implements Iterable, Table { private final QTable keys; private final QTable values; @@ -64,6 +64,24 @@ public QTable getValues() { return values; } + /* + * (non-Javadoc) + * + * @see com.exxeleron.qjava.Table#size() + */ + public int getRowsCount() { + return keys.getRowsCount(); + } + + /* + * (non-Javadoc) + * + * @see com.exxeleron.qjava.Table#getColumnsCount() + */ + public int getColumnsCount() { + return keys.getColumnsCount() + values.getColumnsCount(); + } + /** *

* Returns an iterator over a key/value pairs stored in the keyed table. diff --git a/src/main/java/com/exxeleron/qjava/QTable.java b/src/main/java/com/exxeleron/qjava/QTable.java index d07f7eb..a4595a1 100644 --- a/src/main/java/com/exxeleron/qjava/QTable.java +++ b/src/main/java/com/exxeleron/qjava/QTable.java @@ -24,7 +24,7 @@ /** * Represents a q table type. */ -public final class QTable implements Iterable { +public final class QTable implements Iterable, Table { private final String[] columns; private final Object[] data; @@ -68,29 +68,25 @@ public QTable(final String[] columns, final Object[] data) { } /** - * Gets a column index for specified name. + * Gets a column index for specified name or null if column doesn't exist in the table. * * @param column * Name of the column - * @return 0 based column index + * @return 0 based column index, null if column with given name is not defined */ - public int getColumnIndex( final String column ) { + public Integer getColumnIndex( final String column ) { return columnsMap.get(column); } - /** - * Gets a number of rows in current {@link QTable}. - * - * @return a number of rows + /* (non-Javadoc) + * @see com.exxeleron.qjava.Table#size() */ public int getRowsCount() { return rowsCount; } - /** - * Gets a number of columns in current {@link QTable}. - * - * @return a number of columns + /* (non-Javadoc) + * @see com.exxeleron.qjava.Table#getColumnsCount() */ public int getColumnsCount() { return columns.length; diff --git a/src/main/java/com/exxeleron/qjava/Table.java b/src/main/java/com/exxeleron/qjava/Table.java new file mode 100644 index 0000000..853fa00 --- /dev/null +++ b/src/main/java/com/exxeleron/qjava/Table.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2011-2015 Exxeleron GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.exxeleron.qjava; + +/** + * Common interface for tables. + */ +public interface Table { + + /** + * Gets a number of rows in current {@link QKeyedTable}. + * + * @return a number of rows + */ + public abstract int getRowsCount(); + + /** + * @return a number of columns + */ + public abstract int getColumnsCount(); + +} \ No newline at end of file diff --git a/src/test/java/com/exxeleron/qjava/TestCollections.java b/src/test/java/com/exxeleron/qjava/TestCollections.java new file mode 100644 index 0000000..8821af6 --- /dev/null +++ b/src/test/java/com/exxeleron/qjava/TestCollections.java @@ -0,0 +1,149 @@ +/** + * Copyright (c) 2011-2015 Exxeleron GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.exxeleron.qjava; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; + +import java.lang.reflect.Array; +import java.util.Iterator; + +import org.junit.Test; + +import com.exxeleron.qjava.QKeyedTable.KeyValuePair; +import com.exxeleron.qjava.QTable.Row; + +public class TestCollections { + + private static QTable getTestTable() { + final String[] columns = new String[] { "f", "i", "s" }; + final Object[] data = new Object[] { new double[] { -1.1, 0.0, 10.32 }, new int[] { 10, 0, -2 }, new String[] { "foo", "bar", "" } }; + final QTable table = new QTable(columns, data); + return table; + } + + @Test + public void testQTable() { + final QTable t = getTestTable(); + + assertEquals(null, t.getColumnIndex("unknown")); + assertEquals(Integer.valueOf(0), t.getColumnIndex("f")); + + assertEquals(t, t); + assertEquals(t, getTestTable()); + + int i = 0; + final Iterator it = t.iterator(); + + while ( it.hasNext() ) { + final Row row = it.next(); + final Iterator cit = row.iterator(); + int j = 0; + + while ( cit.hasNext() ) { + final Object v = cit.next(); + assertEquals(t.get(i).get(j), v); + j++; + } + + assertEquals(t.getColumnsCount(), j); + i++; + } + + assertEquals(t.getRowsCount(), i); + } + + @Test + public void testQKeyedTable() { + final QKeyedTable t = new QKeyedTable(getTestTable(), getTestTable()); + + assertEquals(t, t); + assertEquals(t.getKeys(), getTestTable()); + assertEquals(t.getValues(), getTestTable()); + + int i = 0; + final Iterator it = t.iterator(); + + while ( it.hasNext() ) { + final KeyValuePair kv = it.next(); + + Iterator cit = kv.getKey().iterator(); + int j = 0; + + while ( cit.hasNext() ) { + final Object v = cit.next(); + assertEquals(t.getKeys().get(i).get(j), v); + j++; + } + + cit = kv.getValue().iterator(); + j = 0; + + while ( cit.hasNext() ) { + final Object v = cit.next(); + assertEquals(t.getValues().get(i).get(j), v); + j++; + } + + i++; + } + + assertEquals(t.getRowsCount(), i); + } + + @Test + public void testQDictionary() { + final String[] keys = new String[] { "foo", "bar", "z" }; + final Object[] values = new Object[] { 1, "val", null }; + + QDictionary d = new QDictionary(keys, values); + assertEquals(d, d); + assertEquals(d, new QDictionary(keys, values)); + + int i = 0; + Iterator it = d.iterator(); + + while ( it.hasNext() ) { + com.exxeleron.qjava.QDictionary.KeyValuePair kv = it.next(); + + assertEquals(Array.get(d.getKeys(), i), kv.getKey()); + assertEquals(Array.get(d.getValues(), i), kv.getValue()); + + i++; + } + + assertEquals(d.size(), i); + + d = new QDictionary(keys, getTestTable()); + assertEquals(d, d); + assertEquals(d, new QDictionary(keys, getTestTable())); + + i = 0; + it = d.iterator(); + + while ( it.hasNext() ) { + com.exxeleron.qjava.QDictionary.KeyValuePair kv = it.next(); + + assertEquals(Array.get(d.getKeys(), i), kv.getKey()); + assertArrayEquals(((QTable) d.getValues()).get(i).toArray(), ((Row) kv.getValue()).toArray()); + + i++; + } + + assertEquals(d.size(), i); + } + +}