Skip to content

Commit

Permalink
Extend the Table interface API
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejlach committed Aug 14, 2015
1 parent 79b00b8 commit fd1969b
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
------------------------------------------------------------------------------
qJava 2.2.0 [2015.08.x]
qJava 2.2.0 [2015.08.14]
------------------------------------------------------------------------------

- API convenience extensions
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- img src="http://www.devnet.de/fileadmin/images/DEVnet_Logo2014.png" width="150px" height="150px"/-->

qJava 2.1
qJava 2.2
=========

The q/kdb+ interface is implemented as a set of Java classes and provides:
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/exxeleron/qjava/QKeyedTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ public int getColumnsCount() {
return keys.getColumnsCount() + values.getColumnsCount();
}

/*
* (non-Javadoc)
*
* @see com.exxeleron.qjava.Table#hasColumn(java.lang.String)
*/
public boolean hasColumn( final String column ) {
return keys.hasColumn(column) || values.hasColumn(column);
}

/*
* (non-Javadoc)
*
* @see com.exxeleron.qjava.Table#getColumnIndex(java.lang.String)
*/
public int getColumnIndex( final String column ) {
if ( keys.hasColumn(column) ) {
return keys.getColumnIndex(column);
} else {
return keys.getColumnsCount() + values.getColumnIndex(column);
}
}

/**
* <p>
* Returns an iterator over a key/value pairs stored in the keyed table.
Expand Down
35 changes: 23 additions & 12 deletions src/main/java/com/exxeleron/qjava/QTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,42 @@ public QTable(final String[] columns, final Object[] data) {
this.rowsCount = Array.getLength(data[0]);
}

/**
* Gets a column index for specified name or <code>null</code> if column doesn't exist in the table.
/*
* (non-Javadoc)
*
* @param column
* Name of the column
* @return 0 based column index, <code>null</code> if column with given name is not defined
*/
public Integer getColumnIndex( final String column ) {
return columnsMap.get(column);
}

/* (non-Javadoc)
* @see com.exxeleron.qjava.Table#size()
*/
public int getRowsCount() {
return rowsCount;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see com.exxeleron.qjava.Table#getColumnsCount()
*/
public int getColumnsCount() {
return columns.length;
}

/*
* (non-Javadoc)
*
* @see com.exxeleron.qjava.Table#getColumnIndex(java.lang.String)
*/
public int getColumnIndex( final String column ) {
return columnsMap.get(column);
}

/*
* (non-Javadoc)
*
* @see com.exxeleron.qjava.Table#hasColumn(java.lang.String)
*/
public boolean hasColumn( final String column ) {
return columnsMap.containsKey(column);
}

/**
* Gets an array of columns in current {@link QTable}.
*
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/com/exxeleron/qjava/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,31 @@ public interface Table {
*
* @return a number of rows
*/
public abstract int getRowsCount();
public int getRowsCount();

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

/**
* Checks whether table contains column with given name.
*
* @param column
* Name of the column
* @return <code>true</code> if table contains column with given name, <code>false</code> otherwise
*/
public boolean hasColumn( final String column );

/**
* 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
* @throws NullPointerException
* if column is not defined
*/
public int getColumnIndex( final String column );

}
45 changes: 40 additions & 5 deletions src/test/java/com/exxeleron/qjava/TestCollections.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package com.exxeleron.qjava;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.lang.reflect.Array;
import java.util.Iterator;
Expand All @@ -30,6 +33,10 @@ public class TestCollections {

private static QTable getTestTable() {
final String[] columns = new String[] { "f", "i", "s" };
return getTestTable(columns);
}

private static QTable getTestTable( final String[] columns ) {
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;
Expand All @@ -39,8 +46,19 @@ private static QTable getTestTable() {
public void testQTable() {
final QTable t = getTestTable();

assertEquals(null, t.getColumnIndex("unknown"));
assertEquals(Integer.valueOf(0), t.getColumnIndex("f"));
try {
t.getColumnIndex("unknown");
fail("NullPointerException was expected");
} catch ( NullPointerException e ) {
assertTrue(true);
} catch ( Exception e ) {
fail("NullPointerException was expected");
}

assertEquals(0, t.getColumnIndex("f"));

assertTrue(t.hasColumn("f"));
assertFalse(t.hasColumn("unknown"));

assertEquals(t, t);
assertEquals(t, getTestTable());
Expand Down Expand Up @@ -68,11 +86,28 @@ public void testQTable() {

@Test
public void testQKeyedTable() {
final QKeyedTable t = new QKeyedTable(getTestTable(), getTestTable());
final QKeyedTable t = new QKeyedTable(getTestTable(), getTestTable(new String[] { "ff", "ii", "s" }));

assertEquals(t, t);
assertEquals(t.getKeys(), getTestTable());
assertEquals(t.getValues(), getTestTable());
assertEquals(t.getValues(), getTestTable(new String[] { "ff", "ii", "s" }));

assertTrue(t.hasColumn("f"));
assertTrue(t.hasColumn("ff"));
assertFalse(t.hasColumn("unknown"));

assertEquals(0, t.getColumnIndex("f"));
assertEquals(3, t.getColumnIndex("ff"));
assertEquals(2, t.getColumnIndex("s"));

try {
t.getColumnIndex("unknown");
fail("NullPointerException was expected");
} catch ( NullPointerException e ) {
assertTrue(true);
} catch ( Exception e ) {
fail("NullPointerException was expected");
}

int i = 0;
final Iterator<KeyValuePair> it = t.iterator();
Expand Down

0 comments on commit fd1969b

Please sign in to comment.