From f6d5a0010aad708ea13b9f6131a272f84b515574 Mon Sep 17 00:00:00 2001 From: maciejlach Date: Mon, 29 Sep 2014 10:28:51 +0200 Subject: [PATCH] Implement Serializable in temporal classes. Resolves #4 --- CHANGELOG.txt | 6 + pom.xml | 2 +- src/main/java/com/exxeleron/qjava/QDate.java | 7 +- .../java/com/exxeleron/qjava/QDateTime.java | 8 +- .../java/com/exxeleron/qjava/QMinute.java | 8 +- src/main/java/com/exxeleron/qjava/QMonth.java | 8 +- .../java/com/exxeleron/qjava/QSecond.java | 8 +- src/main/java/com/exxeleron/qjava/QTime.java | 8 +- .../java/com/exxeleron/qjava/QTimespan.java | 9 +- .../java/com/exxeleron/qjava/QTimestamp.java | 9 +- .../com/exxeleron/qjava/TestDateTime.java | 126 +++++++++++++++++- 11 files changed, 172 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index a1eb060..eea8b85 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,9 @@ +------------------------------------------------------------------------------ + qJava 2.1.0 [TBA] +------------------------------------------------------------------------------ + + - Temporal classes are now Serializable + ------------------------------------------------------------------------------ qJava 2.0.5 [2014.09.11] ------------------------------------------------------------------------------ diff --git a/pom.xml b/pom.xml index e4d3f4c..7aae5f3 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ 2014 bundle - 2.0.6-SNAPSHOT + 2.1.0-SNAPSHOT exxeleron diff --git a/src/main/java/com/exxeleron/qjava/QDate.java b/src/main/java/com/exxeleron/qjava/QDate.java index a93467a..14fdcfe 100644 --- a/src/main/java/com/exxeleron/qjava/QDate.java +++ b/src/main/java/com/exxeleron/qjava/QDate.java @@ -15,6 +15,7 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -22,12 +23,14 @@ /** * Represents q date type. */ -public final class QDate implements DateTime { +public final class QDate implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; + private static final String NULL_STR = "0Nd"; private static final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd"); - private Date datetime; + private transient Date datetime; private final Integer value; /** diff --git a/src/main/java/com/exxeleron/qjava/QDateTime.java b/src/main/java/com/exxeleron/qjava/QDateTime.java index 3a1a413..d3cd562 100644 --- a/src/main/java/com/exxeleron/qjava/QDateTime.java +++ b/src/main/java/com/exxeleron/qjava/QDateTime.java @@ -15,6 +15,7 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -22,14 +23,15 @@ /** * Represents q datetime type. */ -public final class QDateTime implements DateTime { +public final class QDateTime implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; private static final String NULL_STR = "0Nz"; private static final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd'T'HH:mm:ss.SSS"); - private Date datetime; - private Double value; + private transient Date datetime; + private final Double value; /** * Creates new {@link QDateTime} instance using specified q date value. diff --git a/src/main/java/com/exxeleron/qjava/QMinute.java b/src/main/java/com/exxeleron/qjava/QMinute.java index 594ff0f..8f83863 100644 --- a/src/main/java/com/exxeleron/qjava/QMinute.java +++ b/src/main/java/com/exxeleron/qjava/QMinute.java @@ -15,18 +15,20 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.util.Calendar; import java.util.Date; /** * Represents q minute type. */ -public final class QMinute implements DateTime { +public final class QMinute implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; private static final String NULL_STR = "0Nu"; - private Date datetime; - private Integer value; + private transient Date datetime; + private final Integer value; /** * Creates new {@link QMinute} instance using specified q date value. diff --git a/src/main/java/com/exxeleron/qjava/QMonth.java b/src/main/java/com/exxeleron/qjava/QMonth.java index 9b2de2a..bc6edf0 100644 --- a/src/main/java/com/exxeleron/qjava/QMonth.java +++ b/src/main/java/com/exxeleron/qjava/QMonth.java @@ -15,6 +15,7 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; @@ -23,14 +24,15 @@ /** * Represents q month type. */ -public final class QMonth implements DateTime { +public final class QMonth implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; private static final String NULL_STR = "0Nm"; private static final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM'm'"); - private Date datetime; - private Integer value; + private transient Date datetime; + private final Integer value; /** * Creates new {@link QMonth} instance using specified q date value. diff --git a/src/main/java/com/exxeleron/qjava/QSecond.java b/src/main/java/com/exxeleron/qjava/QSecond.java index 5daabd9..a96247c 100644 --- a/src/main/java/com/exxeleron/qjava/QSecond.java +++ b/src/main/java/com/exxeleron/qjava/QSecond.java @@ -15,18 +15,20 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.util.Calendar; import java.util.Date; /** * Represents q second type. */ -public final class QSecond implements DateTime { +public final class QSecond implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; private static final String NULL_STR = "0Nv"; - private Date datetime; - private Integer value; + private transient Date datetime; + private final Integer value; /** * Creates new {@link QSecond} instance using specified q date value. diff --git a/src/main/java/com/exxeleron/qjava/QTime.java b/src/main/java/com/exxeleron/qjava/QTime.java index 55b2e1d..d4b4b1f 100644 --- a/src/main/java/com/exxeleron/qjava/QTime.java +++ b/src/main/java/com/exxeleron/qjava/QTime.java @@ -15,18 +15,20 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.util.Calendar; import java.util.Date; /** * Represents q time type. */ -public final class QTime implements DateTime { +public final class QTime implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; private static final String NULL_STR = "0Nt"; - private Date datetime; - private Integer value; + private transient Date datetime; + private final Integer value; /** * Creates new {@link QTime} instance using specified q date value. diff --git a/src/main/java/com/exxeleron/qjava/QTimespan.java b/src/main/java/com/exxeleron/qjava/QTimespan.java index e81ecb7..b4f3742 100644 --- a/src/main/java/com/exxeleron/qjava/QTimespan.java +++ b/src/main/java/com/exxeleron/qjava/QTimespan.java @@ -15,6 +15,7 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.NumberFormat; @@ -25,7 +26,9 @@ /** * Represents q timespan type. */ -public final class QTimespan implements DateTime { +public final class QTimespan implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; + private static final String NULL_STR = "0Nn"; private static final DateFormat dateFormat = new SimpleDateFormat("'D'HH:mm:ss.SSS"); @@ -33,8 +36,8 @@ public final class QTimespan implements DateTime { private static final int NANOS_PER_SECOND = 1000000; private static final long NANOS_PER_DAY = Utils.DAY_MILLIS * 1000000; - private Date datetime; - private Long value; + private transient Date datetime; + private final Long value; /** * Creates new {@link QTimespan} instance using specified q date value. diff --git a/src/main/java/com/exxeleron/qjava/QTimestamp.java b/src/main/java/com/exxeleron/qjava/QTimestamp.java index 6ffa670..fd008ca 100644 --- a/src/main/java/com/exxeleron/qjava/QTimestamp.java +++ b/src/main/java/com/exxeleron/qjava/QTimestamp.java @@ -15,6 +15,7 @@ */ package com.exxeleron.qjava; +import java.io.Serializable; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.NumberFormat; @@ -24,15 +25,17 @@ /** * Represents q timestamp type. */ -public final class QTimestamp implements DateTime { +public final class QTimestamp implements DateTime, Serializable { + private static final long serialVersionUID = 762296525233866140L; + private static final String NULL_STR = "0Np"; private static final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd'D'HH:mm:ss.SSS"); private static final NumberFormat nanosFormatter = new DecimalFormat("000000"); private static final int NANOS_PER_SECOND = 1000000; - private Date datetime; - private Long value; + private transient Date datetime; + private final Long value; /** * Creates new {@link QTimestamp} instance using specified q date value. diff --git a/src/test/java/com/exxeleron/qjava/TestDateTime.java b/src/test/java/com/exxeleron/qjava/TestDateTime.java index 2eff451..5046f3f 100644 --- a/src/test/java/com/exxeleron/qjava/TestDateTime.java +++ b/src/test/java/com/exxeleron/qjava/TestDateTime.java @@ -1,12 +1,12 @@ /** * Copyright (c) 2011-2014 Exxeleron GmbH - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,6 +17,12 @@ import static org.junit.Assert.assertEquals; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; import java.util.Calendar; import java.util.Date; @@ -24,6 +30,21 @@ public class TestDateTime { + private static byte[] pickle( final T obj ) throws IOException { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final ObjectOutputStream oos = new ObjectOutputStream(baos); + oos.writeObject(obj); + oos.close(); + return baos.toByteArray(); + } + + private static T unpickle( final byte[] b, final Class cl ) throws IOException, ClassNotFoundException { + final ByteArrayInputStream bais = new ByteArrayInputStream(b); + final ObjectInputStream ois = new ObjectInputStream(bais); + final Object o = ois.readObject(); + return cl.cast(o); + } + @Test public void testQDate() { final Calendar c = Calendar.getInstance(); @@ -65,6 +86,18 @@ public void testQDateFromString() { assertEquals(new QDate(Integer.MIN_VALUE), QDate.fromString("0Nd")); } + @Test + public void testQDateSerialization() throws ClassNotFoundException, IOException { + final QDate[] ref = new QDate[] { new QDate(-1645), new QDate(-365), new QDate(0), new QDate(3653), new QDate(Integer.MIN_VALUE) }; + for ( int i = 0; i < ref.length; i++ ) { + final QDate copy = unpickle(pickle(ref[i]), QDate.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } + @Test public void testQDateTime() { final Calendar c = Calendar.getInstance(); @@ -103,6 +136,19 @@ public void testQDateTimeFromString() { assertEquals(new QDateTime(Double.NaN), QDateTime.fromString("0Nz")); } + @Test + public void testQDateTimeSerialization() throws ClassNotFoundException, IOException { + final QDateTime[] ref = new QDateTime[] { new QDateTime(-364.0000115), new QDateTime(2008.0833218), new QDateTime(0.), new QDateTime(3653.599792), + new QDateTime(Double.NaN) }; + for ( int i = 0; i < ref.length; i++ ) { + final QDateTime copy = unpickle(pickle(ref[i]), QDateTime.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } + @Test public void testQMinute() { final Calendar c = Calendar.getInstance(); @@ -142,6 +188,18 @@ public void testQMinuteFromString() { assertEquals(new QMinute(Integer.MIN_VALUE), QMinute.fromString("0Nu")); } + @Test + public void testQMinuteSerialization() throws ClassNotFoundException, IOException { + final QMinute[] ref = new QMinute[] { new QMinute(-1645), new QMinute(0), new QMinute(3653), new QMinute(Integer.MIN_VALUE) }; + for ( int i = 0; i < ref.length; i++ ) { + final QMinute copy = unpickle(pickle(ref[i]), QMinute.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } + @Test public void testQMonth() { final Calendar c = Calendar.getInstance(); @@ -183,6 +241,18 @@ public void testQMonthFromString() { assertEquals(new QMonth(Integer.MIN_VALUE), QMonth.fromString("0Nm")); } + @Test + public void testQMonthSerialization() throws ClassNotFoundException, IOException { + final QMonth[] ref = new QMonth[] { new QMonth(-1645), new QMonth(0), new QMonth(3653), new QMonth(Integer.MIN_VALUE) }; + for ( int i = 0; i < ref.length; i++ ) { + final QMonth copy = unpickle(pickle(ref[i]), QMonth.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } + @Test public void testQSecond() { final Calendar c = Calendar.getInstance(); @@ -222,6 +292,18 @@ public void testQSecondFromString() { assertEquals(new QSecond(Integer.MIN_VALUE), QSecond.fromString("0Nv")); } + @Test + public void testQSecondSerialization() throws ClassNotFoundException, IOException { + final QSecond[] ref = new QSecond[] { new QSecond(-1645), new QSecond(0), new QSecond(3653), new QSecond(Integer.MIN_VALUE) }; + for ( int i = 0; i < ref.length; i++ ) { + final QSecond copy = unpickle(pickle(ref[i]), QSecond.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } + @Test public void testQTime() { final Calendar c = Calendar.getInstance(); @@ -262,6 +344,18 @@ public void testQTimeFromString() { assertEquals(new QTime(Integer.MIN_VALUE), QTime.fromString("0Nt")); } + @Test + public void testQTimeSerialization() throws ClassNotFoundException, IOException { + final QTime[] ref = new QTime[] { new QTime(-1645), new QTime(0), new QTime(3653), new QTime(Integer.MIN_VALUE) }; + for ( int i = 0; i < ref.length; i++ ) { + final QTime copy = unpickle(pickle(ref[i]), QTime.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } + @Test public void testQTimespan() { final Calendar c = Calendar.getInstance(); @@ -307,6 +401,19 @@ public void testQTimespanFromString() { assertEquals(new QTimespan(Long.MIN_VALUE), QTimespan.fromString("0Nn")); } + @Test + public void testQTimespanSerialization() throws ClassNotFoundException, IOException { + final QTimespan[] ref = new QTimespan[] { new QTimespan(-259199000000000L), new QTimespan(0L), new QTimespan(48613000000000L), + new QTimespan(Long.MIN_VALUE) }; + for ( int i = 0; i < ref.length; i++ ) { + final QTimespan copy = unpickle(pickle(ref[i]), QTimespan.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } + @Test public void testQTimestamp() { final Calendar c = Calendar.getInstance(); @@ -348,4 +455,17 @@ public void testQTimestampFromString() { assertEquals(new QTimestamp(Long.MIN_VALUE), QTimestamp.fromString("")); assertEquals(new QTimestamp(Long.MIN_VALUE), QTimestamp.fromString("0Np")); } + + @Test + public void testQTimestampSerialization() throws ClassNotFoundException, IOException { + final QTimestamp[] ref = new QTimestamp[] { new QTimestamp(-259199000000000L), new QTimestamp(0L), new QTimestamp(48613000000000L), + new QTimestamp(Long.MIN_VALUE) }; + for ( int i = 0; i < ref.length; i++ ) { + final QTimestamp copy = unpickle(pickle(ref[i]), QTimestamp.class); + + assertEquals(ref[i], copy); + assertEquals(ref[i].toString(), copy.toString()); + assertEquals(ref[i].getValue(), copy.getValue()); + } + } }