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=========================================================
20 package org.onap.aai.dbgen;
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;
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;
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}.
43 * @author Stephen Mallette (http://stephen.genoprime.com)
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;
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;
62 public GraphSONPartialReader.Builder reader() {
63 return GraphSONPartialReader.build().mapper(mapper().create());
70 public GraphSONWriter.Builder writer() {
71 return GraphSONWriter.build().mapper(mapper().create());
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));
89 public void writeGraph(final String file) throws IOException {
90 try (final OutputStream out = new FileOutputStream(file)) {
91 writer().create().writeGraph(out, graph);
99 public void readGraph(final String file) throws IOException {
100 try (final InputStream in = new FileInputStream(file)) {
101 reader().create().readGraph(in, graph);
106 * Create a new builder using the default version of GraphSON.
108 public static Io.Builder<GraphSONPartialIO> build() {
109 return build(GraphSONVersion.V1_0);
113 * Create a new builder using the specified version of GraphSON.
115 public static Io.Builder<GraphSONPartialIO> build(final GraphSONVersion version) {
116 return new Builder(version);
119 public final static class Builder implements Io.Builder<GraphSONPartialIO> {
121 private IoRegistry registry = null;
123 private Consumer<Mapper.Builder> onMapper = null;
124 private final GraphSONVersion version;
126 Builder(final GraphSONVersion version) {
127 this.version = version;
131 * @deprecated As of release 3.2.2, replaced by {@link #onMapper(Consumer)}.
135 public Io.Builder<GraphSONPartialIO> registry(final IoRegistry registry) {
136 this.registry = registry;
141 public Io.Builder<? extends Io> onMapper(final Consumer<Mapper.Builder> onMapper) {
142 this.onMapper = onMapper;
147 public Io.Builder<GraphSONPartialIO> graph(final Graph g) {
153 public GraphSONPartialIO create() {
154 if (null == graph) throw new IllegalArgumentException("The graph argument was not specified");
155 return new GraphSONPartialIO(this);