TINKERPOP-1676 Cleaned up serialization with detached

The DetachedUtil was just there to test out if the performance changes would 
help and it did it's job nicely, but it was kinda ugly and hung some methods 
out there in weird way. Cleaned that up with some builder pattern on the 
detached classes themselves.


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

Branch: refs/heads/TINKERPOP-1681
Commit: f32d725a080d39ef7bf93c68d8080939f622cb37
Parents: 049c979
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu May 25 14:03:29 2017 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu May 25 14:52:30 2017 -0400

----------------------------------------------------------------------
 .../io/graphson/GraphSONSerializersV2d0.java    | 49 +++++++-------
 .../structure/util/detached/DetachedEdge.java   | 44 ++++++++++--
 .../util/detached/DetachedElement.java          | 11 ---
 .../structure/util/detached/DetachedUtil.java   | 70 --------------------
 .../structure/util/detached/DetachedVertex.java | 38 ++++++++++-
 .../util/detached/DetachedVertexProperty.java   | 41 +++++++++++-
 6 files changed, 136 insertions(+), 117 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f32d725a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java
index 73bedbb..8d19e4f 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java
@@ -37,7 +37,6 @@ import 
org.apache.tinkerpop.gremlin.structure.util.Comparators;
 import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge;
 import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedFactory;
 import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedProperty;
-import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedUtil;
 import org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex;
 import 
org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertexProperty;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
@@ -430,26 +429,26 @@ class GraphSONSerializersV2d0 {
         }
 
         public Vertex deserialize(final JsonParser jsonParser, final 
DeserializationContext deserializationContext) throws IOException, 
JsonProcessingException {
-            final DetachedVertex v = DetachedUtil.newDetachedVertex();
+            final DetachedVertex.Builder v = DetachedVertex.build();
             while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                 if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setId(v, 
deserializationContext.readValue(jsonParser, Object.class));
+                    v.setId(deserializationContext.readValue(jsonParser, 
Object.class));
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setLabel(v, jsonParser.getText());
+                    v.setLabel(jsonParser.getText());
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
                     jsonParser.nextToken();
                     while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                         jsonParser.nextToken();
                         while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
-                            DetachedUtil.addProperty(v, 
(DetachedVertexProperty) deserializationContext.readValue(jsonParser, 
VertexProperty.class));
+                            v.addProperty((DetachedVertexProperty) 
deserializationContext.readValue(jsonParser, VertexProperty.class));
                         }
                     }
                 }
             }
 
-            return v;
+            return v.create();
         }
 
         @Override
@@ -466,41 +465,41 @@ class GraphSONSerializersV2d0 {
 
         @Override
         public Edge deserialize(final JsonParser jsonParser, final 
DeserializationContext deserializationContext) throws IOException, 
JsonProcessingException {
-            final DetachedEdge e = DetachedUtil.newDetachedEdge();
-            final DetachedVertex inV = DetachedUtil.newDetachedVertex();
-            final DetachedVertex outV = DetachedUtil.newDetachedVertex();
+            final DetachedEdge.Builder e = DetachedEdge.build();
+            final DetachedVertex.Builder inV = DetachedVertex.build();
+            final DetachedVertex.Builder outV = DetachedVertex.build();
             while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                 if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setId(e, 
deserializationContext.readValue(jsonParser, Object.class));
+                    e.setId(deserializationContext.readValue(jsonParser, 
Object.class));
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setLabel(e, jsonParser.getText());
+                    e.setLabel(jsonParser.getText());
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.OUT)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setId(outV, 
deserializationContext.readValue(jsonParser, Object.class));
+                    outV.setId(deserializationContext.readValue(jsonParser, 
Object.class));
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.OUT_LABEL)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setLabel(outV, jsonParser.getText());
+                    outV.setLabel(jsonParser.getText());
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.IN)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setId(inV, 
deserializationContext.readValue(jsonParser, Object.class));
+                    inV.setId(deserializationContext.readValue(jsonParser, 
Object.class));
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.IN_LABEL)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setLabel(inV,jsonParser.getText());
+                    inV.setLabel(jsonParser.getText());
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
                     jsonParser.nextToken();
                     while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                         jsonParser.nextToken();
-                        DetachedUtil.addProperty(e, (DetachedProperty) 
deserializationContext.readValue(jsonParser, Property.class));
+                        
e.addProperty(deserializationContext.readValue(jsonParser, Property.class));
                     }
                 }
             }
 
-            DetachedUtil.setInV(e, inV);
-            DetachedUtil.setOutV(e, outV);
+            e.setInV(inV.create());
+            e.setOutV(outV.create());
 
-            return e;
+            return e.create();
         }
 
         @Override
