From 6d5cd507b7ef3e0bad7cc36618b9a3fb158e0bc0 Mon Sep 17 00:00:00 2001 From: Peter Lawrey Date: Tue, 28 Apr 2015 14:23:36 +0100 Subject: [PATCH] Add a DoubleArray example using ChronicleMap --- .../net/openhft/lang/values/DoubleArray.java | 17 ++++++++++++++- .../openhft/lang/values/DoubleArrayTest.java | 21 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/test/java/net/openhft/lang/values/DoubleArray.java b/src/test/java/net/openhft/lang/values/DoubleArray.java index 999867e39..56cd94175 100755 --- a/src/test/java/net/openhft/lang/values/DoubleArray.java +++ b/src/test/java/net/openhft/lang/values/DoubleArray.java @@ -16,6 +16,10 @@ public class DoubleArray implements Byteable, Copyable { private Bytes bytes; private long offset; + // creates an empty DoubleArray + public DoubleArray() { + } + public DoubleArray(int capacity) { bytes = DirectStore.allocate(BASE + capacity * 8L).bytes(); bytes.writeInt(CAPACITY, capacity); @@ -45,7 +49,7 @@ public class DoubleArray implements Byteable, Copyable { } public int length() { - return bytes.readInt(LENGTH + offset); + return bytes == null ? 6 * 8 : bytes.readInt(LENGTH + offset); } public int capacity() { @@ -98,4 +102,15 @@ public class DoubleArray implements Byteable, Copyable { for (int i = 0; i < length; i++) doubleArray.setDataAt(i, getDataAt(i)); } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("[ "); + String sep = ""; + for (int i = 0, len = length(); i < len; i++) { + sb.append(sep).append(getDataAt(i)); + sep = ", "; + } + return sb.append(" ]").toString(); + } } diff --git a/src/test/java/net/openhft/lang/values/DoubleArrayTest.java b/src/test/java/net/openhft/lang/values/DoubleArrayTest.java index 4192520d9..2868f7b31 100755 --- a/src/test/java/net/openhft/lang/values/DoubleArrayTest.java +++ b/src/test/java/net/openhft/lang/values/DoubleArrayTest.java @@ -1,5 +1,7 @@ package net.openhft.lang.values; +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.map.ChronicleMapBuilder; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -45,4 +47,23 @@ public class DoubleArrayTest { // free the memory. da.bytes().release(); } + + @Test + public void addToAMap() { + DoubleArray a = new DoubleArray(10); + a.setData(new double[]{1, 2, 3, 4, 5}); + + DoubleArray b = new DoubleArray(10); + b.setData(new double[]{5, 6, 7, 8, 9}); + + + ChronicleMap proxyMap = ChronicleMapBuilder.of(Integer.class, DoubleArray.class) + .create(); + proxyMap.put(1, a); + proxyMap.put(2, b); + + System.out.println(proxyMap.get(1)); + System.out.println(proxyMap.get(2)); + proxyMap.close(); + } } \ No newline at end of file