001/*-
002 * #%L
003 * HAPI FHIR Storage api
004 * %%
005 * Copyright (C) 2014 - 2025 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.api.pid;
021
022import ca.uhn.fhir.interceptor.model.RequestPartitionId;
023import ca.uhn.fhir.rest.api.server.storage.IResourcePersistentId;
024import jakarta.annotation.Nonnull;
025import jakarta.annotation.Nullable;
026
027import java.util.ArrayList;
028import java.util.Collection;
029import java.util.Collections;
030import java.util.Date;
031import java.util.List;
032
033public abstract class BaseResourcePidList<T extends IResourcePersistentId<?>> implements IResourcePidList<T> {
034
035        final List<T> myIds = new ArrayList<>();
036
037        @Nullable
038        final Date myLastDate;
039
040        private final RequestPartitionId myRequestPartitionId;
041
042        BaseResourcePidList(Collection<T> theIds, @Nullable Date theLastDate, RequestPartitionId theRequestPartitionId) {
043                myIds.addAll(theIds);
044                myLastDate = theLastDate;
045                myRequestPartitionId = theRequestPartitionId;
046        }
047
048        @Override
049        public RequestPartitionId getRequestPartitionId() {
050                return myRequestPartitionId;
051        }
052
053        @Nullable
054        @Override
055        public Date getLastDate() {
056                return myLastDate;
057        }
058
059        @Override
060        public int size() {
061                return myIds.size();
062        }
063
064        @Override
065        @Nonnull
066        public List<TypedResourcePid> getTypedResourcePids() {
067                List<TypedResourcePid> retval = new ArrayList<>();
068                for (int i = 0; i < myIds.size(); ++i) {
069                        retval.add(new TypedResourcePid(getResourceType(i), myIds.get(i)));
070                }
071                return Collections.unmodifiableList(retval);
072        }
073
074        @Override
075        public boolean isEmpty() {
076                return myIds.isEmpty();
077        }
078
079        @Override
080        public List<T> getIds() {
081                return Collections.unmodifiableList(myIds);
082        }
083
084        public T getId(int theIndex) {
085                return myIds.get(theIndex);
086        }
087
088        @Override
089        public String toString() {
090                return myIds.toString();
091        }
092}