From c38e0345a03c32ba40bc117ca16dab1611196d1b Mon Sep 17 00:00:00 2001 From: devjeonghwan Date: Mon, 21 Aug 2023 04:59:35 +0900 Subject: [PATCH 1/2] Add transformers and Fix errors --- .github/workflows/windows-x64.yml | 26 ++-- README.md | 145 ++++++++++-------- .../java/com/realtimetech/opack/Opacker.java | 67 +++++--- .../realtimetech/opack/bake/TypeBaker.java | 8 +- .../realtimetech/opack/codec/OpackCodec.java | 3 - .../opack/transformer/Transformer.java | 7 +- .../impl/DataStructureTransformer.java | 1 - .../transformer/impl/TypeWrapTransformer.java | 91 +++++++++++ .../impl/file/FileTransformer.java | 70 +++++++++ .../impl/list/ListTransformer.java | 16 +- .../impl/list/WrapListTransformer.java | 2 +- .../transformer/impl/map/MapTransformer.java | 12 +- .../impl/map/WrapMapTransformer.java | 2 +- .../impl/path/PathTransformer.java | 70 +++++++++ .../impl/time/CalendarTransformer.java | 75 +++++++++ .../DateTransformer.java} | 27 +++- .../time/java8/LocalDateTimeTransformer.java | 95 ++++++++++++ .../impl/time/java8/LocalDateTransformer.java | 87 +++++++++++ .../impl/time/java8/LocalTimeTransformer.java | 89 +++++++++++ .../opack/util/OpackArrayConverter.java | 1 - .../realtimetech/opack/value/OpackArray.java | 7 +- .../realtimetech/opack/test/OpackAssert.java | 9 ++ .../opacker/array/PrimitiveArrayTest.java | 16 +- .../test/opacker/array/WrapperArrayTest.java | 16 +- .../opack/test/opacker/map/MapTest.java | 3 +- .../opack/test/opacker/other/ComplexTest.java | 8 +- .../test/opacker/single/PrimitiveTest.java | 1 - .../opacker/transform/TransformClassTest.java | 8 +- .../opacker/transform/TransformFieldTest.java | 15 +- .../transform/TypeWrapTransformTest.java | 69 +++++++++ .../transform/file/FileTransformTest.java | 54 +++++++ .../transform/path/PathTransformTest.java | 54 +++++++ .../time/Java8TimeTransformTest.java | 60 ++++++++ .../transform/time/TimeTransformTest.java | 59 +++++++ .../performance/JacksonPerformanceTest.java | 4 - 35 files changed, 1118 insertions(+), 159 deletions(-) create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java rename src/main/java/com/realtimetech/opack/transformer/impl/{date/DateToLongTransformer.java => time/DateTransformer.java} (76%) create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java create mode 100644 src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java create mode 100644 src/test/java/com/realtimetech/opack/test/opacker/transform/TypeWrapTransformTest.java create mode 100644 src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java create mode 100644 src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java create mode 100644 src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java create mode 100644 src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java diff --git a/.github/workflows/windows-x64.yml b/.github/workflows/windows-x64.yml index 7d80833..e05cc5e 100644 --- a/.github/workflows/windows-x64.yml +++ b/.github/workflows/windows-x64.yml @@ -2,24 +2,24 @@ name: windows-x64 on: push: - branches: [main] + branches: [ main ] pull_request: - branches: [main] + branches: [ main ] jobs: build: - runs-on: [self-hosted, windows, x64] + runs-on: [ self-hosted, windows, x64 ] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v2 - - name: Set up JDK 11 - uses: actions/setup-java@v2 - with: - java-version: '11' - distribution: 'adopt' - architecture: x64 + - name: Set up JDK 11 + uses: actions/setup-java@v2 + with: + java-version: '11' + distribution: 'adopt' + architecture: x64 - - uses: ilammy/msvc-dev-cmd@v1 - - name: Test - run: ./gradlew test + - uses: ilammy/msvc-dev-cmd@v1 + - name: Test + run: ./gradlew test diff --git a/README.md b/README.md index de0add9..eb9a808 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@

