mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
#144 added a test for handling a recycled array of AbstractBytesMarshallable
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package net.openhft.lang.values;
|
||||
|
||||
import net.openhft.chronicle.core.pool.ClassAliasPool;
|
||||
import net.openhft.chronicle.map.ChronicleMap;
|
||||
import net.openhft.chronicle.map.ChronicleMapBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static junit.framework.TestCase.assertSame;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ArrayTest {
|
||||
|
||||
static String TMP = System.getProperty("java.io.tmpdir");
|
||||
|
||||
@Test
|
||||
public void test0() throws IOException {
|
||||
ClassAliasPool.CLASS_ALIASES.addAlias(MovingAverageArray.class);
|
||||
|
||||
File file = new File(TMP + "/pf-PosistionsAndClose-" + System.nanoTime());
|
||||
|
||||
ChronicleMap<Long, MovingAverageArray> mapWrite = ChronicleMap
|
||||
.of(Long.class, MovingAverageArray.class)
|
||||
.entries(100)
|
||||
.averageValue(createSampleWithSize(6, 0))
|
||||
.createPersistedTo(file);
|
||||
mapWrite.put(1L, createSampleWithSize(6, 1));
|
||||
mapWrite.put(2L, createSampleWithSize(4, 2));
|
||||
mapWrite.close();
|
||||
|
||||
ChronicleMap<Long, MovingAverageArray> mapRead = ChronicleMapBuilder
|
||||
.of(Long.class, MovingAverageArray.class)
|
||||
.createPersistedTo(file);
|
||||
MovingAverageArray m = mapRead.get(1L);
|
||||
assertEquals("!MovingAverageArray {\n" +
|
||||
" values: [\n" +
|
||||
" { movingAverage: 0.1, high: 0.1, low: 0.1, stdDev: 0.1 },\n" +
|
||||
" { movingAverage: 1.1, high: 1.1, low: 1.1, stdDev: 1.1 },\n" +
|
||||
" { movingAverage: 2.1, high: 2.1, low: 2.1, stdDev: 2.1 },\n" +
|
||||
" { movingAverage: 3.1, high: 3.1, low: 3.1, stdDev: 3.1 },\n" +
|
||||
" { movingAverage: 4.1, high: 4.1, low: 4.1, stdDev: 4.1 },\n" +
|
||||
" { movingAverage: 5.1, high: 5.1, low: 5.1, stdDev: 5.1 }\n" +
|
||||
" ]\n" +
|
||||
"}\n", m.toString());
|
||||
MovingAverageArray m2 = mapRead.getUsing(2L, m);
|
||||
assertSame(m, m2); // object is recycled, so no objects are created.
|
||||
assertEquals("!MovingAverageArray {\n" +
|
||||
" values: [\n" +
|
||||
" { movingAverage: 0.2, high: 0.2, low: 0.2, stdDev: 0.2 },\n" +
|
||||
" { movingAverage: 1.2, high: 1.2, low: 1.2, stdDev: 1.2 },\n" +
|
||||
" { movingAverage: 2.2, high: 2.2, low: 2.2, stdDev: 2.2 },\n" +
|
||||
" { movingAverage: 3.2, high: 3.2, low: 3.2, stdDev: 3.2 }\n" +
|
||||
" ]\n" +
|
||||
"}\n", m.toString());
|
||||
}
|
||||
|
||||
private MovingAverageArray createSampleWithSize(int size, int seed) {
|
||||
MovingAverageArray sample = new MovingAverageArray();
|
||||
for (int i = 0; i < size; i++) {
|
||||
double value = i + seed / 10.0;
|
||||
sample.add(new MovingAverageCompact(value, value, value, value));
|
||||
}
|
||||
return sample;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* Copyright (C) 2012, 2016 higherfrequencytrading.com
|
||||
* Copyright (C) 2016 Roman Leventov
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.openhft.lang.values;
|
||||
|
||||
import net.openhft.chronicle.bytes.BytesIn;
|
||||
import net.openhft.chronicle.bytes.BytesOut;
|
||||
import net.openhft.chronicle.bytes.DynamicallySized;
|
||||
import net.openhft.chronicle.core.Maths;
|
||||
import net.openhft.chronicle.core.io.IORuntimeException;
|
||||
import net.openhft.chronicle.core.util.ObjectUtils;
|
||||
import net.openhft.chronicle.wire.AbstractBytesMarshallable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by peter.lawrey on 23/04/2015.
|
||||
*/
|
||||
public class MovingAverageArray extends AbstractBytesMarshallable implements DynamicallySized {
|
||||
private transient List<MovingAverageCompact> buffer = new ArrayList<>();
|
||||
private List<MovingAverageCompact> values = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void readMarshallable(BytesIn bytes) throws IORuntimeException {
|
||||
int len = Maths.toUInt31(bytes.readStopBit());
|
||||
values.clear();
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (buffer.size() <= values.size()) {
|
||||
buffer.add(ObjectUtils.newInstance(MovingAverageCompact.class));
|
||||
}
|
||||
MovingAverageCompact next = buffer.get(i);
|
||||
next.readMarshallable(bytes);
|
||||
values.add(next);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeMarshallable(BytesOut bytes) {
|
||||
bytes.writeStopBit(values.size());
|
||||
for (int i = 0; i < values.size(); i++)
|
||||
values.get(i).writeMarshallable(bytes);
|
||||
}
|
||||
|
||||
public void add(MovingAverageCompact movingAverageCompact) {
|
||||
values.add(movingAverageCompact);
|
||||
}
|
||||
|
||||
public MovingAverageCompact get(int i) {
|
||||
return values.get(i);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return values.size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package net.openhft.lang.values;
|
||||
|
||||
import net.openhft.chronicle.bytes.BytesIn;
|
||||
import net.openhft.chronicle.bytes.BytesOut;
|
||||
import net.openhft.chronicle.wire.AbstractBytesMarshallable;
|
||||
|
||||
public class MovingAverageCompact extends AbstractBytesMarshallable {
|
||||
|
||||
private double movingAverage;
|
||||
private double high;
|
||||
private double low;
|
||||
private double stdDev;
|
||||
|
||||
public MovingAverageCompact(double movingAverage, double high, double low, double stdDev) {
|
||||
this.movingAverage = movingAverage;
|
||||
this.high = high;
|
||||
this.low = low;
|
||||
this.stdDev = stdDev;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readMarshallable(BytesIn in) throws IllegalStateException {
|
||||
movingAverage = in.readDouble();
|
||||
high = in.readDouble();
|
||||
low = in.readDouble();
|
||||
stdDev = in.readDouble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeMarshallable(BytesOut out) {
|
||||
out.writeDouble(movingAverage);
|
||||
out.writeDouble(high);
|
||||
out.writeDouble(low);
|
||||
out.writeDouble(stdDev);
|
||||
}
|
||||
|
||||
|
||||
public double getMovingAverage() {
|
||||
return movingAverage;
|
||||
}
|
||||
|
||||
public void setMovingAverage(double movingAverage) {
|
||||
this.movingAverage = movingAverage;
|
||||
}
|
||||
|
||||
public double getHigh() {
|
||||
return high;
|
||||
}
|
||||
|
||||
public void setHigh(double high) {
|
||||
this.high = high;
|
||||
}
|
||||
|
||||
public double getLow() {
|
||||
return low;
|
||||
}
|
||||
|
||||
public void setLow(double low) {
|
||||
this.low = low;
|
||||
}
|
||||
|
||||
public double getStdDev() {
|
||||
return stdDev;
|
||||
}
|
||||
|
||||
public void setStdDev(double stdDev) {
|
||||
this.stdDev = stdDev;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user