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