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