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.GraphSONVersion;
37 import org.apache.tinkerpop.gremlin.structure.io.graphson.GraphSONWriter;
40 * Constructs GraphSON IO implementations given a {@link Graph} and {@link IoRegistry}. Implementers of the
42 * interfaces should see the {@link GraphSONMapper} for information on the expectations for the {@link IoRegistry}.
44 * @author Stephen Mallette (http://stephen.genoprime.com)
46 public final class GraphSONPartialIO
47 implements Io<GraphSONPartialReader.Builder, GraphSONWriter.Builder, GraphSONMapper.Builder> {
48 private final IoRegistry registry;
49 private final Graph graph;
50 private final Optional<Consumer<Mapper.Builder>> onMapper;
51 private final GraphSONVersion version;
53 private GraphSONPartialIO(final Builder builder) {
54 this.registry = builder.registry;
55 this.graph = builder.graph;
56 this.onMapper = Optional.ofNullable(builder.onMapper);
57 this.version = builder.version;
64 public GraphSONPartialReader.Builder reader() {
65 return GraphSONPartialReader.build().mapper(mapper().create());
72 public GraphSONWriter.Builder writer() {
73 return GraphSONWriter.build().mapper(mapper().create());
80 public GraphSONMapper.Builder mapper() {
81 final GraphSONMapper.Builder builder = (null == this.registry) ? GraphSONMapper.build().version(version)
82 : GraphSONMapper.build().version(version).addRegistry(this.registry);
83 onMapper.ifPresent(c -> c.accept(builder));
91 public void writeGraph(final String file) throws IOException {
92 try (final OutputStream out = new FileOutputStream(file)) {
93 writer().create().writeGraph(out, graph);
101 public void readGraph(final String file) throws IOException {
102 try (final InputStream in = new FileInputStream(file)) {
103 reader().create().readGraph(in, graph);
108 * Create a new builder using the default version of GraphSON.
110 public static Io.Builder<GraphSONPartialIO> build() {
111 return build(GraphSONVersion.V1_0);
115 * Create a new builder using the specified version of GraphSON.
117 public static Io.Builder<GraphSONPartialIO> build(final GraphSONVersion version) {
118 return new Builder(version);
121 public static final class Builder implements Io.Builder<GraphSONPartialIO> {
123 private IoRegistry registry = null;
125 private Consumer<Mapper.Builder> onMapper = null;
126 private final GraphSONVersion version;
128 Builder(final GraphSONVersion version) {
129 this.version = version;
133 * @deprecated As of release 3.2.2, replaced by {@link #onMapper(Consumer)}.
137 public Io.Builder<GraphSONPartialIO> registry(final IoRegistry registry) {
138 this.registry = registry;
143 public Io.Builder<? extends Io> onMapper(final Consumer<Mapper.Builder> onMapper) {
144 this.onMapper = onMapper;
149 public Io.Builder<GraphSONPartialIO> graph(final Graph g) {
155 public GraphSONPartialIO create() {
157 throw new IllegalArgumentException("The graph argument was not specified");
158 return new GraphSONPartialIO(this);