From 21201e82a130cc06b67ee1f19aa5bc2e5dd25dcc Mon Sep 17 00:00:00 2001 From: maciejlach Date: Mon, 26 May 2014 11:31:36 +0200 Subject: [PATCH] fix #1: creation of q dictionary with values represented as table --- CHANGELOG.txt | 6 ++++ .../java/com/exxeleron/qjava/QDictionary.java | 29 ++++++++++++++----- .../java/com/exxeleron/qjava/QReader.java | 2 +- src/main/java/com/exxeleron/qjava/QTable.java | 5 ++++ .../com/exxeleron/qjava/QExpressions.java | 2 ++ src/test/resources/QExpressions.out | 2 ++ 6 files changed, 38 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index fe25f0b..b149f66 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,9 @@ +------------------------------------------------------------------------------ + qJava 2.0.1 [2014.05.26] +------------------------------------------------------------------------------ + + - Fix: creation of q dictionary with values represented as table + ------------------------------------------------------------------------------ qJava 2.0 [2014.04.02] ------------------------------------------------------------------------------ diff --git a/src/main/java/com/exxeleron/qjava/QDictionary.java b/src/main/java/com/exxeleron/qjava/QDictionary.java index 4b12270..7afd061 100644 --- a/src/main/java/com/exxeleron/qjava/QDictionary.java +++ b/src/main/java/com/exxeleron/qjava/QDictionary.java @@ -26,6 +26,7 @@ public final class QDictionary implements Iterable { private final Object keys; private final Object values; private final int length; + private final boolean areValuesArray; /** * Creates new {@link QDictionary} instance with given keys and values arrays. @@ -41,16 +42,18 @@ public QDictionary(final Object keys, final Object values) { if ( keys == null || !keys.getClass().isArray() ) { throw new IllegalArgumentException("Parameter: keys is not an array"); } - if ( values == null || !values.getClass().isArray() ) { - throw new IllegalArgumentException("Parameter: values is not an array"); + if ( values == null || !(values.getClass().isArray() || values instanceof QTable) ) { + throw new IllegalArgumentException("Parameter: values is not an array nor table"); } + length = Array.getLength(keys); - if ( length != Array.getLength(values) ) { - throw new IllegalArgumentException("Keys and value arrays cannot have different length"); + if ( (values.getClass().isArray() && length != Array.getLength(values)) || (values instanceof QTable && length != ((QTable) values).getRowsCount()) ) { + throw new IllegalArgumentException("Keys and values cannot have different length"); } this.keys = keys; this.values = values; + this.areValuesArray = values.getClass().isArray(); } /** @@ -128,7 +131,11 @@ public Object getKey() { * @return value */ public Object getValue() { - return Array.get(values, index); + if ( areValuesArray ) { + return Array.get(values, index); + } else { + return ((QTable) values).get(index); + } } } @@ -140,7 +147,11 @@ public Object getValue() { */ @Override public String toString() { - return "QDictionary: " + Utils.arrayToString(keys) + "!" + Utils.arrayToString(values); + if ( areValuesArray ) { + return "QDictionary: " + Utils.arrayToString(keys) + "!" + Utils.arrayToString(values); + } else { + return "QDictionary: " + Utils.arrayToString(keys) + "!" + values.toString(); + } } /** @@ -161,7 +172,11 @@ public boolean equals( final Object obj ) { } final QDictionary d = (QDictionary) obj; - return Utils.deepArraysEquals(keys, d.keys) && Utils.deepArraysEquals(values, d.values); + if ( areValuesArray ) { + return Utils.deepArraysEquals(keys, d.keys) && Utils.deepArraysEquals(values, d.values); + } else { + return Utils.deepArraysEquals(keys, d.keys) && ((QTable) values).equals(d.values); + } } /** diff --git a/src/main/java/com/exxeleron/qjava/QReader.java b/src/main/java/com/exxeleron/qjava/QReader.java index e8c2d4e..a53b00c 100644 --- a/src/main/java/com/exxeleron/qjava/QReader.java +++ b/src/main/java/com/exxeleron/qjava/QReader.java @@ -413,7 +413,7 @@ private Object readDictionary() throws QException, IOException { final Object keys = readObject(); final Object values = readObject(); - if ( keys != null && keys.getClass().isArray() && values != null && values.getClass().isArray() ) { + if ( keys != null && keys.getClass().isArray() && (values != null && values.getClass().isArray() || values instanceof QTable) ) { return new QDictionary(keys, values); } else if ( keys instanceof QTable && values instanceof QTable ) { return new QKeyedTable((QTable) keys, (QTable) values); diff --git a/src/main/java/com/exxeleron/qjava/QTable.java b/src/main/java/com/exxeleron/qjava/QTable.java index 2bcc5d1..d07f7eb 100644 --- a/src/main/java/com/exxeleron/qjava/QTable.java +++ b/src/main/java/com/exxeleron/qjava/QTable.java @@ -271,6 +271,11 @@ public void set( final int index, final Object value ) { Array.set(data[index], rowIndex, value); } + @Override + public String toString() { + return Utils.arrayToString(columns) + "!" + Utils.arrayToString(toArray()); + } + /** *

