Refactor pretty printing
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / query / PrettyPrintParam.java
1 /*
2  * Copyright (c) 2021 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.query;
9
10 import java.net.URI;
11 import org.eclipse.jdt.annotation.NonNull;
12
13 /**
14  * OpenDaylight extension parameter. When used as {@code odl-pretty-print=true}, it will instruct outbound XML/JSON
15  * formatters to make the output easier for humans to understand.
16  */
17 public final class PrettyPrintParam implements RestconfQueryParam<PrettyPrintParam> {
18     // API consistency: must not be confused with enum constants
19     @SuppressWarnings("checkstyle:ConstantName")
20     public static final String uriName = "odl-pretty-print";
21
22     public static final @NonNull PrettyPrintParam FALSE = new PrettyPrintParam(false);
23     public static final @NonNull PrettyPrintParam TRUE = new PrettyPrintParam(true);
24
25     private static final @NonNull URI CAPABILITY =
26         URI.create("urn:opendaylight:params:restconf:capability:pretty-print:1.0");
27
28     private final boolean value;
29
30     private PrettyPrintParam(final boolean value) {
31         this.value = value;
32     }
33
34     public static @NonNull PrettyPrintParam of(final boolean value) {
35         return value ? TRUE : FALSE;
36     }
37
38     public static @NonNull PrettyPrintParam forUriValue(final String uriValue) {
39         return switch (uriValue) {
40             case "false" -> FALSE;
41             case "true" -> TRUE;
42             default -> throw new IllegalArgumentException("Value can be 'false' or 'true', not '" + uriValue + "'");
43         };
44     }
45
46     @Override
47     public Class<PrettyPrintParam> javaClass() {
48         return PrettyPrintParam.class;
49     }
50
51     @Override
52     public String paramName() {
53         return uriName;
54     }
55
56     @Override
57     public String paramValue() {
58         return String.valueOf(value);
59     }
60
61     public boolean value() {
62         return value;
63     }
64
65     public static @NonNull URI capabilityUri() {
66         return CAPABILITY;
67     }
68 }