Sync up the changes for v15
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / dbgen / GraphSONPartialIO.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.dbgen;
21
22 import org.apache.tinkerpop.gremlin.structure.Graph;
23 import org.apache.tinkerpop.gremlin.structure.io.Io;
24 import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
25 import org.apache.tinkerpop.gremlin.structure.io.Mapper;
26 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
27 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader;
28 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONVersion;
29 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
30
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.OutputStream;
36 import java.util.Optional;
37 import java.util.function.Consumer;
38
39 /**
40  * Constructs GraphSON IO implementations given a {@link Graph} and {@link IoRegistry}. Implementers of the {@link Graph}
41  * interfaces should see the {@link GraphSONMapper} for information on the expectations for the {@link IoRegistry}.
42  *
43  * @author Stephen Mallette (http://stephen.genoprime.com)
44  */
45 public final class GraphSONPartialIO implements Io<GraphSONPartialReader.Builder, GraphSONWriter.Builder, GraphSONMapper.Builder> {
46     private final IoRegistry registry;
47     private final Graph graph;
48     private final Optional<Consumer<Mapper.Builder>> onMapper;
49     private final GraphSONVersion version;
50
51     private GraphSONPartialIO(final Builder builder) {
52         this.registry = builder.registry;
53         this.graph = builder.graph;
54         this.onMapper = Optional.ofNullable(builder.onMapper);
55         this.version = builder.version;
56     }
57
58     /**
59      * {@inheritDoc}
60      */
61     @Override
62     public GraphSONPartialReader.Builder reader() {
63         return GraphSONPartialReader.build().mapper(mapper().create());
64     }
65
66     /**
67      * {@inheritDoc}
68      */
69     @Override
70     public GraphSONWriter.Builder writer() {
71         return GraphSONWriter.build().mapper(mapper().create());
72     }
73
74     /**
75      * {@inheritDoc}
76      */
77     @Override
78     public GraphSONMapper.Builder mapper() {
79         final GraphSONMapper.Builder builder = (null == this.registry) ?
80                 GraphSONMapper.build().version(version) : GraphSONMapper.build().version(version).addRegistry(this.registry);
81         onMapper.ifPresent(c -> c.accept(builder));
82         return builder;
83     }
84
85     /**
86      * {@inheritDoc}
87      */
88     @Override
89     public void writeGraph(final String file) throws IOException {
90         try (final OutputStream out = new FileOutputStream(file)) {
91             writer().create().writeGraph(out, graph);
92         }
93     }
94
95     /**
96      * {@inheritDoc}
97      */
98     @Override
99     public void readGraph(final String file) throws IOException {
100         try (final InputStream in = new FileInputStream(file)) {
101             reader().create().readGraph(in, graph);
102         }
103     }
104
105     /**
106      * Create a new builder using the default version of GraphSON.
107      */
108     public static Io.Builder<GraphSONPartialIO> build() {
109         return build(GraphSONVersion.V1_0);
110     }
111
112     /**
113      * Create a new builder using the specified version of GraphSON.
114      */
115     public static Io.Builder<GraphSONPartialIO> build(final GraphSONVersion version) {
116         return new Builder(version);
117     }
118
119     public final static class Builder implements Io.Builder<GraphSONPartialIO> {
120
121         private IoRegistry registry = null;
122         private Graph graph;
123         private Consumer<Mapper.Builder> onMapper = null;
124         private final GraphSONVersion version;
125
126         Builder(final GraphSONVersion version) {
127             this.version = version;
128         }
129
130         /**
131          * @deprecated As of release 3.2.2, replaced by {@link #onMapper(Consumer)}.
132          */
133         @Deprecated
134         @Override
135         public Io.Builder<GraphSONPartialIO> registry(final IoRegistry registry) {
136             this.registry = registry;
137             return this;
138         }
139
140         @Override
141         public Io.Builder<? extends Io> onMapper(final Consumer<Mapper.Builder> onMapper) {
142             this.onMapper = onMapper;
143             return this;
144         }
145
146         @Override
147         public Io.Builder<GraphSONPartialIO> graph(final Graph g) {
148             this.graph = g;
149             return this;
150         }
151
152         @Override
153         public GraphSONPartialIO create() {
154             if (null == graph) throw new IllegalArgumentException("The graph argument was not specified");
155             return new GraphSONPartialIO(this);
156         }
157     }
158 }