96 /**
97 * Closes the output. Closes allocated resources and associated output channel.
98 */
99 @Override
100 public void close() {
101 printer.close();
102 }
103
104 /**
105 * Builder to configure and create an instance of {@link GraphOutput}.
106 *
107 * @param <G> the type of the (root element of) graph
108 * @param <N> the type of the nodes
109 * @param <M> the type of the methods
110 */
111 public static final class Builder<G, N, M> {
112 private final GraphStructure<G, N, ?, ?> structure;
113 private GraphElements<M, ?, ?, ?> elements = null;
114 private GraphTypes types = DefaultGraphTypes.DEFAULT;
115 private GraphBlocks<G, ?, N> blocks = DefaultGraphBlocks.empty();
116
117 Builder(GraphStructure<G, N, ?, ?> structure) {
118 this.structure = structure;
119 }
120
121 /**
122 * Associates different implementation of types.
123 *
124 * @param graphTypes implementation of types and enum recognition
125 * @return this builder
126 */
127 public Builder<G, N, M> types(GraphTypes graphTypes) {
128 this.types = graphTypes;
129 return this;
130 }
131
132 /**
133 * Associates implementation of blocks.
134 *
135 * @param graphBlocks the blocks implementation
136 * @return this builder
137 */
138 public Builder<G, N, M> blocks(GraphBlocks<G, ?, N> graphBlocks) {
139 this.blocks = graphBlocks;
140 return this;
141 }
144 * Associates implementation of graph elements.
145 *
146 * @param graphElements the elements implementation
147 * @return this builder
148 */
149 @SuppressWarnings({"unchecked", "rawtypes"})
150 public <E> Builder<G, N, E> elements(GraphElements<E, ?, ?, ?> graphElements) {
151 this.elements = (GraphElements) graphElements;
152 return (Builder<G, N, E>) this;
153 }
154
155 /**
156 * Creates new {@link GraphOutput} to output to provided channel. The output will use
157 * interfaces currently associated with this builder.
158 *
159 * @param channel the channel to output to
160 * @return new graph output
161 * @throws IOException if something goes wrong when writing to the channel
162 */
163 public GraphOutput<G, M> build(WritableByteChannel channel) throws IOException {
164 ProtocolImpl<G, N, ?, ?, ?, M, ?, ?, ?> p = new ProtocolImpl<>(structure, types, blocks, elements, channel);
165 return new GraphOutput<>(p);
166 }
167 }
168 }
|
96 /**
97 * Closes the output. Closes allocated resources and associated output channel.
98 */
99 @Override
100 public void close() {
101 printer.close();
102 }
103
104 /**
105 * Builder to configure and create an instance of {@link GraphOutput}.
106 *
107 * @param <G> the type of the (root element of) graph
108 * @param <N> the type of the nodes
109 * @param <M> the type of the methods
110 */
111 public static final class Builder<G, N, M> {
112 private final GraphStructure<G, N, ?, ?> structure;
113 private GraphElements<M, ?, ?, ?> elements = null;
114 private GraphTypes types = DefaultGraphTypes.DEFAULT;
115 private GraphBlocks<G, ?, N> blocks = DefaultGraphBlocks.empty();
116 private int major = 4;
117 private int minor = 0;
118
119 Builder(GraphStructure<G, N, ?, ?> structure) {
120 this.structure = structure;
121 }
122
123 /**
124 * Chooses which version of the protocol to use. The default version is <code>4.0</code>
125 * (when the {@link GraphOutput} & co. classes were introduced). The default can be changed
126 * to other known versions manually by calling this method.
127 *
128 * @param majorVersion by default 4, newer version may be known
129 * @param minorVersion usually 0
130 * @return this builder
131 * @since 0.28
132 */
133 public Builder<G, N, M> protocolVersion(int majorVersion, int minorVersion) {
134 this.major = majorVersion;
135 this.minor = minorVersion;
136 return this;
137 }
138
139 /**
140 * Associates different implementation of types.
141 *
142 * @param graphTypes implementation of types and enum recognition
143 * @return this builder
144 */
145 public Builder<G, N, M> types(GraphTypes graphTypes) {
146 this.types = graphTypes;
147 return this;
148 }
149
150 /**
151 * Associates implementation of blocks.
152 *
153 * @param graphBlocks the blocks implementation
154 * @return this builder
155 */
156 public Builder<G, N, M> blocks(GraphBlocks<G, ?, N> graphBlocks) {
157 this.blocks = graphBlocks;
158 return this;
159 }
162 * Associates implementation of graph elements.
163 *
164 * @param graphElements the elements implementation
165 * @return this builder
166 */
167 @SuppressWarnings({"unchecked", "rawtypes"})
168 public <E> Builder<G, N, E> elements(GraphElements<E, ?, ?, ?> graphElements) {
169 this.elements = (GraphElements) graphElements;
170 return (Builder<G, N, E>) this;
171 }
172
173 /**
174 * Creates new {@link GraphOutput} to output to provided channel. The output will use
175 * interfaces currently associated with this builder.
176 *
177 * @param channel the channel to output to
178 * @return new graph output
179 * @throws IOException if something goes wrong when writing to the channel
180 */
181 public GraphOutput<G, M> build(WritableByteChannel channel) throws IOException {
182 ProtocolImpl<G, N, ?, ?, ?, M, ?, ?, ?> p = new ProtocolImpl<>(major, minor, structure, types, blocks, elements, channel);
183 return new GraphOutput<>(p);
184 }
185
186 /**
187 * Support for nesting heterogenous graphs. The newly created output uses all the interfaces
188 * currently associated with this builder, but shares with {@code parent} the output
189 * {@code channel}, internal constant pool and {@link #protocolVersion(int, int) protocol
190 * version}.
191 * <p>
192 * Both GraphOutput (the {@code parent} and the returned one) has to be used in
193 * synchronization - e.g. only one
194 * {@link #beginGroup(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object, int, java.util.Map)
195 * begin}, {@link #endGroup() end} of group or
196 * {@link #print(java.lang.Object, java.util.Map, int, java.lang.String, java.lang.Object...)
197 * printing} can be on at a given moment.
198 *
199 * @param parent the output to inherit {@code channel} and protocol version from
200 * @return new output sharing {@code channel} and other internals with {@code parent}
201 */
202 public GraphOutput<G, M> build(GraphOutput<?, ?> parent) {
203 ProtocolImpl<G, N, ?, ?, ?, M, ?, ?, ?> p = new ProtocolImpl<>(parent.printer, structure, types, blocks, elements);
204 return new GraphOutput<>(p);
205 }
206 }
207 }
|