Rework body formatting wiring
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / FormattableBody.java
1 /*
2  * Copyright (c) 2024 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.restconf.api;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.base.MoreObjects.ToStringHelper;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.opendaylight.yangtools.concepts.Immutable;
16
17 /**
18  * A body which is capable of being formatted to an {@link OutputStream} in either JSON or XML format using particular
19  * {@link FormatParameters}.
20  */
21 @NonNullByDefault
22 public abstract class FormattableBody implements Immutable {
23     /**
24      * Write the content of this body as a JSON document.
25      *
26      * @param format {@link FormatParameters}
27      * @param out output stream
28      * @throws IOException if an IO error occurs.
29      */
30     public abstract void formatToJSON(FormatParameters format, OutputStream out) throws IOException;
31
32     /**
33      * Write the content of this body as an XML document.
34      *
35      * @param format {@link FormatParameters}
36      * @param out output stream
37      * @throws IOException if an IO error occurs.
38      */
39     public abstract void formatToXML(FormatParameters format, OutputStream out) throws IOException;
40
41     protected abstract ToStringHelper addToStringAttributes(ToStringHelper helper);
42
43     @Override
44     public final String toString() {
45         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
46     }
47 }