Add generalized FileGenerator interface
[yangtools.git] / plugin / plugin-generator-api / src / main / java / org / opendaylight / yangtools / plugin / generator / api / GeneratedFile.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 com.google.common.annotations.Beta;
11 import com.google.common.io.ByteSource;
12 import com.google.common.io.CharSource;
13 import java.io.IOException;
14 import java.io.OutputStream;
15 import java.nio.charset.StandardCharsets;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.opendaylight.yangtools.concepts.Immutable;
18
19 /**
20  * The contents of a generated file and its {@link GeneratedFileLifecycle}.
21  *
22  * @author Robert Varga
23  */
24 @Beta
25 @NonNullByDefault
26 public interface GeneratedFile extends Immutable {
27     /**
28      * Return the lifecycle governing this file.
29      *
30      * @return Governing lifecycle
31      */
32     GeneratedFileLifecycle getLifecycle();
33
34     /**
35      * Write the body of this file into specified {@link OutputStream}.
36      *
37      * @param output stream where to write the output
38      * @throws IOException when the stream reports an IOException
39      */
40     void writeBody(OutputStream output) throws IOException;
41
42     /**
43      * Create a new {@link GeneratedFile} with the specified {@link CharSequence} body. The body will be encoded in
44      * {@link StandardCharsets#UTF_8}.
45      *
46      * @param lifecycle File lifecycle
47      * @param body File body
48      * @return A GeneratedFile.
49      * @throws NullPointerException if any argument is null
50      */
51     static GeneratedFile of(final GeneratedFileLifecycle lifecycle, final CharSequence body) {
52         return new CharSeqGeneratedTextFile(lifecycle, body);
53     }
54
55     /**
56      * Create a new {@link GeneratedFile} with the specified {@link CharSource} body. The body will be encoded in
57      * {@link StandardCharsets#UTF_8}.
58      *
59      * @param lifecycle File lifecycle
60      * @param body File body
61      * @return A GeneratedFile.
62      * @throws NullPointerException if any argument is null
63      */
64     static GeneratedFile of(final GeneratedFileLifecycle lifecycle, final CharSource body) {
65         return new CharSourceGeneratedTextFile(lifecycle, body);
66     }
67
68     /**
69      * Create a new {@link GeneratedFile} with the specified {@link ByteSource} body. The body will be written as is.
70      *
71      * @param lifecycle File lifecycle
72      * @param body File body
73      * @return A GeneratedFile.
74      * @throws NullPointerException if any argument is null
75      */
76     static GeneratedFile of(final GeneratedFileLifecycle lifecycle, final ByteSource body) {
77         return new ByteSourceGeneratedFile(lifecycle, body);
78     }
79 }