-Opack is a Java library that can serialize/deserialize between Java objects and common objects(OpackValue). Also, common objects can be encoded or decoded as JSON or Bytes(Dense). +Opack is a Java library that can serialize/deserialize between Java objects and common objects(OpackValue). Also, common +objects can be encoded or decoded as JSON or Bytes(Dense). **We faster then GSON and Kryo.** (See [tests](./src/test/java/com/realtimetech/opack/test/performance))
@@ -28,22 +29,23 @@ Opack is a Java library that can serialize/deserialize between Java objects and Jackson: 6412ms Opack : 6404ms ``` -
+ ### Simple flow +

- +

- ### Download Gradle: + ```gradle dependencies { implementation 'com.realtimetech:opack:0.0.9' @@ -51,103 +53,112 @@ dependencies { ``` Maven: + ```xml + - com.realtimetech - opack - 0.0.9 + com.realtimetech + opack + 0.0.9 ``` ### Usage + #### 1. Serialize + ```java -Opacker opacker = new Opacker.Builder().create(); +Opacker opacker=new Opacker.Builder().create(); -SomeObject someObject = new SomeObject(); + SomeObject someObject=new SomeObject(); -OpackValue opackValue = opacker.serialize(someObject); + OpackValue opackValue=opacker.serialize(someObject); ``` #### 2. Deserialize + ```java -Opacker opacker = new Opacker.Builder() +Opacker opacker=new Opacker.Builder() .setContextStackInitialSize(128) // (Optional) Creation size of stack for processing .setValueStackInitialSize(512) // (Optional) Creation size of stack for processing - + .setEnableWrapListElementType(false) // (Optional) When converting elements of a list, record the type as well .setEnableWrapMapElementType(false) // (Optional) When converting elements of a map, record the type as well .setEnableConvertEnumToOrdinal(false) // (Optional) Convert enum to ordinal or name .setEnableConvertRecursiveDependencyToNull(false); // (Optional) Convert recursive depandency, record null - + .create(); -OpackValue serializedSomeObject = /** See Serialize Usage **/; + OpackValue serializedSomeObject= /** See Serialize Usage **/; -SomeObject someObject = opacker.deserialize(SomeObject.class, serializedSomeObject); + SomeObject someObject=opacker.deserialize(SomeObject.class,serializedSomeObject); ``` #### 3. Json Codec + ```java -JsonCodec jsonCodec = new JsonCodec.Builder() +JsonCodec jsonCodec=new JsonCodec.Builder() .setEncodeStackInitialSize(128) // (Optional) Creation size of stack for processing .setEncodeStringBufferSize(1024) // (Optional) Creation size of stack for processing .setDecodeStackInitialSize(128) // (Optional) Creation size of stack for processing - + .setAllowOpackValueToKeyValue(false) // (Optional) Accepts Objct or Array as Key of Json Object .setEnableConvertCharacterToString(false) // (Optional) Convert character to string instead of character int value .setUsePrettyFormat(false) // (Optional) When encoding, it prints formatted - + .create(); -OpackValue opackValue = /** See Serialize Usage **/; + OpackValue opackValue= /** See Serialize Usage **/; /* Encode */ -String json = jsonCodec.encode(opackValue); + String json=jsonCodec.encode(opackValue); // Or -Writer writer = new StringWriter(); -jsonCodec.encode(writer, opackValue); + Writer writer=new StringWriter(); + jsonCodec.encode(writer,opackValue); /* Decode */ -OpackValue decodedOpackValue = jsonCodec.decode(json); + OpackValue decodedOpackValue=jsonCodec.decode(json); ``` #### 4. Dense Codec + ```java -DenseCodec denseCodec = new DenseCodec.Builder() +DenseCodec denseCodec=new DenseCodec.Builder() .setEncodeStackInitialSize(128) // (Optional) Creation size of stack for processing .setEncodeOutputBufferInitialSize(1024) // (Optional) Creation size of stack for processing .setDecodeStackInitialSize(128) // (Optional) Creation size of stack for processing - + .setIgnoreVersionCompare(false) // (Optional) Ignore compare dense codec version in data - + .create(); -OpackValue opackValue = /** See Serialize Usage **/; + OpackValue opackValue= /** See Serialize Usage **/; /* Encode */ -byte[] bytes = denseCodec.encode(opackValue); + byte[]bytes=denseCodec.encode(opackValue); // Or -OutputStream outputStream = new ByteArrayOutputStream(); -denseCodec.encode(outputStream, opackValue); + OutputStream outputStream=new ByteArrayOutputStream(); + denseCodec.encode(outputStream,opackValue); /* Decode */ -OpackValue decodedOpackValue1 = denseCodec.decode(bytes); + OpackValue decodedOpackValue1=denseCodec.decode(bytes); // Or -InputStream inputStream = new ByteArrayInputStream(bytes); -OpackValue decodedOpackValue2 = denseCodec.decode(inputStream); + InputStream inputStream=new ByteArrayInputStream(bytes); + OpackValue decodedOpackValue2=denseCodec.decode(inputStream); ``` ### Advanced Usage + #### 1. Ignore and Type and Name + ```java public class SomeObject { private String stringField; @@ -172,7 +183,9 @@ public class SomeObject { private String oldFieldName; } ``` + #### 2. Field Transformer + ```java public class ByteToBase64Transformer implements Transformer { @Override @@ -202,7 +215,9 @@ public class SomeObject { private byte[] bytesField; } ``` + #### 3. Class Transformer + ```java public class AnimalTransformer implements Transformer { /* @@ -272,44 +287,52 @@ public class SomeObject { ``` #### 4. Handling Opack Value + ```java -OpackObject rootObject = new OpackObject<>(); +OpackObject rootObject=new OpackObject<>(); -{ - OpackArray opackArray = new OpackArray<>(); - opackArray.add(Integer.MAX_VALUE); - rootObject.put("array", opackArray); -} + { + OpackArray opackArray=new OpackArray<>(); + opackArray.add(Integer.MAX_VALUE); + rootObject.put("array",opackArray); + } -{ - OpackArray opackArray = OpackArray.createWithArrayObject(new int[]{1, 2, 3, 4, 5, 6}); - rootObject.put("unmodifiable(but, really fast) array", opackArray); -} + { + OpackArray opackArray=OpackArray.createWithArrayObject(new int[]{1,2,3,4,5,6}); + rootObject.put("unmodifiable(but, really fast) array",opackArray); + } -{ - OpackObject opackObject = new OpackObject<>(); - opackObject.put("int", 1); - opackObject.put("float", 1.1f); - opackObject.put("long", Long.MAX_VALUE); - opackObject.put("double", 1.1d); + { + OpackObject opackObject=new OpackObject<>(); + opackObject.put("int",1); + opackObject.put("float",1.1f); + opackObject.put("long",Long.MAX_VALUE); + opackObject.put("double",1.1d); - opackObject.put(1024, "2^10"); - opackObject.put( - OpackArray.createWithArrayObject(new byte[]{1,2,3,4,5}), - "a lot of bytes" - ); + opackObject.put(1024,"2^10"); + opackObject.put( + OpackArray.createWithArrayObject(new byte[]{1,2,3,4,5}), + "a lot of bytes" + ); - rootObject.put("number_map", opackObject); -} + rootObject.put("number_map",opackObject); + } -OpackArray opackArray = (OpackArray) rootObject.get("array"); -OpackObject opackObject = (OpackObject) rootObject.get("number_map"); + OpackArray opackArray=(OpackArray)rootObject.get("array"); + OpackObject opackObject=(OpackObject)rootObject.get("number_map"); -System.out.println("1024 is " + (opackObject.get(1024))); -System.out.println("Array length is " + (opackArray.length())); -System.out.println("First element is " + (opackArray.get(0))); + System.out.println("1024 is "+(opackObject.get(1024))); + System.out.println("Array length is "+(opackArray.length())); + System.out.println("First element is "+(opackArray.get(0))); ``` +### To-Do + +- [ ] Separate field transformer and class transformer +- [ ] Add generic into transformer for type safety +- [ ] Add field pre/post transformer +- [ ] Remove `fieldTransformer` argument of `Opacker.prepareObjectDeserialize` + ### License Opack uses [Apache License 2.0](./LICENSE.txt). Please, leave your feedback if you have any suggestions! diff --git a/src/main/java/com/realtimetech/opack/Opacker.java b/src/main/java/com/realtimetech/opack/Opacker.java index bbf9183..44feda1 100644 --- a/src/main/java/com/realtimetech/opack/Opacker.java +++ b/src/main/java/com/realtimetech/opack/Opacker.java @@ -28,10 +28,17 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.transformer.Transformer; +import com.realtimetech.opack.transformer.impl.file.FileTransformer; import com.realtimetech.opack.transformer.impl.list.ListTransformer; import com.realtimetech.opack.transformer.impl.list.WrapListTransformer; import com.realtimetech.opack.transformer.impl.map.MapTransformer; import com.realtimetech.opack.transformer.impl.map.WrapMapTransformer; +import com.realtimetech.opack.transformer.impl.path.PathTransformer; +import com.realtimetech.opack.transformer.impl.time.CalendarTransformer; +import com.realtimetech.opack.transformer.impl.time.DateTransformer; +import com.realtimetech.opack.transformer.impl.time.java8.LocalDateTimeTransformer; +import com.realtimetech.opack.transformer.impl.time.java8.LocalDateTransformer; +import com.realtimetech.opack.transformer.impl.time.java8.LocalTimeTransformer; import com.realtimetech.opack.util.OpackArrayConverter; import com.realtimetech.opack.util.ReflectionUtil; import com.realtimetech.opack.util.structure.FastStack; @@ -41,11 +48,14 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.File; import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; -import java.util.HashSet; -import java.util.List; -import java.util.Map; +import java.nio.file.Path; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.*; public class Opacker { public static class Builder { @@ -150,6 +160,16 @@ private Opacker(@NotNull Builder builder) { } else { this.typeBaker.registerPredefinedTransformer(Map.class, MapTransformer.class, true); } + + this.typeBaker.registerPredefinedTransformer(File.class, FileTransformer.class, true); + this.typeBaker.registerPredefinedTransformer(Path.class, PathTransformer.class, true); + + this.typeBaker.registerPredefinedTransformer(Date.class, DateTransformer.class, true); + this.typeBaker.registerPredefinedTransformer(Calendar.class, CalendarTransformer.class, true); + + this.typeBaker.registerPredefinedTransformer(LocalDate.class, LocalDateTransformer.class, true); + this.typeBaker.registerPredefinedTransformer(LocalTime.class, LocalTimeTransformer.class, true); + this.typeBaker.registerPredefinedTransformer(LocalDateTime.class, LocalDateTimeTransformer.class, true); } catch (InstantiationException exception) { throw new IllegalStateException(exception); } @@ -204,7 +224,7 @@ public synchronized OpackValue serialize(@NotNull Object object) throws Serializ BakedType bakedType = this.typeBaker.get(baseType); for (Transformer transformer : bakedType.getTransformers()) { - object = transformer.serialize(this, object); + object = transformer.serialize(this, baseType, object); if (object == null) { return null; @@ -317,7 +337,7 @@ private void executeSerializeStack(int endOfStack) throws SerializeException { Class fieldType = property.getType(); if (property.getTransformer() != null) { - element = property.getTransformer().serialize(this, element); + element = property.getTransformer().serialize(this, fieldType, element); } if (element != null) { @@ -347,7 +367,8 @@ public synchronized T deserialize(@NotNull Class type, @NotNull OpackValu throw new DeserializeException("Opacker is serializing."); int separatorStack = this.objectStack.getSize(); - Object object = this.prepareObjectDeserialize(type, opackValue); + + Object object = this.prepareObjectDeserialize(type, opackValue, null); if (object == null) { return null; @@ -378,12 +399,22 @@ public synchronized T deserialize(@NotNull Class type, @NotNull OpackValu * @return prepared object * @throws DeserializeException if a problem occurs during deserializing */ - private synchronized @Nullable Object prepareObjectDeserialize(@NotNull Class goalType, @NotNull Object object) throws DeserializeException { + private synchronized @Nullable Object prepareObjectDeserialize(@NotNull Class goalType, @NotNull Object object, @Nullable Transformer fieldTransformer) throws DeserializeException { try { BakedType bakedType = this.typeBaker.get(goalType); - for (Transformer transformer : bakedType.getTransformers()) { - object = transformer.deserialize(this, goalType, object); + Transformer[] transformers = bakedType.getTransformers(); + + for (int index = transformers.length - 1; index >= 0; index--) { + object = transformers[index].deserialize(this, goalType, object); + + if (object == null) { + return null; + } + } + + if (fieldTransformer != null) { + object = fieldTransformer.deserialize(this, goalType, object); if (object == null) { return null; @@ -465,7 +496,7 @@ public synchronized T deserialize(@NotNull Class type, @NotNull OpackValu this.typeStack.push(bakedType); return targetObject; - } else if (object.getClass() == goalType) { + } else if (goalType.isAssignableFrom(object.getClass())) { return object; } else { throw new DeserializeException("Found object, stack corruption."); @@ -495,7 +526,7 @@ private void executeDeserializeStack(int endOfStack) throws DeserializeException Object element = opackArray.get(index); if (element != null) { - Object deserializedValue = this.prepareObjectDeserialize(componentType, element); + Object deserializedValue = this.prepareObjectDeserialize(componentType, element, null); if (deserializedValue != null) { ReflectionUtil.setArrayItem(object, index, ReflectionUtil.cast(componentType, deserializedValue)); @@ -514,21 +545,17 @@ private void executeDeserializeStack(int endOfStack) throws DeserializeException Class fieldType = property.getType(); Class actualFieldType = property.getField().getType(); - if (property.getTransformer() != null) { - element = property.getTransformer().deserialize(this, fieldType, element); - } + Object propertyValue = null; if (element != null) { - Object deserializedValue = this.prepareObjectDeserialize(fieldType, element); + Object deserializedValue = this.prepareObjectDeserialize(fieldType, element, property.getTransformer()); if (deserializedValue != null) { - property.set(object, ReflectionUtil.cast(actualFieldType, deserializedValue)); - } else { - property.set(object, null); + propertyValue = ReflectionUtil.cast(actualFieldType, deserializedValue); } - } else { - property.set(object, null); } + + property.set(object, propertyValue); } catch (IllegalAccessException | IllegalArgumentException exception) { throw new DeserializeException("Can't set " + property.getName() + " field in " + bakedType.getType().getSimpleName() + ".", exception); } diff --git a/src/main/java/com/realtimetech/opack/bake/TypeBaker.java b/src/main/java/com/realtimetech/opack/bake/TypeBaker.java index 9fd56e7..f739538 100644 --- a/src/main/java/com/realtimetech/opack/bake/TypeBaker.java +++ b/src/main/java/com/realtimetech/opack/bake/TypeBaker.java @@ -23,10 +23,10 @@ package com.realtimetech.opack.bake; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.annotation.Ignore; import com.realtimetech.opack.annotation.Name; import com.realtimetech.opack.annotation.Transform; +import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.exception.BakeException; import com.realtimetech.opack.transformer.Transformer; import com.realtimetech.opack.transformer.TransformerFactory; @@ -203,7 +203,7 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N if (predefinedTransformers != null) { for (PredefinedTransformer predefinedTransformer : predefinedTransformers) { - if (root || predefinedTransformer.isInheritable()) { + if (root || predefinedTransformer.isInheritable() && !transformers.contains(predefinedTransformer.getTransformer())) { transformers.add(predefinedTransformer.getTransformer()); } } @@ -233,9 +233,11 @@ private void addTransformer(@NotNull List<@NotNull Transformer> transformers, @N * @return transformers * @throws BakeException if transformer class object cannot be instantiated */ - private @NotNull Transformer @NotNull[] getTransformer(@NotNull AnnotatedElement annotatedElement) throws BakeException { + private @NotNull Transformer @NotNull [] getTransformer(@NotNull AnnotatedElement annotatedElement) throws BakeException { List transformers = new LinkedList<>(); + this.addTransformer(transformers, annotatedElement, true); + return transformers.toArray(new Transformer[0]); } diff --git a/src/main/java/com/realtimetech/opack/codec/OpackCodec.java b/src/main/java/com/realtimetech/opack/codec/OpackCodec.java index 1f6b8d9..f99f04e 100644 --- a/src/main/java/com/realtimetech/opack/codec/OpackCodec.java +++ b/src/main/java/com/realtimetech/opack/codec/OpackCodec.java @@ -28,9 +28,6 @@ import org.jetbrains.annotations.NotNull; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.Writer; public abstract class OpackCodec { /** diff --git a/src/main/java/com/realtimetech/opack/transformer/Transformer.java b/src/main/java/com/realtimetech/opack/transformer/Transformer.java index 5992adb..3ead26a 100644 --- a/src/main/java/com/realtimetech/opack/transformer/Transformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/Transformer.java @@ -32,15 +32,18 @@ public interface Transformer { /** * Serialize specific value to opack value. * - * @param value the value to be serialized + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ - @Nullable Object serialize(@NotNull Opacker opacker, @Nullable Object value) throws SerializeException; + @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException; /** * Deserialize opack value. * + * @param opacker the opacker * @param value the opack value to be deserialized * @param goalType the goal type to deserialize * @return deserialized value diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java index 8a05177..5d77376 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/DataStructureTransformer.java @@ -26,7 +26,6 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.transformer.Transformer; -import com.realtimetech.opack.value.OpackObject; import com.realtimetech.opack.value.OpackValue; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java new file mode 100644 index 0000000..fc3cdc1 --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/TypeWrapTransformer.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2021 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.Transformer; +import com.realtimetech.opack.value.OpackObject; +import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class TypeWrapTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ + @Override + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value != null) { + OpackValue opackValue = opacker.serialize(value); + OpackObject opackObject = new OpackObject<>(); + + opackObject.put("type", value.getClass().getName()); + opackObject.put("value", opackValue); + + return opackObject; + } + + return value; + } + + /** + * Deserialize opack value. + * + * @param opacker the opacker + * @param goalType the goal type to deserialize + * @param value the opack value to be deserialized + * @return deserialized value + * @throws DeserializeException if a problem occurs during deserializing + */ + @Override + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { + if (value instanceof OpackObject) { + OpackObject opackObject = (OpackObject) value; + + if (opackObject.containsKey("type") && opackObject.containsKey("value")) { + String type = (String) opackObject.get("type"); + Object opackValue = opackObject.get("value"); + + try { + Class objectClass = Class.forName(type); + + if (opackValue instanceof OpackValue) { + return opacker.deserialize(objectClass, (OpackValue) value); + } + } catch (ClassNotFoundException classNotFoundException) { + throw new DeserializeException(classNotFoundException); + } + } + } + + return value; + } +} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java new file mode 100644 index 0000000..c0b28c2 --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/file/FileTransformer.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2022 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl.file; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.Transformer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; + +public class FileTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ + @Override + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value instanceof File) { + return value.toString(); + } + + return value; + } + + /** + * Deserialize opack value. + * + * @param opacker the opacker + * @param goalType the goal type to deserialize + * @param value the opack value to be deserialized + * @return deserialized value + * @throws DeserializeException if a problem occurs during deserializing + */ + @Override + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { + if (value instanceof String) { + return new File(String.valueOf(value)); + } + + return value; + } +} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java index 9c0a533..10574a0 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/list/ListTransformer.java @@ -37,15 +37,16 @@ public class ListTransformer extends DataStructureTransformer { /** - * Serializes the list to {@link OpackArray OpackArray}. + * Serialize specific value to opack value. * - * @param opacker the opacker - * @param value the value to serialize - * @return serialized value + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @Nullable Object value) throws SerializeException { + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { if (value instanceof List) { List list = (List) value; OpackArray opackArray = new OpackArray<>(list.size()); @@ -63,7 +64,7 @@ public class ListTransformer extends DataStructureTransformer { /** * Deserialize opack value. * - * @param opacker + * @param opacker the opacker * @param goalType the goal type to deserialize * @param value the opack value to be deserialized * @return deserialized value @@ -84,7 +85,8 @@ public class ListTransformer extends DataStructureTransformer { } return list; - } catch (InvocationTargetException | InstantiationException | IllegalAccessException | ClassNotFoundException exception) { + } catch (InvocationTargetException | InstantiationException | IllegalAccessException | + ClassNotFoundException exception) { throw new DeserializeException(exception); } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java index 0e9494f..150868a 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/list/WrapListTransformer.java @@ -65,7 +65,7 @@ public class WrapListTransformer extends ListTransformer { */ @Override protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws ClassNotFoundException, DeserializeException { - if (element != null && element instanceof OpackObject) { + if (element instanceof OpackObject) { OpackObject opackObject = (OpackObject) element; if (opackObject.containsKey("type") && opackObject.containsKey("value")) { diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java index 6939f2c..f439f0e 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/map/MapTransformer.java @@ -39,13 +39,14 @@ public class MapTransformer extends DataStructureTransformer { /** * Serialize specific value to opack value. * - * @param opacker - * @param value the value to be serialized + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @Nullable Object value) throws SerializeException { + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { if (value instanceof Map) { Map map = (Map) value; OpackObject opackObject = new OpackObject<>(map.size()); @@ -69,7 +70,7 @@ public class MapTransformer extends DataStructureTransformer { /** * Deserialize opack value. * - * @param opacker + * @param opacker the opacker * @param goalType the goal type to deserialize * @param value the opack value to be deserialized * @return deserialized value @@ -94,7 +95,8 @@ public class MapTransformer extends DataStructureTransformer { } return map; - } catch (InvocationTargetException | InstantiationException | IllegalAccessException | ClassNotFoundException exception) { + } catch (InvocationTargetException | InstantiationException | IllegalAccessException | + ClassNotFoundException exception) { throw new DeserializeException(exception); } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java index 669b938..50af33b 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/map/WrapMapTransformer.java @@ -65,7 +65,7 @@ public class WrapMapTransformer extends MapTransformer { */ @Override protected @Nullable Object deserializeObject(@NotNull Opacker opacker, @Nullable Object element) throws ClassNotFoundException, DeserializeException { - if (element != null && element instanceof OpackObject) { + if (element instanceof OpackObject) { OpackObject opackObject = (OpackObject) element; if (opackObject.containsKey("type") && opackObject.containsKey("value")) { diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java new file mode 100644 index 0000000..2cad36a --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/path/PathTransformer.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2022 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl.path; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.Transformer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.nio.file.Path; + +public class PathTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ + @Override + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value instanceof Path) { + return ((Path) value).toString(); + } + + return value; + } + + /** + * Deserialize opack value. + * + * @param opacker the opacker + * @param goalType the goal type to deserialize + * @param value the opack value to be deserialized + * @return deserialized value + * @throws DeserializeException if a problem occurs during deserializing + */ + @Override + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { + if (value instanceof String) { + return Path.of(String.valueOf(value)); + } + + return value; + } +} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java new file mode 100644 index 0000000..53307d7 --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/CalendarTransformer.java @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2022 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl.time; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.Transformer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Calendar; +import java.util.Date; + +public class CalendarTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ + @Override + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value instanceof Calendar) { + return ((Calendar) value).getTime().getTime(); + } + + return value; + } + + /** + * Deserialize opack value. + * + * @param opacker the opacker + * @param goalType the goal type to deserialize + * @param value the opack value to be deserialized + * @return deserialized value + * @throws DeserializeException if a problem occurs during deserializing + */ + @Override + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { + if (value instanceof Long) { + Calendar calendar = Calendar.getInstance(); + + calendar.setTime(new Date((Long) value)); + + return calendar; + } + + return value; + } +} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/date/DateToLongTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/DateTransformer.java similarity index 76% rename from src/main/java/com/realtimetech/opack/transformer/impl/date/DateToLongTransformer.java rename to src/main/java/com/realtimetech/opack/transformer/impl/time/DateTransformer.java index 223dd36..2c9f262 100644 --- a/src/main/java/com/realtimetech/opack/transformer/impl/date/DateToLongTransformer.java +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/DateTransformer.java @@ -20,7 +20,7 @@ * limitations under the License. */ -package com.realtimetech.opack.transformer.impl.date; +package com.realtimetech.opack.transformer.impl.time; import com.realtimetech.opack.Opacker; import com.realtimetech.opack.exception.DeserializeException; @@ -29,24 +29,31 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public class DateToLongTransformer implements Transformer { +import java.util.Date; + +public class DateTransformer implements Transformer { /** * Serialize specific value to opack value. * - * @param opacker - * @param value the value to be serialized + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized * @return opack value * @throws SerializeException if a problem occurs during serializing */ @Override - public @Nullable Object serialize(@NotNull Opacker opacker, @Nullable Object value) throws SerializeException { - return null; + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value instanceof Date) { + return ((Date) value).getTime(); + } + + return value; } /** * Deserialize opack value. * - * @param opacker + * @param opacker the opacker * @param goalType the goal type to deserialize * @param value the opack value to be deserialized * @return deserialized value @@ -54,6 +61,10 @@ public class DateToLongTransformer implements Transformer { */ @Override public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { - return null; + if (value instanceof Long) { + return new Date((Long) value); + } + + return value; } } diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java new file mode 100644 index 0000000..0f5266f --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTimeTransformer.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2022 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl.time.java8; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.Transformer; +import com.realtimetech.opack.value.OpackArray; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.time.LocalDateTime; + +public class LocalDateTimeTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ + @Override + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value instanceof LocalDateTime) { + LocalDateTime localDateTime = (LocalDateTime) value; + + return OpackArray.createWithArrayObject( + new int[]{ + localDateTime.getYear(), + localDateTime.getMonthValue(), + localDateTime.getDayOfMonth(), + localDateTime.getHour(), + localDateTime.getMinute(), + localDateTime.getSecond(), + localDateTime.getNano() + } + ); + } + + return value; + } + + /** + * Deserialize opack value. + * + * @param opacker the opacker + * @param goalType the goal type to deserialize + * @param value the opack value to be deserialized + * @return deserialized value + * @throws DeserializeException if a problem occurs during deserializing + */ + @Override + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { + if (value instanceof OpackArray) { + OpackArray opackArray = (OpackArray) value; + + if (opackArray.length() == 7) { + return LocalDateTime.of( + opackArray.get(0), + opackArray.get(1), + opackArray.get(2), + opackArray.get(3), + opackArray.get(4), + opackArray.get(5), + opackArray.get(6) + ); + } + } + + return value; + } +} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java new file mode 100644 index 0000000..735124a --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalDateTransformer.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2022 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl.time.java8; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.Transformer; +import com.realtimetech.opack.value.OpackArray; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.time.LocalDate; + +public class LocalDateTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ + @Override + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value instanceof LocalDate) { + LocalDate localDate = (LocalDate) value; + + return OpackArray.createWithArrayObject( + new int[]{ + localDate.getYear(), + localDate.getMonthValue(), + localDate.getDayOfMonth() + } + ); + } + + return value; + } + + /** + * Deserialize opack value. + * + * @param opacker the opacker + * @param goalType the goal type to deserialize + * @param value the opack value to be deserialized + * @return deserialized value + * @throws DeserializeException if a problem occurs during deserializing + */ + @Override + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { + if (value instanceof OpackArray) { + OpackArray opackArray = (OpackArray) value; + + if (opackArray.length() == 3) { + return LocalDate.of( + opackArray.get(0), + opackArray.get(1), + opackArray.get(2) + ); + } + } + + return value; + } +} diff --git a/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java new file mode 100644 index 0000000..7bbd127 --- /dev/null +++ b/src/main/java/com/realtimetech/opack/transformer/impl/time/java8/LocalTimeTransformer.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2022 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.transformer.impl.time.java8; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.transformer.Transformer; +import com.realtimetech.opack.value.OpackArray; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.time.LocalTime; + +public class LocalTimeTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ + @Override + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { + if (value instanceof LocalTime) { + LocalTime localTime = (LocalTime) value; + + return OpackArray.createWithArrayObject( + new int[]{ + localTime.getHour(), + localTime.getMinute(), + localTime.getSecond(), + localTime.getNano() + } + ); + } + + return value; + } + + /** + * Deserialize opack value. + * + * @param opacker the opacker + * @param goalType the goal type to deserialize + * @param value the opack value to be deserialized + * @return deserialized value + * @throws DeserializeException if a problem occurs during deserializing + */ + @Override + public @Nullable Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, @Nullable Object value) throws DeserializeException { + if (value instanceof OpackArray) { + OpackArray opackArray = (OpackArray) value; + + if (opackArray.length() == 4) { + return LocalTime.of( + opackArray.get(0), + opackArray.get(1), + opackArray.get(2), + opackArray.get(3) + ); + } + } + + return value; + } +} diff --git a/src/main/java/com/realtimetech/opack/util/OpackArrayConverter.java b/src/main/java/com/realtimetech/opack/util/OpackArrayConverter.java index 5d96e3f..c6be3ae 100644 --- a/src/main/java/com/realtimetech/opack/util/OpackArrayConverter.java +++ b/src/main/java/com/realtimetech/opack/util/OpackArrayConverter.java @@ -29,7 +29,6 @@ import java.lang.reflect.Array; import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.List; public class OpackArrayConverter { diff --git a/src/main/java/com/realtimetech/opack/value/OpackArray.java b/src/main/java/com/realtimetech/opack/value/OpackArray.java index 8cc2651..5573793 100644 --- a/src/main/java/com/realtimetech/opack/value/OpackArray.java +++ b/src/main/java/com/realtimetech/opack/value/OpackArray.java @@ -22,11 +22,14 @@ package com.realtimetech.opack.value; -import com.realtimetech.opack.util.structure.NativeList; import com.realtimetech.opack.util.ReflectionUtil; +import com.realtimetech.opack.util.structure.NativeList; import org.jetbrains.annotations.NotNull; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; public final class OpackArray extends AbstractOpackValue> { /** diff --git a/src/test/java/com/realtimetech/opack/test/OpackAssert.java b/src/test/java/com/realtimetech/opack/test/OpackAssert.java index e862c40..50d9107 100644 --- a/src/test/java/com/realtimetech/opack/test/OpackAssert.java +++ b/src/test/java/com/realtimetech/opack/test/OpackAssert.java @@ -25,9 +25,13 @@ import com.realtimetech.opack.annotation.Ignore; import com.realtimetech.opack.value.OpackValue; +import java.io.File; import java.lang.reflect.Array; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import java.nio.file.Path; +import java.util.Calendar; +import java.util.Date; import java.util.List; import java.util.Map; @@ -156,6 +160,11 @@ static void assertSingleValue(Object originalObject, Object targetObject) throws if (!originalObject.equals(targetObject)) { OpackAssert.throwException(originalObject, targetObject); } + } else if (originalObject instanceof File && targetObject instanceof File || + originalObject instanceof Path && targetObject instanceof Path || + originalObject instanceof Date && targetObject instanceof Date || + originalObject instanceof Calendar && targetObject instanceof Calendar) { + OpackAssert.assertEquals(originalObject.toString(), targetObject.toString()); } else { OpackAssert.assertObject(originalObject, targetObject); } diff --git a/src/test/java/com/realtimetech/opack/test/opacker/array/PrimitiveArrayTest.java b/src/test/java/com/realtimetech/opack/test/opacker/array/PrimitiveArrayTest.java index 9735969..7d69703 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/array/PrimitiveArrayTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/array/PrimitiveArrayTest.java @@ -52,42 +52,42 @@ public PrimitiveArrayClass() { int length = RANDOM.nextInt(1024) + 512; this.booleanArrayValue = new boolean[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.booleanArrayValue[index] = RANDOM.nextBoolean(); } this.byteArrayValue = new byte[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.byteArrayValue[index] = (byte) RANDOM.nextInt(); } this.charArrayValue = new char[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.charArrayValue[index] = (char) RANDOM.nextInt(); } this.shortArrayValue = new short[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.shortArrayValue[index] = (short) RANDOM.nextInt(); } this.intArrayValue = new int[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.intArrayValue[index] = RANDOM.nextInt(); } this.floatArrayValue = new float[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.floatArrayValue[index] = RANDOM.nextFloat(); } this.doubleArrayValue = new double[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.doubleArrayValue[index] = RANDOM.nextDouble(); } this.longArrayValue = new long[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.longArrayValue[index] = RANDOM.nextLong(); } } diff --git a/src/test/java/com/realtimetech/opack/test/opacker/array/WrapperArrayTest.java b/src/test/java/com/realtimetech/opack/test/opacker/array/WrapperArrayTest.java index e180b94..7fef61a 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/array/WrapperArrayTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/array/WrapperArrayTest.java @@ -51,49 +51,49 @@ public WrapperArrayClass() { int length = RANDOM.nextInt(1024) + 512; this.booleanArrayValue = new Boolean[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.booleanArrayValue[index] = RANDOM.nextBoolean(); } this.booleanArrayValue[length / 2] = null; this.byteArrayValue = new Byte[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.byteArrayValue[index] = (byte) RANDOM.nextInt(); } this.byteArrayValue[length / 2] = null; this.charArrayValue = new Character[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.charArrayValue[index] = (char) RANDOM.nextInt(); } this.charArrayValue[length / 2] = null; this.shortArrayValue = new Short[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.shortArrayValue[index] = (short) RANDOM.nextInt(); } this.shortArrayValue[length / 2] = null; this.intArrayValue = new Integer[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.intArrayValue[index] = RANDOM.nextInt(); } this.intArrayValue[length / 2] = null; this.floatArrayValue = new Float[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.floatArrayValue[index] = RANDOM.nextFloat(); } this.floatArrayValue[length / 2] = null; this.doubleArrayValue = new Double[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.doubleArrayValue[index] = RANDOM.nextDouble(); } this.doubleArrayValue[length / 2] = null; this.longArrayValue = new Long[length]; - for(int index = 0; index < length; index++){ + for (int index = 0; index < length; index++) { this.longArrayValue[index] = RANDOM.nextLong(); } this.longArrayValue[length / 2] = null; diff --git a/src/test/java/com/realtimetech/opack/test/opacker/map/MapTest.java b/src/test/java/com/realtimetech/opack/test/opacker/map/MapTest.java index 896ccea..ca431a0 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/map/MapTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/map/MapTest.java @@ -30,7 +30,8 @@ import com.realtimetech.opack.value.OpackValue; import org.junit.jupiter.api.Test; -import java.util.*; +import java.util.HashMap; +import java.util.Map; public class MapTest { public static class MapClass { diff --git a/src/test/java/com/realtimetech/opack/test/opacker/other/ComplexTest.java b/src/test/java/com/realtimetech/opack/test/opacker/other/ComplexTest.java index 669f49e..800da95 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/other/ComplexTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/other/ComplexTest.java @@ -26,14 +26,14 @@ import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; -import com.realtimetech.opack.test.opacker.single.ObjectTest; -import com.realtimetech.opack.test.opacker.single.PrimitiveTest; -import com.realtimetech.opack.test.opacker.single.StringTest; -import com.realtimetech.opack.test.opacker.single.WrapperTest; import com.realtimetech.opack.test.opacker.annotation.AnnotationTypeObjectTest; import com.realtimetech.opack.test.opacker.array.PrimitiveArrayTest; import com.realtimetech.opack.test.opacker.array.WrapperArrayTest; import com.realtimetech.opack.test.opacker.list.ListTest; +import com.realtimetech.opack.test.opacker.single.ObjectTest; +import com.realtimetech.opack.test.opacker.single.PrimitiveTest; +import com.realtimetech.opack.test.opacker.single.StringTest; +import com.realtimetech.opack.test.opacker.single.WrapperTest; import com.realtimetech.opack.test.opacker.transform.TransformClassTest; import com.realtimetech.opack.test.opacker.transform.TransformFieldTest; import com.realtimetech.opack.value.OpackValue; diff --git a/src/test/java/com/realtimetech/opack/test/opacker/single/PrimitiveTest.java b/src/test/java/com/realtimetech/opack/test/opacker/single/PrimitiveTest.java index 2d6bd8f..20e396c 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/single/PrimitiveTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/single/PrimitiveTest.java @@ -27,7 +27,6 @@ import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; import com.realtimetech.opack.value.OpackValue; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.Random; diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java index 139e1d0..1823fac 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformClassTest.java @@ -23,8 +23,8 @@ package com.realtimetech.opack.test.opacker.transform; import com.realtimetech.opack.Opacker; -import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.annotation.Transform; +import com.realtimetech.opack.annotation.Type; import com.realtimetech.opack.exception.DeserializeException; import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.test.OpackAssert; @@ -32,6 +32,8 @@ import com.realtimetech.opack.util.ReflectionUtil; import com.realtimetech.opack.value.OpackObject; import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -41,7 +43,7 @@ public class TransformClassTest { public static class ClassTransformer implements Transformer { @Override - public Object serialize(Opacker opacker, Object value) throws SerializeException { + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { if (value instanceof ClassTransformInheritable) { ClassTransformInheritable classTransformInheritable = (ClassTransformInheritable) value; return new String(classTransformInheritable.bytes, StandardCharsets.UTF_8); @@ -55,7 +57,7 @@ public Object serialize(Opacker opacker, Object value) throws SerializeException } @Override - public Object deserialize(Opacker opacker, Class goalType, Object value) throws DeserializeException { + public Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, Object value) throws DeserializeException { if (value instanceof String && ClassTransformInheritable.class.isAssignableFrom(goalType)) { try { ClassTransformInheritable classTransformInheritable = (ClassTransformInheritable) ReflectionUtil.createInstanceUnsafe(goalType); diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java index 7486289..776650d 100644 --- a/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/TransformFieldTest.java @@ -29,14 +29,25 @@ import com.realtimetech.opack.test.OpackAssert; import com.realtimetech.opack.transformer.Transformer; import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; public class TransformFieldTest { public static class ByteToStringTransformer implements Transformer { + /** + * Serialize specific value to opack value. + * + * @param opacker the opacker + * @param originalType the original type + * @param value the value to be serialized + * @return opack value + * @throws SerializeException if a problem occurs during serializing + */ @Override - public Object serialize(Opacker opacker, Object value) throws SerializeException { + public @Nullable Object serialize(@NotNull Opacker opacker, @NotNull Class originalType, @Nullable Object value) throws SerializeException { if (value instanceof byte[]) { return new String(((byte[]) value), StandardCharsets.UTF_8); } @@ -45,7 +56,7 @@ public Object serialize(Opacker opacker, Object value) throws SerializeException } @Override - public Object deserialize(Opacker opacker, Class goalType, Object value) throws DeserializeException { + public Object deserialize(@NotNull Opacker opacker, @NotNull Class goalType, Object value) throws DeserializeException { if (value instanceof String && goalType == byte[].class) { return ((String) value).getBytes(StandardCharsets.UTF_8); } diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/TypeWrapTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/TypeWrapTransformTest.java new file mode 100644 index 0000000..0395746 --- /dev/null +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/TypeWrapTransformTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2023 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.test.opacker.transform; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.annotation.Transform; +import com.realtimetech.opack.annotation.Type; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.test.OpackAssert; +import com.realtimetech.opack.transformer.impl.TypeWrapTransformer; +import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.util.LinkedList; +import java.util.List; + +public class TypeWrapTransformTest { + public static class TypeWrapTransformClass { + @Transform(transformer = TypeWrapTransformer.class) + private @NotNull List listWithTransformer; + + @Type(LinkedList.class) + private @NotNull List listWithAnnotation; + + public TypeWrapTransformClass() { + this.listWithTransformer = new LinkedList<>(); + this.listWithAnnotation = new LinkedList<>(); + } + + private void addItemsRandomly(@NotNull List list) { + for (int index = 0; index < 32; index++) { + list.add(Math.random()); + } + } + } + + @Test + public void test() throws SerializeException, DeserializeException, OpackAssert.AssertException { + Opacker opacker = new Opacker.Builder().create(); + TypeWrapTransformClass originalObject = new TypeWrapTransformClass(); + + OpackValue serialized = opacker.serialize(originalObject); + TypeWrapTransformClass deserialized = opacker.deserialize(TypeWrapTransformClass.class, serialized); + + OpackAssert.assertEquals(originalObject, deserialized); + } +} diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java new file mode 100644 index 0000000..12a8fb3 --- /dev/null +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/file/FileTransformTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2023 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.test.opacker.transform.file; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.test.OpackAssert; +import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.io.File; + +public class FileTransformTest { + public static class FileTransformClass { + private @NotNull File file; + + public FileTransformClass() { + this.file = new File("src/test/java/com/realtimetech/opack/test/opacker/transform/FileTransformTest.java"); + } + } + + @Test + public void test() throws SerializeException, DeserializeException, OpackAssert.AssertException { + Opacker opacker = new Opacker.Builder().create(); + FileTransformClass originalObject = new FileTransformClass(); + + OpackValue serialized = opacker.serialize(originalObject); + FileTransformClass deserialized = opacker.deserialize(FileTransformClass.class, serialized); + + OpackAssert.assertEquals(originalObject, deserialized); + } +} diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java new file mode 100644 index 0000000..2df0761 --- /dev/null +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/path/PathTransformTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2023 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.test.opacker.transform.path; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.test.OpackAssert; +import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; + +public class PathTransformTest { + public static class PathTransformClass { + private @NotNull Path path; + + public PathTransformClass() { + this.path = Path.of("src/test/java/com/realtimetech/opack/test/opacker/transform/FileTransformTest.java"); + } + } + + @Test + public void test() throws SerializeException, DeserializeException, OpackAssert.AssertException { + Opacker opacker = new Opacker.Builder().create(); + PathTransformClass originalObject = new PathTransformClass(); + + OpackValue serialized = opacker.serialize(originalObject); + PathTransformClass deserialized = opacker.deserialize(PathTransformClass.class, serialized); + + OpackAssert.assertEquals(originalObject, deserialized); + } +} diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java new file mode 100644 index 0000000..fbc17db --- /dev/null +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/Java8TimeTransformTest.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2023 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.test.opacker.transform.time; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.test.OpackAssert; +import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; + +public class Java8TimeTransformTest { + public static class Java8TimeTransformClass { + private @NotNull LocalDate localDate; + private @NotNull LocalTime localTime; + private @NotNull LocalDateTime localDateTime; + + public Java8TimeTransformClass() { + this.localDate = LocalDate.now(); + this.localTime = LocalTime.now(); + this.localDateTime = LocalDateTime.now(); + } + } + + @Test + public void test() throws SerializeException, DeserializeException, OpackAssert.AssertException { + Opacker opacker = new Opacker.Builder().create(); + Java8TimeTransformClass originalObject = new Java8TimeTransformClass(); + + OpackValue serialized = opacker.serialize(originalObject); + Java8TimeTransformClass deserialized = opacker.deserialize(Java8TimeTransformClass.class, serialized); + + OpackAssert.assertEquals(originalObject, deserialized); + } +} diff --git a/src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java new file mode 100644 index 0000000..03c3905 --- /dev/null +++ b/src/test/java/com/realtimetech/opack/test/opacker/transform/time/TimeTransformTest.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 REALTIMETECH All Rights Reserved + * + * Licensed either under the Apache License, Version 2.0, or (at your option) + * under the terms of the GNU General Public License as published by + * the Free Software Foundation (subject to the "Classpath" exception), + * either version 2, or any later version (collectively, 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 + * http://www.gnu.org/licenses/ + * http://www.gnu.org/software/classpath/license.html + * + * or as provided in the LICENSE file that accompanied this code. + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.realtimetech.opack.test.opacker.transform.time; + +import com.realtimetech.opack.Opacker; +import com.realtimetech.opack.exception.DeserializeException; +import com.realtimetech.opack.exception.SerializeException; +import com.realtimetech.opack.test.OpackAssert; +import com.realtimetech.opack.value.OpackValue; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.Test; + +import java.util.Calendar; +import java.util.Date; + +public class TimeTransformTest { + public static class TimeTransformClass { + private @NotNull Date date; + private @NotNull Calendar calendar; + + public TimeTransformClass() { + this.date = new Date(); + + this.calendar = Calendar.getInstance(); + this.calendar.setTime(new Date()); + } + } + + @Test + public void test() throws SerializeException, DeserializeException, OpackAssert.AssertException { + Opacker opacker = new Opacker.Builder().create(); + TimeTransformClass originalObject = new TimeTransformClass(); + + OpackValue serialized = opacker.serialize(originalObject); + TimeTransformClass deserialized = opacker.deserialize(TimeTransformClass.class, serialized); + + OpackAssert.assertEquals(originalObject, deserialized); + } +} diff --git a/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java b/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java index 674d6d0..3306566 100644 --- a/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java +++ b/src/test/java/com/realtimetech/opack/test/performance/JacksonPerformanceTest.java @@ -32,12 +32,8 @@ import com.realtimetech.opack.exception.SerializeException; import com.realtimetech.opack.value.OpackValue; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import java.nio.file.Files; -import java.nio.file.Path; - public class JacksonPerformanceTest { @Test public void jackson_bytes() throws SerializeException, EncodeException, DecodeException { From aab9745ba387612d393a355286e2bde1e395a0a2 Mon Sep 17 00:00:00 2001 From: devjeonghwan Date: Mon, 21 Aug 2023 05:03:16 +0900 Subject: [PATCH 2/2] Add assertion java8 time object to `OpackAssert` --- .../java/com/realtimetech/opack/test/OpackAssert.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/realtimetech/opack/test/OpackAssert.java b/src/test/java/com/realtimetech/opack/test/OpackAssert.java index 50d9107..4e6fd4d 100644 --- a/src/test/java/com/realtimetech/opack/test/OpackAssert.java +++ b/src/test/java/com/realtimetech/opack/test/OpackAssert.java @@ -30,6 +30,9 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.file.Path; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.LocalTime; import java.util.Calendar; import java.util.Date; import java.util.List; @@ -163,7 +166,10 @@ static void assertSingleValue(Object originalObject, Object targetObject) throws } else if (originalObject instanceof File && targetObject instanceof File || originalObject instanceof Path && targetObject instanceof Path || originalObject instanceof Date && targetObject instanceof Date || - originalObject instanceof Calendar && targetObject instanceof Calendar) { + originalObject instanceof Calendar && targetObject instanceof Calendar || + originalObject instanceof LocalDate && targetObject instanceof LocalDate || + originalObject instanceof LocalTime && targetObject instanceof LocalTime || + originalObject instanceof LocalDateTime && targetObject instanceof LocalDateTime) { OpackAssert.assertEquals(originalObject.toString(), targetObject.toString()); } else { OpackAssert.assertObject(originalObject, targetObject);