Add ApiPath.empty()
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / api / FormatParametersHelper.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.server.api;
9
10 import org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.restconf.api.FormatParameters;
12 import org.opendaylight.restconf.api.query.PrettyPrintParam;
13
14 /**
15  * Helper utilities for structures which contain plain {@link FormatParameters}.
16  */
17 final class FormatParametersHelper {
18     private FormatParametersHelper() {
19         // Hidden on purpose
20     }
21
22     static <T extends FormatParameters> @NonNull T of(final QueryParams params, final @NonNull T compact,
23             final @NonNull T pretty) {
24         var prettyPrint = params.prettyPrint();
25         for (var entry : params.asCollection()) {
26             final var paramName = entry.getKey();
27
28             prettyPrint = switch (paramName) {
29                 case PrettyPrintParam.uriName -> EventStreamGetParams.mandatoryParam(PrettyPrintParam::forUriValue,
30                     paramName, entry.getValue());
31                 default -> throw new IllegalArgumentException("Invalid parameter: " + paramName);
32             };
33         }
34         return prettyPrint.value() ? pretty : compact;
35     }
36 }