Allow odl-pretty-print to be configured to true
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / QueryParameters.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.collect.ImmutableMap;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.restconf.api.query.RestconfQueryParam;
19 import org.opendaylight.yangtools.concepts.Immutable;
20
21 /**
22  * Query parameters of a RESTCONF request URI. Individual parameters can be looked up by
23  * {@link RestconfQueryParam#paramName()} via {@link #lookup(String)}. All parameters are accessible via
24  * {@link #asCollection()}, where each {@link RestconfQueryParam#paramName()} is guaranteed to be encountered at most
25  * once.
26  */
27 @NonNullByDefault
28 public interface QueryParameters extends Immutable {
29
30     Collection<? extends Entry<String, String>> asCollection();
31
32     @Nullable String lookup(String paramName);
33
34     static QueryParameters of() {
35         return ImmutableQueryParameters.EMPTY;
36     }
37
38     static QueryParameters of(final String paramName, final String paramValue) {
39         return new ImmutableQueryParameters(ImmutableMap.of(paramName, paramValue));
40     }
41
42     static QueryParameters of(final Entry<String, String> entry) {
43         return of(entry.getKey(), entry.getValue());
44     }
45
46     static QueryParameters of(final RestconfQueryParam<?> param) {
47         return of(param.paramName(), param.paramValue());
48     }
49
50     static QueryParameters of(final RestconfQueryParam<?>... params) {
51         return switch (params.length) {
52             case 0 -> of();
53             case 1 -> of(params[0]);
54             default -> new ImmutableQueryParameters(Arrays.asList(params));
55         };
56     }
57
58     static QueryParameters of(final Collection<RestconfQueryParam<?>> params) {
59         return params instanceof List ? of((List<RestconfQueryParam<?>>) params) : switch (params.size()) {
60             case 0 -> of();
61             case 1 -> of(params.iterator().next());
62             default -> new ImmutableQueryParameters(params);
63         };
64     }
65
66     static QueryParameters of(final List<RestconfQueryParam<?>> params) {
67         return switch (params.size()) {
68             case 0 -> of();
69             case 1 -> of(params.get(0));
70             default -> new ImmutableQueryParameters(params);
71         };
72     }
73
74     static QueryParameters of(final Map<String, String> params) {
75         return params.isEmpty() ? of() : new ImmutableQueryParameters(ImmutableMap.copyOf(params));
76     }
77
78     /**
79      * Normalize query parameters from an map containing zero or more values for each parameter value, such as coming
80      * from JAX-RS's {@code UriInfo}.
81      *
82      * @param multiParams Input map
83      * @return An {@link ImmutableQueryParameters} instance
84      * @throws NullPointerException if {@code uriInfo} is {@code null}
85      * @throws IllegalArgumentException if there are multiple values for a parameter
86      */
87     static QueryParameters ofMultiValue(final Map<String, List<String>> multiParams) {
88         if (multiParams.isEmpty()) {
89             return of();
90         }
91
92         final var builder = ImmutableMap.<String, String>builder();
93         for (var entry : multiParams.entrySet()) {
94             final var values = entry.getValue();
95             switch (values.size()) {
96                 case 0 -> {
97                     // No-op
98                 }
99                 case 1 -> builder.put(entry.getKey(), values.get(0));
100                 default -> throw new IllegalArgumentException(
101                     "Parameter " + entry.getKey() + " can appear at most once in request URI");
102             }
103         }
104
105         final var params = builder.build();
106         return params.isEmpty() ? of() : new ImmutableQueryParameters(params);
107     }
108 }