diff --git a/CHANGELOG.md b/CHANGELOG.md index 95a9aed1c..242835f5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/ta4j-core/src/main/java/org/ta4j/core/num/DecimalNum.java b/ta4j-core/src/main/java/org/ta4j/core/num/DecimalNum.java index 7e2145899..d730789c1 100644 --- a/ta4j-core/src/main/java/org/ta4j/core/num/DecimalNum.java +++ b/ta4j-core/src/main/java/org/ta4j/core/num/DecimalNum.java @@ -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); diff --git a/ta4j-core/src/main/java/org/ta4j/core/num/DoubleNum.java b/ta4j-core/src/main/java/org/ta4j/core/num/DoubleNum.java index 1a329cfcb..34224b09e 100644 --- a/ta4j-core/src/main/java/org/ta4j/core/num/DoubleNum.java +++ b/ta4j-core/src/main/java/org/ta4j/core/num/DoubleNum.java @@ -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); diff --git a/ta4j-core/src/main/java/org/ta4j/core/num/NaN.java b/ta4j-core/src/main/java/org/ta4j/core/num/NaN.java index a14bb4797..b665da993 100644 --- a/ta4j-core/src/main/java/org/ta4j/core/num/NaN.java +++ b/ta4j-core/src/main/java/org/ta4j/core/num/NaN.java @@ -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(); diff --git a/ta4j-core/src/main/java/org/ta4j/core/num/Num.java b/ta4j-core/src/main/java/org/ta4j/core/num/Num.java index 308b15a17..b8ef30d68 100644 --- a/ta4j-core/src/main/java/org/ta4j/core/num/Num.java +++ b/ta4j-core/src/main/java/org/ta4j/core/num/Num.java @@ -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; @@ -37,7 +38,7 @@ * @see DoubleNum * @see DecimalNum */ -public interface Num extends Comparable { +public interface Num extends Comparable, Serializable { /** * @return the Num of 0 diff --git a/ta4j-core/src/test/java/org/ta4j/core/num/NumTest.java b/ta4j-core/src/test/java/org/ta4j/core/num/NumTest.java index 69caa8580..8c0e87aa0 100644 --- a/ta4j-core/src/test/java/org/ta4j/core/num/NumTest.java +++ b/ta4j-core/src/test/java/org/ta4j/core/num/NumTest.java @@ -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; @@ -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()); + } + + } + } + }