AAI-1523 Batch reformat aai-core
[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
21 package org.onap.aai.dbgen;
22
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;
30
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;
39
40 /**
41  * Constructs GraphSON IO implementations given a {@link Graph} and {@link IoRegistry}. Implementers of the
42  * {@link Graph}
43  * interfaces should see the {@link GraphSONMapper} for information on the expectations for the {@link IoRegistry}.
44  *
45  * @author Stephen Mallette (http://stephen.genoprime.com)
46  */
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;
53
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;
59     }
60
61     /**
62      * {@inheritDoc}
63      */
64     @Override
65     public GraphSONPartialReader.Builder reader() {
66         return GraphSONPartialReader.build().mapper(mapper().create());
67     }
68
69     /**
70      * {@inheritDoc}
71      */
72     @Override
73     public GraphSONWriter.Builder writer() {
74         return GraphSONWriter.build().mapper(mapper().create());
75     }
76
77     /**
78      * {@inheritDoc}
79      */
80     @Override
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));
85         return builder;
86     }
87
88     /**
89      * {@inheritDoc}
90      */
91     @Override
92     public void writeGraph(final String file) throws IOException {
93         try (final OutputStream out = new FileOutputStream(file)) {
94             writer().create().writeGraph(out, graph);
95         }
96     }
97
98     /**
99      * {@inheritDoc}
100      */
101     @Override
102     public void readGraph(final String file) throws IOException {
103         try (final InputStream in = new FileInputStream(file)) {
104             reader().create().readGraph(in, graph);
105         }
106     }
107
108     /**
109      * Create a new builder using the default version of GraphSON.
110      */
111     public static Io.Builder<GraphSONPartialIO> build() {
112         return build(GraphSONVersion.V1_0);
113     }
114
115     /**
116      * Create a new builder using the specified version of GraphSON.
117      */
118     public static Io.Builder<GraphSONPartialIO> build(final GraphSONVersion version) {
119         return new Builder(version);
120     }
121
122     public final static class Builder implements Io.Builder<GraphSONPartialIO> {
123
124         private IoRegistry registry = null;
125         private Graph graph;
126         private Consumer<Mapper.Builder> onMapper = null;
127         private final GraphSONVersion version;
128
129         Builder(final GraphSONVersion version) {
130             this.version = version;
131         }
132
133         /**
134          * @deprecated As of release 3.2.2, replaced by {@link #onMapper(Consumer)}.
135          */
136         @Deprecated
137         @Override
138         public Io.Builder<GraphSONPartialIO> registry(final IoRegistry registry) {
139             this.registry = registry;
140             return this;
141         }
142
143         @Override
144         public Io.Builder<? extends Io> onMapper(final Consumer<Mapper.Builder> onMapper) {
145             this.onMapper = onMapper;
146             return this;
147         }
148
149         @Override
150         public Io.Builder<GraphSONPartialIO> graph(final Graph g) {
151             this.graph = g;
152             return this;
153         }
154
155         @Override
156         public GraphSONPartialIO create() {
157             if (null == graph)
158                 throw new IllegalArgumentException("The graph argument was not specified");
159             return new GraphSONPartialIO(this);
160         }
161     }
162 }