Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
keilw committed Oct 8, 2020
2 parents 9357657 + 9c258a5 commit 233f00f
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/main/java/tech/units/indriya/unit/ProductUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*/
package tech.units.indriya.unit;

import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -60,7 +62,7 @@
* @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
* @author <a href="mailto:werner@units.tech">Werner Keil</a>
* @author Andi Huber
* @version 1.11, September 27, 2020
* @version 1.12, October 1, 2020
* @since 1.0
*/
public final class ProductUnit<Q extends Quantity<Q>> extends AbstractUnit<Q> {
Expand Down Expand Up @@ -560,4 +562,36 @@ private static boolean arrayEqualsArbitraryOrder(final Element[] e0, final Eleme
return true;
}
}


// -- SERIALIZATION PROXY

// Chapter 12. Serialization
// Bloch, Joshua. Effective Java (p. 339). Pearson Education.

private Object writeReplace() {
return new SerializationProxy(this);
}

private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}

private static class SerializationProxy implements Serializable {
private static final long serialVersionUID = 1L;
private final Element[] elements;
private final String symbol;

private SerializationProxy(ProductUnit<?> productUnit) {
this.elements = productUnit.elements;
this.symbol = productUnit.getSymbol();
}

private Object readResolve() {
ProductUnit<?> pu = new ProductUnit<>(elements);
pu.setSymbol(symbol);
return pu;
}
}

}
78 changes: 78 additions & 0 deletions src/test/java/tech/units/indriya/SerializationRoundTrip.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Units of Measurement Reference Implementation
* Copyright (c) 2005-2020, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-385, Indriya nor the names of their contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.units.indriya;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.jupiter.api.Assertions;

/**
*
* @author Andi Huber
*/
public final class SerializationRoundTrip {

/**
* Verify that after a serializing de-serializing cycle given {@code value} is equal to
* the original value.
* @param value
*/
public static void assertProperSerializationRoundTrip(Object value) {
try {
Assertions.assertEquals(value, serializationRoundTrip(value));
} catch (Exception e) {
Assertions.fail(e);
}
}

/**
* Returns given {@code value} after doing a serializing de-serializing cycle on it.
* @param value
*/
@SuppressWarnings("unchecked")
public static <T> T serializationRoundTrip(final T value) throws Exception {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(value);
oos.flush();
}
try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
try (ObjectInputStream ois = new ObjectInputStream(in)) {
return (T) ois.readObject();
}
}
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Units of Measurement Reference Implementation
* Copyright (c) 2005-2020, Jean-Marie Dautelle, Werner Keil, Otavio Santana.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
* and the following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of JSR-385, Indriya nor the names of their contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package tech.units.indriya.unit;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import tech.units.indriya.SerializationRoundTrip;

/**
*
* @author Andi Huber
*/
class ProductUnitSerializationTest {

private static final ProductUnit<?> KILOGRAM_METRE =
(ProductUnit<?>) ProductUnit.ofProduct(Units.KILOGRAM, Units.METRE);

@Test
public void productUnitsShouldProperlySerialize() throws Exception {
SerializationRoundTrip.assertProperSerializationRoundTrip(KILOGRAM_METRE);

final ProductUnit<?> unitAfterRoundTrip = SerializationRoundTrip.serializationRoundTrip(KILOGRAM_METRE);

assertEquals(KILOGRAM_METRE.hashCode(), unitAfterRoundTrip.hashCode());
assertEquals(KILOGRAM_METRE.getSymbol(), unitAfterRoundTrip.getSymbol());
assertEquals(KILOGRAM_METRE.getDimension(), unitAfterRoundTrip.getDimension());
assertEquals(KILOGRAM_METRE.getName(), unitAfterRoundTrip.getName());
assertEquals(KILOGRAM_METRE.getUnitCount(), unitAfterRoundTrip.getUnitCount());
assertEquals(KILOGRAM_METRE.getBaseUnits(), unitAfterRoundTrip.getBaseUnits());

}

}

0 comments on commit 233f00f

Please sign in to comment.