@@ -579,27 +578,27 @@ class GraphSONSerializersV2d0 {
 
         @Override
         public VertexProperty deserialize(final JsonParser jsonParser, final 
DeserializationContext deserializationContext) throws IOException, 
JsonProcessingException {
-            final DetachedVertexProperty vp = 
DetachedUtil.newDetachedVertexProperty();
+            final DetachedVertexProperty.Builder vp = 
DetachedVertexProperty.build();
 
             Map<String, Object> properties;
             while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                 if (jsonParser.getCurrentName().equals(GraphSONTokens.ID)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setId(vp, 
deserializationContext.readValue(jsonParser, Object.class));
+                    vp.setId(deserializationContext.readValue(jsonParser, 
Object.class));
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.LABEL)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setLabel(vp, jsonParser.getText());
+                    vp.setLabel(jsonParser.getText());
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.VALUE)) {
                     jsonParser.nextToken();
-                    DetachedUtil.setValue(vp, 
deserializationContext.readValue(jsonParser, Object.class));
+                    vp.setValue(deserializationContext.readValue(jsonParser, 
Object.class));
                 } else if 
(jsonParser.getCurrentName().equals(GraphSONTokens.PROPERTIES)) {
                     jsonParser.nextToken();
                     properties = deserializationContext.readValue(jsonParser, 
propertiesType);
-                    properties.entrySet().iterator().forEachRemaining(kv -> 
DetachedUtil.addProperty(vp, new DetachedProperty(kv.getKey(), kv.getValue())));
+                    properties.entrySet().iterator().forEachRemaining(kv -> 
vp.addProperty(new DetachedProperty(kv.getKey(), kv.getValue())));
                 }
             }
 
