Skip to content

Commit

Permalink
fix #1: creation of q dictionary with values represented as table
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejlach committed May 26, 2014
1 parent 9aaa753 commit 21201e8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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]
------------------------------------------------------------------------------
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/com/exxeleron/qjava/QDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public final class QDictionary implements Iterable<QDictionary.KeyValuePair> {
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.
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
}
}

Expand All @@ -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();
}
}

/**
Expand All @@ -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);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/exxeleron/qjava/QReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/exxeleron/qjava/QTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
* <p>
* Returns an iterator over columns in a particular row in the table.
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/exxeleron/qjava/QExpressions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" }));
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/QExpressions.out
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 21201e8

Please sign in to comment.