Add generalized FileGenerator interface
[yangtools.git] / plugin / plugin-generator-api / src / main / java / org / opendaylight / yangtools / plugin / generator / api / ByteSourceGeneratedFile.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.plugin.generator.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.io.ByteSource;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * A generated file with a body is available as a {@link ByteSource}.
21  */
22 @NonNullByDefault
23 final class ByteSourceGeneratedFile extends AbstractGeneratedFile {
24     private final ByteSource body;
25
26     ByteSourceGeneratedFile(final GeneratedFileLifecycle lifecycle, final ByteSource body) {
27         super(lifecycle);
28         this.body = requireNonNull(body);
29     }
30
31     @Override
32     public void writeBody(final @NonNull OutputStream output) throws IOException {
33         body.copyTo(output);
34     }
35
36     @Override
37     protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
38         return helper.add("body", body);
39     }
40 }