-            return vp;
+            return vp.create();
         }
 
         @Override

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f32d725a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
index 40b6e34..367dbc9 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedEdge.java
@@ -51,7 +51,7 @@ public class DetachedEdge extends DetachedElement<Edge> 
implements Edge {
     private DetachedVertex outVertex;
     private DetachedVertex inVertex;
 
-    DetachedEdge() {}
+    private DetachedEdge() {}
 
     protected DetachedEdge(final Edge edge, final boolean withProperties) {
         super(edge);
@@ -155,11 +155,45 @@ public class DetachedEdge extends DetachedElement<Edge> 
implements Edge {
         this.properties.put(p.key(), Collections.singletonList(p));
     }
 
-    void internalSetOutV(final DetachedVertex v) {
-        outVertex = v;
+    /**
+     * Provides a way to construct an immutable {@link DetachedEdge}.
+     */
+    public static DetachedEdge.Builder build() {
+        return new Builder(new DetachedEdge());
     }
 
-    void internalSetInV(final DetachedVertex v) {
-        inVertex = v;
+    public static class Builder {
+        private DetachedEdge e;
+
+        private Builder(final DetachedEdge e) {
+            this.e = e;
+        }
+
+        public Builder addProperty(final Property p) {
+            e.internalAddProperty(p);
+            return this;
+        }
+
+        public Builder setId(final Object id) {
+            e.id = id;
+            return this;
+        }
+
+        public Builder setLabel(final String label) {
+            e.label = label;
+            return this;
+        }
+
+        public void setOutV(final DetachedVertex v) {
+            e.outVertex = v;
+        }
+
+        public void setInV(final DetachedVertex v) {
+            e.inVertex = v;
+        }
+
+        public DetachedEdge create() {
+            return e;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f32d725a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
index 4e40303..8826bfd 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedElement.java
@@ -25,12 +25,9 @@ import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.util.Attachable;
 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
 import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
-import org.apache.tinkerpop.gremlin.util.function.HashMapSupplier;
 
 import java.io.Serializable;
-import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -111,12 +108,4 @@ public abstract class DetachedElement<E> implements 
Element, Serializable, Attac
     }
 
     abstract void internalAddProperty(final Property p);
-
-    void internalSetId(final Object id) {
-        this.id = id;
-    }
-
-    void inernalSetLabel(final String label) {
-        this.label = label;
-    }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f32d725a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedUtil.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedUtil.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedUtil.java
deleted file mode 100644
index f2c658d..0000000
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedUtil.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.tinkerpop.gremlin.structure.util.detached;
-
-/**
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public final class DetachedUtil {
-
-    private DetachedUtil() {}
-
-    /**
-     * Provides a way to mutate something that is "detached". This method is 
really for internal usage as there
-     * typically is not need for application developers to mutate a "detached" 
element.
-     */
-    public static void addProperty(final DetachedElement e, final 
DetachedProperty p) {
-        e.internalAddProperty(p);
-    }
-    public static void addProperty(final DetachedVertex v, final 
DetachedVertexProperty vp) {
-        v.internalAddProperty(vp);
-    }
-
-    public static void setId(final DetachedElement e, final Object id) {
-        e.internalSetId(id);
-    }
-
-    public static void setLabel(final DetachedElement e, final String label) {
-        e.inernalSetLabel(label);
-    }
-
-    public static void setValue(final DetachedVertexProperty vp, final Object 
value) {
-        vp.internalSetValue(value);
-    }
-
-    public static DetachedVertex newDetachedVertex() {
-        return new DetachedVertex();
-    }
-
-    public static DetachedVertexProperty newDetachedVertexProperty() {
-        return new DetachedVertexProperty();
-    }
-
-    public static DetachedEdge newDetachedEdge() {
-        return new DetachedEdge();
-    }
-
-    public static void setInV(final DetachedEdge e, final DetachedVertex v) {
-        e.internalSetInV(v);
-    }
-
-    public static void setOutV(final DetachedEdge e, final DetachedVertex v) {
-        e.internalSetOutV(v);
-    }
-}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f32d725a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
index 15e956b..3f0b670 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertex.java
@@ -25,7 +25,6 @@ import org.apache.tinkerpop.gremlin.structure.Graph;
 import org.apache.tinkerpop.gremlin.structure.Property;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
-import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
 import org.apache.tinkerpop.gremlin.structure.util.StringFactory;
 import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
 
@@ -35,7 +34,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.stream.Collectors;
 
 /**
  * Represents a {@link Vertex} that is disconnected from a {@link Graph}.  
"Disconnection" can mean detachment from
@@ -55,7 +53,7 @@ public class DetachedVertex extends DetachedElement<Vertex> 
implements Vertex {
     private static final String VALUE = "value";
     private static final String PROPERTIES = "properties";
 
-    DetachedVertex() {}
+    private DetachedVertex() {}
 
     protected DetachedVertex(final Vertex vertex, final boolean 
withProperties) {
         super(vertex);
@@ -154,4 +152,38 @@ public class DetachedVertex extends 
DetachedElement<Vertex> implements Vertex {
 
         this.properties.get(p.key()).add(p);
     }
+
+    /**
+     * Provides a way to construct an immutable {@link DetachedVertex}.
+     */
+    public static DetachedVertex.Builder build() {
+        return new Builder(new DetachedVertex());
+    }
+
+    public static class Builder {
+        private DetachedVertex v;
+
+        private Builder(final DetachedVertex v) {
+            this.v = v;
+        }
+
+        public Builder addProperty(final DetachedVertexProperty vp) {
+            v.internalAddProperty(vp);
+            return this;
+        }
+
+        public Builder setId(final Object id) {
+            v.id = id;
+            return this;
+        }
+
+        public Builder setLabel(final String label) {
+            v.label = label;
+            return this;
+        }
+
+        public DetachedVertex create() {
+            return v;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/f32d725a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertexProperty.java
----------------------------------------------------------------------
diff --git 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertexProperty.java
 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertexProperty.java
index f682e04..b64be7c 100644
--- 
a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertexProperty.java
+++ 
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/util/detached/DetachedVertexProperty.java
@@ -39,7 +39,7 @@ public class DetachedVertexProperty<V> extends 
DetachedElement<VertexProperty<V>
     protected V value;
     protected transient DetachedVertex vertex;
 
-    DetachedVertexProperty() {}
+    private DetachedVertexProperty() {}
 
     protected DetachedVertexProperty(final VertexProperty<V> vertexProperty, 
final boolean withProperties) {
         super(vertexProperty);
@@ -132,7 +132,42 @@ public class DetachedVertexProperty<V> extends 
DetachedElement<VertexProperty<V>
         this.properties.put(p.key(), Collections.singletonList(p));
     }
 
-    void internalSetValue(final V value) {
-        this.value = value;
+    /**
+     * Provides a way to construct an immutable {@link DetachedEdge}.
+     */
+    public static DetachedVertexProperty.Builder build() {
+        return new Builder(new DetachedVertexProperty());
+    }
+
+    public static class Builder {
+        private DetachedVertexProperty vp;
+
+        private Builder(final DetachedVertexProperty e) {
+            this.vp = e;
+        }
+
+        public Builder addProperty(final Property p) {
+            vp.internalAddProperty(p);
+            return this;
+        }
+
+        public Builder setId(final Object id) {
+            vp.id = id;
+            return this;
+        }
+
+        public Builder setLabel(final String label) {
+            vp.label = label;
+            return this;
+        }
+
+        public Builder setValue(final Object value) {
+            vp.value = value;
+            return this;
+        }
+
+        public DetachedVertexProperty create() {
+            return vp;
+        }
     }
 }

Reply via email to