JAMES-2578 Strengthen ArbitrarySerializable types

Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/dbee6f7e
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/dbee6f7e
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/dbee6f7e

Branch: refs/heads/master
Commit: dbee6f7e8a8487918a0f37ae08147180aed8c23c
Parents: 46ba928
Author: Gautier DI FOLCO <gdifo...@linagora.com>
Authored: Mon Oct 29 14:18:15 2018 +0100
Committer: Benoit Tellier <btell...@linagora.com>
Committed: Thu Nov 1 11:02:43 2018 +0700

----------------------------------------------------------------------
 .../apache/mailet/ArbitrarySerializable.java    | 16 +++++++-------
 .../java/org/apache/mailet/AttributeValue.java  |  4 ++--
 .../main/java/org/apache/mailet/Serializer.java | 22 +++++++++-----------
 .../org/apache/mailet/AttributeValueTest.java   | 14 ++++++-------
 4 files changed, 27 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/dbee6f7e/mailet/api/src/main/java/org/apache/mailet/ArbitrarySerializable.java
----------------------------------------------------------------------
diff --git 
a/mailet/api/src/main/java/org/apache/mailet/ArbitrarySerializable.java 
b/mailet/api/src/main/java/org/apache/mailet/ArbitrarySerializable.java
index 2bab770..d4ab5ad 100644
--- a/mailet/api/src/main/java/org/apache/mailet/ArbitrarySerializable.java
+++ b/mailet/api/src/main/java/org/apache/mailet/ArbitrarySerializable.java
@@ -21,17 +21,17 @@ package org.apache.mailet;
 
 import java.util.Optional;
 
