2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.plugin.generator.api;
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;
20 * The contents of a generated file and its {@link GeneratedFileLifecycle}.
22 * @author Robert Varga
26 public interface GeneratedFile extends Immutable {
28 * Return the lifecycle governing this file.
30 * @return Governing lifecycle
32 GeneratedFileLifecycle getLifecycle();
35 * Write the body of this file into specified {@link OutputStream}.
37 * @param output stream where to write the output
38 * @throws IOException when the stream reports an IOException
40 void writeBody(OutputStream output) throws IOException;
43 * Create a new {@link GeneratedFile} with the specified {@link CharSequence} body. The body will be encoded in
44 * {@link StandardCharsets#UTF_8}.
46 * @param lifecycle File lifecycle
47 * @param body File body
48 * @return A GeneratedFile.
49 * @throws NullPointerException if any argument is null
51 static GeneratedFile of(final GeneratedFileLifecycle lifecycle, final CharSequence body) {
52 return new CharSeqGeneratedTextFile(lifecycle, body);
56 * Create a new {@link GeneratedFile} with the specified {@link CharSource} body. The body will be encoded in
57 * {@link StandardCharsets#UTF_8}.
59 * @param lifecycle File lifecycle
60 * @param body File body
61 * @return A GeneratedFile.
62 * @throws NullPointerException if any argument is null
64 static GeneratedFile of(final GeneratedFileLifecycle lifecycle, final CharSource body) {
65 return new CharSourceGeneratedTextFile(lifecycle, body);
69 * Create a new {@link GeneratedFile} with the specified {@link ByteSource} body. The body will be written as is.
71 * @param lifecycle File lifecycle
72 * @param body File body
73 * @return A GeneratedFile.
74 * @throws NullPointerException if any argument is null
76 static GeneratedFile of(final GeneratedFileLifecycle lifecycle, final ByteSource body) {
77 return new ByteSourceGeneratedFile(lifecycle, body);