2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.dbgen;
23 import java.io.FileInputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.util.Optional;
29 import java.util.function.Consumer;
31 import org.apache.tinkerpop.gremlin.structure.Graph;
32 import org.apache.tinkerpop.gremlin.structure.io.Io;
33 import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
34 import org.apache.tinkerpop.gremlin.structure.io.Mapper;
35 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONMapper;
36 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONReader;
37 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONVersion;
38 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
41 * Constructs GraphSON IO implementations given a {@link Graph} and {@link IoRegistry}. Implementers of the
43 * interfaces should see the {@link GraphSONMapper} for information on the expectations for the {@link IoRegistry}.
45 * @author Stephen Mallette (http://stephen.genoprime.com)
47 public final class GraphSONPartialIO
48 implements Io<GraphSONPartialReader.Builder, GraphSONWriter.Builder, GraphSONMapper.Builder> {
49 private final IoRegistry registry;
50 private final Graph graph;
51 private final Optional<Consumer<Mapper.Builder>> onMapper;
52 private final GraphSONVersion version;
54 private GraphSONPartialIO(final Builder builder) {
55 this.registry = builder.registry;
56 this.graph = builder.graph;
57 this.onMapper = Optional.ofNullable(builder.onMapper);
58 this.version = builder.version;
65 public GraphSONPartialReader.Builder reader() {
66 return GraphSONPartialReader.build().mapper(mapper().create());
73 public GraphSONWriter.Builder writer() {
74 return GraphSONWriter.build().mapper(mapper().create());
81 public GraphSONMapper.Builder mapper() {
82 final GraphSONMapper.Builder builder = (null == this.registry) ? GraphSONMapper.build().version(version)
83 : GraphSONMapper.build().version(version).addRegistry(this.registry);
84 onMapper.ifPresent(c -> c.accept(builder));
92 public void writeGraph(final String file) throws IOException {
93 try (final OutputStream out = new FileOutputStream(file)) {
94 writer().create().writeGraph(out, graph);
102 public void readGraph(final String file) throws IOException {
103 try (final InputStream in = new FileInputStream(file)) {
104 reader().create().readGraph(in, graph);
109 * Create a new builder using the default version of GraphSON.
111 public static Io.Builder<GraphSONPartialIO> build() {
112 return build(GraphSONVersion.V1_0);
116 * Create a new builder using the specified version of GraphSON.
118 public static Io.Builder<GraphSONPartialIO> build(final GraphSONVersion version) {
119 return new Builder(version);
122 public final static class Builder implements Io.Builder<GraphSONPartialIO> {
124 private IoRegistry registry = null;
126 private Consumer<Mapper.Builder> onMapper = null;
127 private final GraphSONVersion version;
129 Builder(final GraphSONVersion version) {
130 this.version = version;
134 * @deprecated As of release 3.2.2, replaced by {@link #onMapper(Consumer)}.
138 public Io.Builder<GraphSONPartialIO> registry(final IoRegistry registry) {
139 this.registry = registry;
144 public Io.Builder<? extends Io> onMapper(final Consumer<Mapper.Builder> onMapper) {
145 this.onMapper = onMapper;
150 public Io.Builder<GraphSONPartialIO> graph(final Graph g) {
156 public GraphSONPartialIO create() {
158 throw new IllegalArgumentException("The graph argument was not specified");
159 return new GraphSONPartialIO(this);