Skip to content

Commit

Permalink
Update Num interface to implement Serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
gretard committed Jan 5, 2024
1 parent a361c9b commit 873bcfd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Changelog for `ta4j`, roughly following [keepachangelog.com](http://keepachangel
- **PreviousValueIndicator** returns `NaN` if the (n-th) previous value of an indicator does not exist, i.e. if the (n-th) previous is below the first available index.
- **EnterAndHoldReturnCriterion** fixes exception thrown when bar series was empty
- **BaseBarSeries** fixed `UnsupportedOperationException` when creating a bar series that is based on an unmodifiable collection
- **Num** implements Serializable

### Changed
- **BarSeriesManager** consider finishIndex when running backtest
Expand Down
2 changes: 2 additions & 0 deletions ta4j-core/src/main/java/org/ta4j/core/num/DecimalNum.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
*/
public final class DecimalNum implements Num {

private static final long serialVersionUID = 387498017007756808L;

private static final int DEFAULT_PRECISION = 32;
private static final Logger log = LoggerFactory.getLogger(DecimalNum.class);

Expand Down
2 changes: 2 additions & 0 deletions ta4j-core/src/main/java/org/ta4j/core/num/DoubleNum.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
*/
public class DoubleNum implements Num {

private static final long serialVersionUID = -1373009487614026992L;

public static final DoubleNum ZERO = DoubleNum.valueOf(0);
private static final DoubleNum ONE = DoubleNum.valueOf(1);
private static final DoubleNum HUNDRED = DoubleNum.valueOf(100);
Expand Down
2 changes: 2 additions & 0 deletions ta4j-core/src/main/java/org/ta4j/core/num/NaN.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
*/
public class NaN implements Num {

private static final long serialVersionUID = -7292533679302352359L;

/** A static Not-a-Number instance. */
public static final Num NaN = new NaN();

Expand Down
3 changes: 2 additions & 1 deletion ta4j-core/src/main/java/org/ta4j/core/num/Num.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.ta4j.core.num;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
Expand All @@ -37,7 +38,7 @@
* @see DoubleNum
* @see DecimalNum
*/
public interface Num extends Comparable<Num> {
public interface Num extends Comparable<Num>, Serializable {

/**
* @return the Num of 0
Expand Down
30 changes: 30 additions & 0 deletions ta4j-core/src/test/java/org/ta4j/core/num/NumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.ta4j.core.TestUtils.assertNumEquals;
import static org.ta4j.core.TestUtils.assertNumNotEquals;
import static org.ta4j.core.num.NaN.NaN;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
Expand Down Expand Up @@ -346,4 +351,29 @@ public void sqrtOddExponent() {
assertNumEquals("547722.55750516611345696978280080", sqrt);
}

@Test
public void testSerialization() throws Exception {
Num numVal = numFunction.apply(1.3);
serializeDeserialize(numVal);
}

private static void serializeDeserialize(Num o) throws IOException, ClassNotFoundException {
byte[] array;
try (var baos = new ByteArrayOutputStream()) {
try (var out = new ObjectOutputStream(baos)) {
out.writeObject(o);
array = baos.toByteArray();
}

}
try (var baos = new ByteArrayInputStream(array)) {
try (var out = new ObjectInputStream(baos)) {
var deserialized = (Num) out.readObject();
assertNotSame(o, deserialized);
assertEquals(deserialized.doubleValue(), o.doubleValue());
}

}
}

}

0 comments on commit 873bcfd

Please sign in to comment.