Skip to content

Commit

Permalink
API convenience extensions. Introduce common interface for QTable and…
Browse files Browse the repository at this point in the history
… QKeyedTable: Table
  • Loading branch information
maciejlach committed Aug 7, 2015
1 parent 5f3295d commit 9c93402
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 15 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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]
------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<inceptionYear>2014</inceptionYear>

<packaging>bundle</packaging>
<version>2.1.3-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>

<organization>
<name>exxeleron</name>
Expand All @@ -25,7 +25,7 @@
<name>Apache License Version 2.0</name>
<url>http://www.apache.org/licenses/</url>
<comments>
Copyright (c) 2011-2014 Exxeleron GmbH
Copyright (c) 2011-2015 Exxeleron GmbH
</comments>
</license>
</licenses>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/exxeleron/qjava/QDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
* <p>
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/com/exxeleron/qjava/QKeyedTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* Represents a q keyed table type.
*/
public class QKeyedTable implements Iterable<QKeyedTable.KeyValuePair> {
public class QKeyedTable implements Iterable<QKeyedTable.KeyValuePair>, Table {

private final QTable keys;
private final QTable values;
Expand Down Expand Up @@ -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();
}

/**
* <p>
* Returns an iterator over a key/value pairs stored in the keyed table.
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/com/exxeleron/qjava/QTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
/**
* Represents a q table type.
*/
public final class QTable implements Iterable<QTable.Row> {
public final class QTable implements Iterable<QTable.Row>, Table {

private final String[] columns;
private final Object[] data;
Expand Down Expand Up @@ -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 <code>null</code> if column doesn't exist in the table.
*
* @param column
* Name of the column
* @return 0 based column index
* @return 0 based column index, <code>null</code> 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;
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/exxeleron/qjava/Table.java
Original file line number Diff line number Diff line change
@@ -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();

This comment has been minimized.

Copy link
@adnang

adnang Aug 8, 2015

do these methods need to be public and abstract? They are both implicit in interfaces


/**
* @return a number of columns
*/
public abstract int getColumnsCount();

}
149 changes: 149 additions & 0 deletions src/test/java/com/exxeleron/qjava/TestCollections.java
Original file line number Diff line number Diff line change
@@ -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<Row> it = t.iterator();

while ( it.hasNext() ) {
final Row row = it.next();
final Iterator<Object> 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<KeyValuePair> it = t.iterator();

while ( it.hasNext() ) {
final KeyValuePair kv = it.next();

Iterator<Object> 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<com.exxeleron.qjava.QDictionary.KeyValuePair> 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);
}

}

0 comments on commit 9c93402

Please sign in to comment.