001/*-
002 * #%L
003 * HAPI FHIR JPA - Search Parameters
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.jpa.cache;
021
022import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
023import org.hl7.fhir.instance.model.api.IIdType;
024
025import java.util.HashMap;
026import java.util.List;
027import java.util.Map;
028
029public class ResourcePersistentIdMap {
030        private final Map<IIdType, IResourcePersistentId> myMap = new HashMap<>();
031
032        public static ResourcePersistentIdMap fromResourcePersistentIds(
033                        List<IResourcePersistentId> theResourcePersistentIds) {
034                ResourcePersistentIdMap retval = new ResourcePersistentIdMap();
035                theResourcePersistentIds.forEach(retval::add);
036                return retval;
037        }
038
039        private void add(IResourcePersistentId theResourcePersistentId) {
040                IIdType id = theResourcePersistentId.getAssociatedResourceId();
041                myMap.put(id.toUnqualifiedVersionless(), theResourcePersistentId);
042        }
043
044        public boolean containsKey(IIdType theId) {
045                return myMap.containsKey(theId.toUnqualifiedVersionless());
046        }
047
048        public IResourcePersistentId getResourcePersistentId(IIdType theId) {
049                return myMap.get(theId.toUnqualifiedVersionless());
050        }
051
052        public boolean isEmpty() {
053                return myMap.isEmpty();
054        }
055
056        public int size() {
057                return myMap.size();
058        }
059
060        public void put(IIdType theId, IResourcePersistentId thePid) {
061                myMap.put(theId, thePid);
062        }
063
064        public void putAll(ResourcePersistentIdMap theIdAndPID) {
065                myMap.putAll(theIdAndPID.myMap);
066        }
067}