-public interface ArbitrarySerializable {
-    public class Serializable {
+public interface ArbitrarySerializable<T> {
+    public class Serializable<U> {
         private final AttributeValue<?> value;
-        private final Class<? extends Factory> factory;
+        private final Class<? extends Factory<U>> factory;
 
-        public Serializable(AttributeValue<?> value, Class<? extends Factory> 
factory) {
+        public Serializable(AttributeValue<?> value, Class<? extends 
Factory<U>> factory) {
             this.value = value;
             this.factory = factory;
         }
 
-        public Class<? extends Factory> getFactory() {
+        public Class<? extends Factory<U>> getFactory() {
             return factory;
         }
 
@@ -40,9 +40,9 @@ public interface ArbitrarySerializable {
         }
     }
 
-    public interface Factory {
-        Optional<ArbitrarySerializable> deserialize(Serializable serializable);
+    public interface Factory<U> {
+        Optional<U> deserialize(Serializable<U> serializable);
     }
     
-    Serializable serialize();
+    Serializable<T> serialize();
 }

http://git-wip-us.apache.org/repos/asf/james-project/blob/dbee6f7e/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
----------------------------------------------------------------------
diff --git a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java 
b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
index fd997c4..4a548b3 100644
--- a/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
+++ b/mailet/api/src/main/java/org/apache/mailet/AttributeValue.java
@@ -69,8 +69,8 @@ public class AttributeValue<T> {
         return new AttributeValue<>(value, Serializer.DOUBLE_SERIALIZER);
     }
 
-    public static AttributeValue<ArbitrarySerializable> 
of(ArbitrarySerializable value) {
-        return new AttributeValue<>(value, 
Serializer.ARIBITRARY_SERIALIZABLE_SERIALIZER);
+    public static <T extends ArbitrarySerializable<T>> AttributeValue<T> of(T 
value) {
+        return new AttributeValue<>(value, new 
Serializer.ArbitrarySerializableSerializer<>());
     }
 
     public static AttributeValue<URL> of(URL value) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/dbee6f7e/mailet/api/src/main/java/org/apache/mailet/Serializer.java
----------------------------------------------------------------------
diff --git a/mailet/api/src/main/java/org/apache/mailet/Serializer.java 
b/mailet/api/src/main/java/org/apache/mailet/Serializer.java
index d71a01c..514b464 100644
--- a/mailet/api/src/main/java/org/apache/mailet/Serializer.java
+++ b/mailet/api/src/main/java/org/apache/mailet/Serializer.java
@@ -78,7 +78,7 @@ public interface Serializer<T> {
                     LONG_SERIALIZER,
                     FLOAT_SERIALIZER,
                     DOUBLE_SERIALIZER,
-                    ARIBITRARY_SERIALIZABLE_SERIALIZER,
+                    new Serializer.ArbitrarySerializableSerializer(),
                     URL_SERIALIZER,
                     new CollectionSerializer<>(),
                     new MapSerializer<>(),
@@ -259,12 +259,12 @@ public interface Serializer<T> {
 
     Serializer<Double> DOUBLE_SERIALIZER = new DoubleSerializer();
 
-    class ArbitrarySerializableSerializer implements 
Serializer<ArbitrarySerializable> {
+    class ArbitrarySerializableSerializer<T extends ArbitrarySerializable<T>> 
implements Serializer<T> {
         private static final Logger LOGGER = 
LoggerFactory.getLogger(ArbitrarySerializableSerializer.class);
 
         @Override
-        public JsonNode serialize(ArbitrarySerializable serializable) {
-            ArbitrarySerializable.Serializable serialized = 
serializable.serialize();
+        public JsonNode serialize(T serializable) {
+            ArbitrarySerializable.Serializable<T> serialized = 
serializable.serialize();
             ObjectNode serializedJson = JsonNodeFactory.instance.objectNode();
             serializedJson.put("factory", serialized.getFactory().getName());
             serializedJson.replace("value", serialized.getValue().toJson());
@@ -272,14 +272,14 @@ public interface Serializer<T> {
         }
 
         @Override
-        public Optional<ArbitrarySerializable> deserialize(JsonNode json) {
+        public Optional<T> deserialize(JsonNode json) {
             return Optional.of(json)
                     .filter(ObjectNode.class::isInstance)
                     .map(ObjectNode.class::cast)
                     .flatMap(this::instantiate);
         }
 
-        public Optional<ArbitrarySerializable> instantiate(ObjectNode fields) {
+        public Optional<T> instantiate(ObjectNode fields) {
             return Optional.ofNullable(fields.get("factory"))
                 .flatMap(serializer ->
                     Optional.ofNullable(fields.get("value"))
@@ -287,15 +287,15 @@ public interface Serializer<T> {
         }
 
         @SuppressWarnings("unchecked")
-        private Optional<ArbitrarySerializable> deserialize(String serializer, 
AttributeValue<?> value) {
+        private Optional<T> deserialize(String serializer, AttributeValue<?> 
value) {
             try {
                 Class<?> factoryClass = Class.forName(serializer);
                 if 
(ArbitrarySerializable.Factory.class.isAssignableFrom(factoryClass)) {
-                    ArbitrarySerializable.Factory factory = 
(ArbitrarySerializable.Factory) factoryClass.newInstance();
-                    return factory.deserialize(new 
ArbitrarySerializable.Serializable(value, 
(Class<ArbitrarySerializable.Factory>) factoryClass));
+                    ArbitrarySerializable.Factory<T> factory = 
(ArbitrarySerializable.Factory<T>) factoryClass.newInstance();
+                    return factory.deserialize(new 
ArbitrarySerializable.Serializable<>(value, 
(Class<ArbitrarySerializable.Factory<T>>) factoryClass));
                 }
             } catch (Exception e) {
-                LOGGER.error("Error while deserializing", e);
+                LOGGER.error("Error while deserializing using serializer {} 
and value {}", serializer, value, e);
             }
 
             return Optional.empty();
@@ -312,8 +312,6 @@ public interface Serializer<T> {
         }
     }
 
-    Serializer<ArbitrarySerializable> ARIBITRARY_SERIALIZABLE_SERIALIZER = new 
ArbitrarySerializableSerializer();
-
     class UrlSerializer implements Serializer<URL> {
         @Override
         public JsonNode serialize(URL object) {

http://git-wip-us.apache.org/repos/asf/james-project/blob/dbee6f7e/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
----------------------------------------------------------------------
diff --git a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java 
b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
index f741d05..45cfede 100644
--- a/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
+++ b/mailet/api/src/test/java/org/apache/mailet/AttributeValueTest.java
@@ -311,7 +311,7 @@ class AttributeValueTest {
     class QueueSerializableTest {
         @Test
         void queueSerializableShouldBeSerializedAndBack() {
-            AttributeValue<ArbitrarySerializable> expected = 
AttributeValue.of(new TestArbitrarySerializable(42));
+            AttributeValue<TestArbitrarySerializable> expected = 
AttributeValue.of(new TestArbitrarySerializable(42));
 
             JsonNode json = expected.toJson();
             AttributeValue<?> actual = AttributeValue.fromJson(json);
@@ -322,7 +322,7 @@ class AttributeValueTest {
 
         @Test
         void 
fromJsonStringShouldReturnQueueSerializableAttributeValueWhenQueueSerializable()
 throws Exception {
-            AttributeValue<ArbitrarySerializable> expected = 
AttributeValue.of(new TestArbitrarySerializable(42));
+            AttributeValue<TestArbitrarySerializable> expected = 
AttributeValue.of(new TestArbitrarySerializable(42));
 
             AttributeValue<?> actual = 
AttributeValue.fromJsonString("{\"serializer\":\"ArbitrarySerializableSerializer\",\"value\":{\"factory\":\"org.apache.mailet.AttributeValueTest$TestArbitrarySerializable$Factory\",\"value\":{\"serializer\":\"IntSerializer\",\"value\":42}}}");
 
@@ -515,10 +515,10 @@ class AttributeValueTest {
             .isThrownBy(() -> 
AttributeValue.fromJsonString("{\"serializer\":\"MapSerializer\"}"));
     }
 
-    private static class TestArbitrarySerializable implements 
ArbitrarySerializable {
-        public static class Factory implements ArbitrarySerializable.Factory {
+    private static class TestArbitrarySerializable implements 
ArbitrarySerializable<TestArbitrarySerializable> {
+        public static class Factory implements 
ArbitrarySerializable.Factory<TestArbitrarySerializable> {
             @Override
-            public Optional<ArbitrarySerializable> deserialize(Serializable 
serializable) {
+            public Optional<TestArbitrarySerializable> 
deserialize(Serializable<TestArbitrarySerializable> serializable) {
                 return Optional.of(serializable.getValue().value())
                         .filter(Integer.class::isInstance)
                         .map(Integer.class::cast)
@@ -533,8 +533,8 @@ class AttributeValueTest {
         }
 
         @Override
-        public Serializable serialize() {
-            return new Serializable(AttributeValue.of(value), Factory.class);
+        public Serializable<TestArbitrarySerializable> serialize() {
+            return new Serializable<>(AttributeValue.of(value), Factory.class);
         }
 
         @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org
For additional commands, e-mail: server-dev-h...@james.apache.org

Reply via email to