* Returns an iterator over columns in a particular row in the table. diff --git a/src/test/java/com/exxeleron/qjava/QExpressions.java b/src/test/java/com/exxeleron/qjava/QExpressions.java index d906564..75e1f80 100644 --- a/src/test/java/com/exxeleron/qjava/QExpressions.java +++ b/src/test/java/com/exxeleron/qjava/QExpressions.java @@ -105,6 +105,8 @@ private void initExpressions() throws QException { reference.put("12:04:59.123 0Nt", new QTime[] { new QTime(43499123), new QTime(Integer.MIN_VALUE) }); reference.put("(enlist `a)!(enlist 1)", new QDictionary(new String[] { "a" }, new int[] { 1 })); reference.put("1 2!`abc`cdefgh", new QDictionary(new int[] { 1, 2 }, new String[] { "abc", "cdefgh" })); + reference.put("`abc`def`gh!([] one: 1 2 3; two: 4 5 6)", new QDictionary(new String[] { "abc", "def", "gh" }, new QTable(new String[] { "one", "two" }, + new Object[] { new long[] { 1, 2, 3 }, new long[] { 4, 5, 6 } }))); reference.put("(1;2h;3.3;\"4\")!(`one;2 3;\"456\";(7;8 9))", new QDictionary(new Object[] { 1, (short) 2, 3.3, '4' }, new Object[] { "one", new int[] { 2, 3 }, "456".toCharArray(), new Object[] { 7, new int[] { 8, 9 } } })); reference.put("(0 1; 2 3)!`first`second", new QDictionary(new Object[] { new int[] { 0, 1 }, new int[] { 2, 3 } }, new String[] { "first", "second" })); diff --git a/src/test/resources/QExpressions.out b/src/test/resources/QExpressions.out index 831e16c..20a5378 100644 --- a/src/test/resources/QExpressions.out +++ b/src/test/resources/QExpressions.out @@ -132,6 +132,8 @@ ED00000080 630B0001000000610006000100000001000000 1 2!`abc`cdefgh 6306000200000001000000020000000B00020000006162630063646566676800 +`abc`def`gh!([] one: 1 2 3; two: 4 5 6) +630B000300000061626300646566006768006200630B00020000006F6E650074776F00000002000000070003000000010000000000000002000000000000000300000000000000070003000000040000000000000005000000000000000600000000000000 (1;2h;3.3;"4")!(`one;2 3;"456";(7;8 9)) 63000004000000FA01000000FB0200F76666666666660A40F634000004000000F56F6E650006000200000002000000030000000A0003000000343536000002000000FA070000000600020000000800000009000000 (0 1; 2 3)!`first`second