001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.server.util;
021
022import ca.uhn.fhir.context.RuntimeSearchParam;
023import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
024
025import java.util.Collection;
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.LinkedHashMap;
029import java.util.Map;
030import java.util.Set;
031import java.util.stream.Stream;
032
033public class ResourceSearchParams {
034        private final String myResourceName;
035        private final Map<String, RuntimeSearchParam> myMap;
036
037        public ResourceSearchParams(String theResourceName) {
038                myResourceName = theResourceName;
039                myMap = new LinkedHashMap<>();
040        }
041
042        private ResourceSearchParams(String theResourceName, Map<String, RuntimeSearchParam> theMap) {
043                myResourceName = theResourceName;
044                myMap = theMap;
045        }
046
047        public Collection<RuntimeSearchParam> values() {
048                return myMap.values();
049        }
050
051        public static ResourceSearchParams empty(String theResourceName) {
052                return new ResourceSearchParams(theResourceName, Collections.emptyMap());
053        }
054
055        public ResourceSearchParams readOnly() {
056                return new ResourceSearchParams(myResourceName, Collections.unmodifiableMap(this.myMap));
057        }
058
059        public void remove(String theName) {
060                myMap.remove(theName);
061        }
062
063        public int size() {
064                return myMap.size();
065        }
066
067        public RuntimeSearchParam get(String theParamName) {
068                return myMap.get(theParamName);
069        }
070
071        public RuntimeSearchParam put(String theName, RuntimeSearchParam theSearchParam) {
072                return myMap.put(theName, theSearchParam);
073        }
074
075        public void addSearchParamIfAbsent(String theParamName, RuntimeSearchParam theRuntimeSearchParam) {
076                myMap.putIfAbsent(theParamName, theRuntimeSearchParam);
077        }
078
079        public Set<String> getSearchParamNames() {
080                return myMap.keySet();
081        }
082
083        public boolean containsParamName(String theParamName) {
084                return myMap.containsKey(theParamName);
085        }
086
087        public void removeInactive() {
088                myMap.entrySet()
089                                .removeIf(entry ->
090                                                entry.getValue().getStatus() != RuntimeSearchParam.RuntimeSearchParamStatusEnum.ACTIVE);
091        }
092
093        public Stream<String> getReferenceSearchParamNames() {
094                return myMap.entrySet().stream()
095                                .filter(entry -> entry.getValue().getParamType() == RestSearchParameterTypeEnum.REFERENCE)
096                                .map(Map.Entry::getKey);
097        }
098
099        public ResourceSearchParams makeCopy() {
100                return new ResourceSearchParams(myResourceName, new HashMap<>(myMap));
101        }
102}