Add ApiPath.empty()
[netconf.git] / protocol / restconf-api / src / main / java / org / opendaylight / restconf / api / ImmutableQueryParameters.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 static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Collection;
14 import java.util.Map.Entry;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.restconf.api.query.RestconfQueryParam;
18
19 /**
20  * Default implementation of {@link QueryParameters}.
21  */
22 @NonNullByDefault
23 record ImmutableQueryParameters(ImmutableMap<String, String> params) implements QueryParameters {
24     static final ImmutableQueryParameters EMPTY = new ImmutableQueryParameters(ImmutableMap.of());
25
26     ImmutableQueryParameters {
27         requireNonNull(params);
28     }
29
30     ImmutableQueryParameters(final Collection<RestconfQueryParam<?>> params) {
31         // TODO: consider caching common request parameter combinations
32         this(params.stream()
33             .collect(ImmutableMap.toImmutableMap(RestconfQueryParam::paramName, RestconfQueryParam::paramValue)));
34     }
35
36     @Override
37     public boolean isEmpty() {
38         return params.isEmpty();
39     }
40
41     @Override
42     public Collection<Entry<String, String>> asCollection() {
43         return params.entrySet();
44     }
45
46     @Override
47     public @Nullable String lookup(final String paramName) {
48         return params.get(requireNonNull(paramName));
49     }
50
51     @Override
52     public String toString() {
53         return QueryParameters.class.getSimpleName() + "(" + params + ")";
54     }
55 }