001/*-
002 * #%L
003 * HAPI FHIR Storage api
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.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 implements IResourcePidList {
034
035        final List<IResourcePersistentId> myIds = new ArrayList<>();
036
037        @Nullable
038        final Date myLastDate;
039
040        private final RequestPartitionId myRequestPartitionId;
041
042        BaseResourcePidList(
043                        Collection<? extends IResourcePersistentId> theIds,
044                        Date theLastDate,
045                        RequestPartitionId theRequestPartitionId) {
046                myIds.addAll(theIds);
047                myLastDate = theLastDate;
048                myRequestPartitionId = theRequestPartitionId;
049        }
050
051        @Override
052        public RequestPartitionId getRequestPartitionId() {
053                return myRequestPartitionId;
054        }
055
056        @Override
057        public Date getLastDate() {
058                return myLastDate;
059        }
060
061        @Override
062        public int size() {
063                return myIds.size();
064        }
065
066        @Override
067        @Nonnull
068        public List<TypedResourcePid> getTypedResourcePids() {
069                List<TypedResourcePid> retval = new ArrayList<>();
070                for (int i = 0; i < myIds.size(); ++i) {
071                        retval.add(new TypedResourcePid(getResourceType(i), myIds.get(i)));
072                }
073                return Collections.unmodifiableList(retval);
074        }
075
076        @Override
077        public boolean isEmpty() {
078                return myIds.isEmpty();
079        }
080
081        @Override
082        public List<IResourcePersistentId> getIds() {
083                return Collections.unmodifiableList(myIds);
084        }
085
086        public IResourcePersistentId getId(int theIndex) {
087                return myIds.get(theIndex);
088        }
089
090        @Override
091        public String toString() {
092                return myIds.toString();
093        }
094}